47

I tried this to exclude whole directory (${basedir}/src/main/webapp/webscripts) from my WAR file but it failed. What is wrong?

this doesn't work:

<configuration>
   <webResources>
      <resource>
       <directory>${basedir}/src/main/webapp/webscripts</directory>
       <excludes>
        <exclude>**/*.*</exclude>
       </excludes>
      </resource>
   </webResources>
</configuration>

this too:

<configuration>
   <webResources>
      <resource>
       <directory>${basedir}/src/main/webapp</directory>
       <excludes>
        <exclude>**/webscripts</exclude>
       </excludes>
      </resource>
   </webResources>
</configuration>

Can anybody help?

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
pls
  • 473
  • 1
  • 4
  • 4

5 Answers5

93

Both of your solutions wouldn't help, as they would add an additional resource that is then deactivated. The webapp source folder is copied by default, without the resource mechanism.

The mechanism to deactivate a part of that is through the <warSourceExcludes> parameter, like this:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.1</version>
    <configuration>
        <warSourceExcludes>webscripts/**</warSourceExcludes>
    </configuration>
</plugin>
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
33

warSourceExcludes seems not to have been just renamed packagingExcludes.

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

packagingExcludes: The comma separated list of tokens to exclude from the WAR before packaging. This option may be used to implement the skinny WAR use case.

There is a big difference: With packagingExcludes, the tokens are completely excluded from the final war file. With warSourceExcludes, the tokens are just ignored when copying the war directory into the war file. As a result, if the tokens are present in the webappDirectory for example, they will not be ignored when using warSourceExcludes but will be when using packagingExcludes.

And a working syntax example:

<warSourceExcludes>**/${project.artifactId}/**</warSourceExcludes>
Thomas
  • 1,053
  • 2
  • 11
  • 20
  • What do you mean by "ignored when copying"??? If they are not copied to the war file - they are not packaged in the war file, and it's the same as packagingExcludes. Can you explain it more clearly? – kolobok May 30 '16 at 07:37
  • 1
    packagingExcludes doesn't copy to final war while warSourceExcludes doesn't copy to warDirectory that will be compressed to a war. – Frohlich Aug 31 '16 at 21:06
7

In version 2.1-alpha-1, this was incorrectly named warSourceExcludes. The right parameter is packagingExcludes

Example of usage (excludes WEB-INF/statics/ folder from WAR):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.1.1</version>
    <configuration>
        <warName>searchservice-web</warName>
        <packagingExcludes>WEB-INF/statics/</packagingExcludes>
    </configuration>
</plugin>
igor
  • 5,351
  • 2
  • 26
  • 22
  • 2
    This is incorrect and misled me for a while. I am using version 2.1.1, but packagingExcludes does not work, while warSourceExcludes does. It looks like in newer versions the packagingExcludes is the correct tag to use. – Noremac Jul 10 '13 at 18:19
5

Project Structure

Inorder to remove the source files i have used the below configuration in maven

 <plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.2.0</version>
    <configuration>
      <packagingExcludes>
        eb-app/app/**,eb-app/node_modules/**,eb-app/public/**,eb-app/server/**,eb-app/tests/**,eb-app/tmp/**,eb-app/vendor/**,eb-app/*
      </packagingExcludes>
    </configuration>
  </plugin>
Mani
  • 604
  • 7
  • 9
  • I am using SpringBoot 2.0.3.RELEASE and I am also trying to create a WAR of my project which has an angular2 front end and java rest back end. I tried this to exclude my node_modules folder, but didn't work. – Thilina Samiddhi Aug 03 '18 at 11:32
1

Had a scenario where I had to exclude two folders which could be matched by *scripts and they were in the resources folder. The first confusion was whether to provide the exclude value as src/main/resources/*scripts or as WEB-INF/classes/*scripts, i.e. pre or post compilation structure.

Was much important to provide /** to get the entire directory being excluded from the war file. In my case, *scripts/**.

Here is the final configuration which worked:

    <packagingExcludes>WEB-INF/classes/*scripts/**</packagingExcludes>
James Jithin
  • 10,183
  • 5
  • 36
  • 51