Search code examples
mavenjarwarear

JAR library in WAR module not seen


I have Maven project with WAR and EAR module. EAR module has WAR as a dependency and is included as a war module in maven-ear-plugin. There's also JAR library added in EAR module and is used by WAR module.

EAR module pom.xml:

<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.example</groupId>
        <artifactId>MyApp</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>MyEar</artifactId>
    <packaging>ear</packaging>

    <dependencies>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>MyWar</artifactId>
                <version>0.0.1-SNAPSHOT</version>
                <type>war</type>
            </dependency>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>MyJar</artifactId>
            </dependency>
        </dependencies>

        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-ear-plugin</artifactId>
                    <version>2.9</version>
                    <configuration>
                        <modules>
                            <jarModule>
                                <groupId>com.example</groupId>
                                <artifactId>MyJar</artifactId>
                            </jarModule>
                            <webModule>
                                <groupId>com.example</groupId>
                                <artifactId>MyWar</artifactId>
                                <contextRoot>/MyWebApp</contextRoot>
                            </webModule>
                        </modules>
                        <version>6</version>
                    </configuration>
                </plugin>
            </plugins>
        </build>

</project>

And WAR module pom.xml:

<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.example</groupId>
        <artifactId>MyApp</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>MyWar</artifactId>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>MyJar</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

I'm trying to build EAR that includes MyWar and MyJar in root. The problem is that if I use EAR pom like the one above, MyJar library is not "seen" by MyWar.

What am I missing here?


Solution

  • The only modules that should be in the root of the EAR archive are ejb-jars, wars and rars.

    Utility library jars should be placed in a "lib" directory within the EAR.

    You just need to add one line to your EAR plugin configuration to accomplish this:

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-ear-plugin</artifactId>
                    <version>2.9</version>
                    <configuration>
                        <modules>
                            <jarModule>
                                <groupId>com.example</groupId>
                                <artifactId>MyJar</artifactId>
                            </jarModule>
                            <webModule>
                                <groupId>com.example</groupId>
                                <artifactId>MyWar</artifactId>
                                <contextRoot>/MyWebApp</contextRoot>
                            </webModule>
                        </modules>
                        <version>6</version>
                        <defaultLibBundleDir>lib</defaultLibBundleDir>  <--- this!
                    </configuration>
                </plugin>
    

    If you're going to be using EAR files you should familiarise yourself with the class visibility rules for the various modules within them. This is loosely summarised in My ear is not able to find ejb module classes.

    In general you should not be using EAR files these days. In most cases a simple WAR file will be sufficient