1

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 ?

Simeon
  • 7,582
  • 15
  • 64
  • 101
  • Run it with -X and see if you can get info - you can just run the war:war. – gkamal May 08 '12 at 08:48
  • @gkamal I can't see anything out of the ordinary. – Simeon May 08 '12 at 08:55
  • Can you try with the full path of one of the jars instead of the wildcard? – gkamal May 08 '12 at 10:29
  • @gkamal I did, doesn't work :) – Simeon May 08 '12 at 10:48
  • 1
    what version of the plugin are you using? the documentation says this "In version 2.1-alpha-1, this was incorrectly named warSourceExcludes" - http://maven.apache.org/plugins/maven-war-plugin/examples/skinny-wars.html – gkamal May 08 '12 at 11:52
  • 2
    Another way of fixing this would be to add explicit dependencies for the two jars into your war project and mark the scope as provided. – gkamal May 08 '12 at 11:53

4 Answers4

3

This means you have them as dependency in your project so they will be packaged into the war. Just remove the dependencies so they wont be packaged anymore.

Based on the documentation of the option you used you have to use regex which means you should write:

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

instead of

<packagingExcludes>WEB-INF/lib/jaxb*.jar</packagingExcludes>
khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • No. We do not have a dependency. The dependency is transitive, meaning that a library we use uses a JAXB implementation and we want to use the JDK implementation. – Simeon May 08 '12 at 09:05
  • I did try `WEB-INF/lib/jaxb.*` to the same result. – Simeon May 08 '12 at 09:06
  • I also don't see where the documentation states that a regex should be used. The syntax `**/*.xml` is not a regex and is an example in the documentation. – Simeon May 08 '12 at 09:08
  • 1
    Ah.. Transitive dependencies than this will not work (As far as i know). You have to add the dependency which picks up the jaxb deps and do an excludes in the dependencies itself. – khmarbaise May 08 '12 at 09:08
  • Thanks a lot, will try it. Could you please add this to your answer if you have the time ? – Simeon May 08 '12 at 09:12
3

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>
Community
  • 1
  • 1
Jörn Horstmann
  • 33,639
  • 11
  • 75
  • 118
  • Accepting this as it seems a bit clearer and has more options (personal opinion only, both answers are correct IMO). – Simeon Jun 19 '12 at 15:21
2

Further to @gkamal's comment to your question (08/05/12@11:52), check your maven-war-plugin's version. I've just spent 2hrs looking at this issue myself to exclude an unknown transient to javaee-api*.jar.

With maven 3.0.4, I was defaulted to maven-war-plugin version 2.1 (you can tell if you run your build in debug - e.g. mvn clean package -X). One of your other comments is correct in saying package-excludes came in after this. See the war plugin page for details (although the actual page describing the entry doesn't indicate version info which is pretty poor as that's what you first search for).

If you update to maven-war-plugin to 2.3 (add <version>2.3</version>, your packaging-excludes should be used. Note though you will only see this in the built war's WEB-INF/lib, not in the transient war directory (which includes the excluded jars even if debug says they're to be excluded, which is very confusing when looking at this issue).

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.3</version>
    ...
    <packagingExcludes>
      WEB-INF/lib/whatever-*.jar,
      WEB-INF/lib/javaee-api-*.jar
    </packagingExcludes>
    ...
  </plugin>
  ...

However, in terms of best practice, this is probably a last gasp effort to exclude jars fromo the war and the dependency-level exclusions for transient jars is probably the most precise and correct way. That said, what if a transient jar is being brought in by multiple dependencies?

So, with the version upgrade, I think @khmarbaise's solution is fine (and the comment indicating it won't work is wrong). However, I think best practice is to use dependency-level exclusions as per your accepted answer.

Community
  • 1
  • 1
wmorrison365
  • 5,995
  • 2
  • 27
  • 40
1

You can do this by specifying inside <packagingExcludes></packagingExcludes> inside </configuration><configuration>.

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.0</version>
        <configuration>
          <packagingExcludes>
            WEB-INF/lib/ex1-*.jar,
            WEB-INF/lib/ex2-logging-*.jar
          </packagingExcludes>
        </configuration>
      </plugin>
    </plugins>
  </build>
  ...
</project>

You can specify path by wild cards and regular expressions too. See this link for more info.

Supun Wijerathne
  • 11,964
  • 10
  • 61
  • 87