Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid duplication of derived dependencies in EAR/WAR with Maven?

To make the example as simple as possible, let's say I have classic Java EE 5 application.
Let's say I use x-lib in EAR module, and x-lib uses commons-io.
Also I use y-lib in WAR module, and y-lib uses commons-io too. The EAR is set as provided in WAR.

The result I get is:

- app.ear
    /lib
       x-lib.jar
       commons-io.jar
    /app.war
       /WEB-INF
          /lib
             y-lib.jar
             commons-io.jar

I don't want commons-io to be packaged in app.war/WEB-INF/lib once it is packaged already in app.ear/lib.

In my war's pom.xml I have:

<dependency>
   <groupId>my.group</groupId>
   <artifactId>app</artifactId>
   <type>ejb</artifact>
   <scope>provided</scope>  
   <version>${project.version}</version>
</dependency>

<dependency>
   <groupId>some.other.group</groupId>
   <artifactId>y-lib</artifactId> <!-- This loads commons-io as compile dependency -->
   <version>1.2.3</version>
</dependency>

Is there a way to tell maven that I want everything that is provided along with app ejb dependency should be set to provided and not included in WAR?

I do not want to track all those duplicated JARs and set them as provided or exclude explicitly one by one.

EDIT

I am aware of skinny-war solution. However I don't like the drwaback of duplicating dependencies in WAR and EAR. Maybe you know how to overcome this.

like image 274
Piotr Gwiazda Avatar asked Jan 31 '26 09:01

Piotr Gwiazda


1 Answers

From the version 2.7 of the maven-ear-plugin, you can use SkinnyWars.
See here for specs: https://maven.apache.org/plugins/maven-ear-plugin/examples/skinny-wars.html

Basically, you can add the skinnyWars tag to your plugin configuration:

<plugin>
  <artifactId>maven-ear-plugin</artifactId>
  <version>2.10.1</version>
  <configuration>
    <defaultLibBundleDir>lib/</defaultLibBundleDir>
    <skinnyWars>true</skinnyWars>
  </configuration>
</plugin>
like image 152
Andrea Colleoni Avatar answered Feb 03 '26 00:02

Andrea Colleoni