Search code examples
javamavenjaxbmaven-3war

Can't get the maven-war-plugin to exclude jaxb jars


I'm trying to use the <packagingExcludes> of the Maven war-plugin.

This is my configuration:

          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <archive>
                    <manifestEntries>
                        <Implementation-Version>${project.artifactId}-${project.version}-r${buildNumber}</Implementation-Version>
                        <Implementation-Buildtime>${timestamp}</Implementation-Buildtime>
                    </manifestEntries>
                </archive>

                <packagingExcludes>WEB-INF/lib/jaxb*.jar</packagingExcludes>
            </configuration>
        </plugin>

In my understanding this line:

<packagingExcludes>WEB-INF/lib/jaxb*.jar</packagingExcludes>

Should exclude all jars starting with 'jaxb' from the built .war file.

However after I run clean install I get both:

jaxb-api-2.1.jar
jaxb-impl-2.1.3.jar

Packaged in my .war WEB-INF/lib dir.

I'm using Maven 3.

Any help greatly appreciated.

Thanks in advance,


To answer gkamal's comment.

When I run mvn war:war -X I can see:

[DEBUG] Processing: jaxb-api-2.1.jar
[DEBUG]  + WEB-INF/lib/jaxb-api-2.1.jar has been copied.
[DEBUG] Processing: jaxb-impl-2.1.3.jar
[DEBUG]  + WEB-INF/lib/jaxb-impl-2.1.3.jar has been copied.

Also

[DEBUG] Excluding [WEB-INF/lib/jaxb*.jar] from the generated webapp archive.

No, exceptions, warning or errors or nothing that looks suspicious, anything specific I should look for ?


Solution

  • For a transitive dependency, you can use the exclusions element to exclude it.

    <dependency>
        <groupId>...</groupId>
        <artifactId>...</artifactId>
        <version>...</version>
        <exclusions>
            <exclusion>
                <groupId>javax.xml.bind</groupId>
                <artifactId>jaxb-api-2.1</artifactId>
            </exclusion>
        </exclusions>
    </dependency> 
    

    As gkamal commented, you could also add an explicit dependency on jaxb and set its scope to provided, this will override the scope of the transitive dependency so it is no longer packaged.

    Another alternative, the war plugin also allows to exclude based on regular expressions, but the syntax is a bit more involved, the following snippet should exclude everything under lib whose filename starts with "jaxb":

    <packagingExcludes>%regex[WEB-INF/lib/jaxb.*]</packagingExcludes>