Maven - Exclude class files from war
Solution 1
As the documentation says, you should use the packagingExcludes
element instead.
Note that packagingExcludes
doesn't use nested elements. Instead, the content is a "comma-separated list of Ant file set patterns".
Solution 2
From the documentation, excludes will take higher priority over includes. In your case, use packagingExcludes for excluding class2 explicitly.
<packagingExcludes>WEB-INF/classes/com/tuto/Class2.class</packagingExcludes>
Solution 3
The complete plugin xml should be like the following
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<outputDirectory>${build.output.directory}</outputDirectory>
<packagingExcludes>WEB-INF/classes/ItemConsumer.class</packagingExcludes>
</configuration>
</plugin>
Solution 4
add plugin to pom.xml - 100% working properly.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<!--package path you want to delete-->
<packagingExcludes>WEB-INF/classes/lk/**</packagingExcludes>
<webResources>
<resource>
<!--project build path-->
<directory>${project.basedir}/target/classes/</directory>
<filtering>true</filtering>
<includes><include>**/*.class</include></includes>
</resource>
</webResources>
</configuration>
</plugin>
tweetysat
Updated on August 06, 2022Comments
-
tweetysat almost 2 years
I have :
src/main/java/com.tuto
Class1.java
Class2.java
Class3.java
My pom is packaging a war. So in my war, in
WEB-INF/classes/com/tuto
I found the 3 classes.Now I want to exclude Class2.java
I tried
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.1</version> <configuration> <excludes> <exclude>**/Class2.class</exclude> </excludes> </configuration> </plugin>
But it's not working.
How can I do ?
Ok, so like Aaron tell me, this is working :
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.1</version> <configuration> <packagingExcludes>**/Class2.class</packagingExcludes> </configuration> </plugin>
But ...
Let's take the problem by the other side : I want to exclude all except Class1 and Class3
So I tried
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.1</version> <configuration> <packagingIncludes>**/Class1.class,**/Class3.class</packagingIncludes> </configuration> </plugin>
and this time it's not working.