How to exclude files from being copied, by maven, into the exploded war

18,636

Solution 1

Thanks to @alexksandr & @maba for their answers - which though correct didn't fully resolve my issue.

The solution seems to be - though I am not sure exactly why this is the case - that the configuration section is not picked up on when it is placed within the execution section.

Taking my original pom.xml and re-factoring it to make it work gives:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-war-plugin</artifactId>
  <version>2.1.1</version>
  <configuration>
    <failOnMissingWebXml>false</failOnMissingWebXml>
    <overlays>
    </overlays>
    <webappDirectory>${project.build.directory}/${project.build.finalName}-work</webappDirectory>
    <warSourceExcludes>client/</warSourceExcludes>
  </configuration>
  <executions>
    <execution>
      <id>parent-resources</id>
      <phase>generate-sources</phase>
      <goals>
        <goal>exploded</goal>
      </goals>
    </execution>
  </executions>
</plugin>

The important detail seems to be that the configuration should be at the top level of the plugin and not within the execution section - though clearly the xml in my first attempt to use warSourceExcludes was way off target (see original question prior to the update section).

Solution 2

I could find no configuration of <warSourceExcludes> which worked, however <packagingExcludes> did work:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.5</version>
    <configuration>
      <packagingExcludes>excludeMe/**</packagingExcludes>
    </configuration>
  </plugin>

Solution 3

Your warSourceExcludes is wrong. Here is what the maven-war-plugin says regarding warSourceExludes:

The comma separated list of tokens to exclude when copying the content of the warSourceDirectory.

So in your case this is what your configuration should look like:

<configuration>
  ...
  <warSourceExcludes>client/</warSourceExcludes>
  ...
</configuration>

This means also that if you want to add some more excludes, just add them separated with a comma:

<configuration>
  ...
  <warSourceExcludes>client/,otherDir/,evenMoreDir/</warSourceExcludes>
  ...
</configuration>

Solution 4

In order to exclude all files from folder use wildcards like that client/**

<configuration>
 <failOnMissingWebXml>false</failOnMissingWebXml>
 <overlays>
 </overlays>                                 
 <webappDirectory>${project.build.directory}/${project.build.finalName}-work</webappDirectory>
 <warSourceExcludes>client/**</warSourceExcludes>
</configuration>
Share:
18,636
Kris
Author by

Kris

Updated on July 13, 2022

Comments

  • Kris
    Kris about 1 year

    I have a Java web app, in which I have some folders within the standard webapp source directory (src/main/webapp) that I don't want to get copied over into the war (exploded or packaged).

    One of the reasons I don't want these files copying over is that we run the YUI JS & CSS minimizer & compressor on .js and .css files within the exploded war. The files that I want to exclude produce errors during the compression phase. The other reason I don't want them adding to the war is that they support testing a single page JS app that lives within the webapp (they are client side JS test scripts that rely on node / angular.js).

    Below are the relevant sections from the POM.xml:

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-war-plugin</artifactId>
      <version>2.1.1</version>
      <executions>
        <execution>
          <id>parent-resources</id>
          <configuration>
            <failOnMissingWebXml>false</failOnMissingWebXml>
            <overlays>
            </overlays>
            <webappDirectory>${project.build.directory}/${project.build.finalName}-work</webappDirectory>
          </configuration>
          <phase>generate-sources</phase>
          <goals>
            <goal>exploded</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    

    I have tried, unsuccessfully, to use warSourceExcludes to exclude certain paths, but to no avail. An example of my usage is shown below, where client/ is a folder directly beneath src/main/webapp:

    <configuration>
      ...
      <warSourceExcludes>
        <excludes>
          <exclude>
            client/
          </exclude>
        </excludes>
      </warSourceExcludes>
      ...
    </configuration>
    

    What is the correct way to exclude certain paths, and or individual files, within the web app source directory from being included in the exploded war?

    UPDATE

    Following on from the suggestion from @maba I updated the configuration as follows:

    <configuration>
      <failOnMissingWebXml>false</failOnMissingWebXml>
      <overlays>
      </overlays>                                 
      <webappDirectory>${project.build.directory}/${project.build.finalName}-work</webappDirectory>
      <warSourceExcludes>client/</warSourceExcludes>
    </configuration>
    

    The folder, client/, still is getting copied across. Any ideas?

  • Kris
    Kris over 10 years
    Hi,First off thanks for the response. just tried your suggestion with no luck, Will update my question to include what I have tried. thanks
  • maba
    maba over 10 years
    I tried with your setup and I got a folder in target that ends with -work and in that folder there is no sign of the client directory.
  • maba
    maba over 10 years
    Do you want to create a war as well with the same layout? Or is the exploded war the only result you want?
  • Kris
    Kris over 10 years
    strange.... I am running mvn clean war:exploded and the client/ still gets copied over. Not sure what is going on here
  • Kris
    Kris over 10 years
    forgot to say I want it excluded from the packaged war as well
  • maba
    maba over 10 years
    That's how maven plugins work. They have either configuration bound to be executed in a build OR configuration for command line execution. Take a look at the maven-dependency-plugin as an example. It is quite specific about how the two configuration parts differ. Your requirements were not totally clear. But it's nice that you now have a working configuration.
  • Kris
    Kris over 10 years
    thanks for the clarification - and apollogies if I didn't describe the issue correctly (probably due to me not really getting the finer details of maven). But thanks again maba @aleksandr for your help!
  • maba
    maba over 10 years
    If you found our answers useful/helpful then upvote them. And accept your own answer if that is what you found to be the best solution.
  • Kris
    Kris over 10 years
    yes, apologies. Have up-voted your answers as they were indeed useful. thanks again
  • Cesar
    Cesar about 8 years
    That solved my problem with minify-maven-plugin, which was keeping the concatenated files in my .war, even with the use of <warSourceExcludes>.
  • jediz
    jediz over 6 years
    note that packagingExcludes only excludes from the .war file, not from the exploded folder
  • lenach87
    lenach87 about 3 years
    great you mentioned that part on execution section, saved me now 8 years later from the same mistake.