maven2: excluding directory from WAR

2019-01-10 20:57发布

问题:

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?

回答1:

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>


回答2:

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>


回答3:

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>


回答4:

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>


回答5:

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>