23
votes

We have a solution with numerous wars. Wars are similar in the sense they all use hibernate and spring. This means that we have a number of same jars inside each war. This is becoming a problem, because the size of the ear is starting to grow out of proportion.

I would like to use Maven to calculate dependencies and to place all jars common to multiple wars to the root of the EAR.

I tried organizing my project using j2ee archetype (maven-archetype-j2ee-simple), but all wars are still packaged with dependencies inside the WEB-INF/lib. Is there a way to make Maven calculate common dependencies and place them to EAR, just as he is able to calculate all transitional dependencies when constructing a war or a jar?

4

4 Answers

66
votes

As you've mentioned in a comment, it's maven's task to calculate every dependency. When you're creating an artifact, with every common dependency, then you'll also have to guess, which dependencies belong there.

It could also be possible, that you have to deploy one war, with it's dependencies on another machine without an ear, an when you set every war dependency to provided, then you're stuck again.

The only right way, to get skinny wars is from the examples: http://maven.apache.org/plugins/maven-war-plugin/examples/skinny-wars.html

But, and now comes the interesting part, there is one big! shortcut (which completly takes away the mentioned pain), to tell maven, which dependencies your WARs have.

Go inside your EAR-Module an declare a second dependency on the WAR with type pom for every WAR dependency.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>com.foo</groupId>
    <artifactId>skinny</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>ear</artifactId>
<packaging>ear</packaging>
<dependencies>
    <dependency>
        <groupId>com.foo</groupId>
        <artifactId>war</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <type>war</type>
    </dependency>
    <dependency>
        <groupId>com.foo</groupId>
        <artifactId>war</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <type>pom</type>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-ear-plugin</artifactId>
            <version>2.8</version>
            <configuration>
                <skinnyWars>true</skinnyWars>
                <defaultLibBundleDir>lib</defaultLibBundleDir>
                <modules>
                    <webModule>
                        <groupId>com.foo</groupId>
                        <artifactId>war</artifactId>
                    </webModule>
                </modules>
            </configuration>
        </plugin>
    </plugins>
</build>

Now, every WAR will be packaged independently with it's own dependencies and the EAR will be packaged with skinny WARs and every dependency inside the lib folder

Update:

Keep in mind, that the ear/lib folder can't be used for every dependency jar in a strict Container like JBoss EAP 6. JSF Component libraries like tomahawk, primefaces, etc. have to reside in WEB-INF/lib folder.

A handy way to achieve this with the above described solution is to make an exclusion for the component library in the EARs pom.xml like this:

...
<dependencies>
    <dependency>
        <groupId>com.foo</groupId>
        <artifactId>war</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <type>war</type>
    </dependency>
    <dependency>
        <groupId>com.foo</groupId>
        <artifactId>war</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <type>pom</type>
        <exclusions>
            <exclusion>
                <groupId>org.primefaces</groupId>
                <artifactId>primefaces</artifactId>
            <exclusion>
        </exclusions>
    </dependency>
</dependencies>
...

Now every dependency of the WAR will be placed in ear/lib except the component library which will be placed in WEB-INF/lib inside the WAR

9
votes

Create a new artifact named commons-jars and package it as pom. It should depend on all the common jars you are using - Spring, Hibernate, Log4j, etc.

Then, in each on your wars add it as dependency with scope "provided" (and don't forget to set the type as pom). You will be able to see it in your classpath but they won't be packaged into the war. This way you can also have war specific dependencies packaged into it, which the solution from skinny wars does not provide.

4
votes

You can set the dependancies scope to "provided". This means they will be provided by some other module and will not be included in the final jar or war.

Perhaps the assembly plugin can help you when packaging up the final EAR and place common jars there.