Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven - Exclude class files from war

Tags:

maven-2

war

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.

like image 725
tweetysat Avatar asked Sep 10 '13 07:09

tweetysat


People also ask

How to exclude file from WAR?

It is possible to include or exclude certain files from the WAR file, by using the <packagingIncludes> and <packagingExcludes> configuration parameters.

Can we exclude a class from Maven dependency?

Since Maven resolves dependencies transitively, it is possible for unwanted dependencies to be included in your project's classpath. For example, a certain older jar may have security issues or be incompatible with the Java version you're using. To address this, Maven allows you to exclude specific dependencies.

How to exclude dependencies in Maven?

You can use a diagram to exclude a dependency from the project's POM. Select a dependency in the diagram window. From the context menu, choose Exclude. From the list, select the module (if any) where the exclusion definition will be added.

How do I exclude properties from a jar file?

xml file to exclude property files. You need to specify the directory where the files are reside in your application structure. Also you need to specify the file types to be excluded using exclude tag.


3 Answers

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".

like image 190
Aaron Digulla Avatar answered Sep 24 '22 20:09

Aaron Digulla


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>
like image 37
siddhuKantipudi Avatar answered Sep 25 '22 20:09

siddhuKantipudi


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>
like image 33
Zeus Avatar answered Sep 24 '22 20:09

Zeus