How to build an EAR project with EJB and WAR using Maven?

Pavel picture Pavel · Oct 7, 2015 · Viewed 16.1k times · Source

I tried to create EAR Project with EJB and WAR but I have some problem. I created the main project from the Java EE 6 EAR Archetype:

<dependency>
    <groupId>org.codehaus.mojo.archetypes</groupId>
    <artifactId>ear-javaee6</artifactId>
    <version>1.5</version>
</dependency>

Then I created the EJB module from the Java EE 6 EJB JAR Archetype:

<dependency>
    <groupId>org.codehaus.mojo.archetypes</groupId>
    <artifactId>ejb-javaee6</artifactId>
    <version>1.5</version>
</dependency>

And then I created the second module from the Javax Faces WAR Archetype:

<dependency>
    <groupId>javax.faces</groupId>
    <artifactId>javax.faces-war-archetype</artifactId>
    <version>2.2</version>
</dependency>

Then I added dependencies to main pom.xml:

<!-- Define the versions of your ear components here -->
<dependencies>
    <dependency>
        <groupId>QCforCC-main</groupId>
        <artifactId>QCforCC-ejb</artifactId>
        <version>1.0-SNAPSHOT</version>
        <type>ejb</type>
    </dependency>
    <dependency>
        <groupId>QCforCC-main</groupId>
        <artifactId>QCforCC-war</artifactId>
        <version>1.0-SNAPSHOT</version>
        <type>war</type>
    </dependency>
</dependencies>

And then I tried to build the project - using maven clean and instal . But I have an error:

  [ERROR] The projects in the reactor contain a cyclic reference: Edge between 'Vertex{label='QCforCC-main:QCforCC-war:1.0-SNAPSHOT'}' and 'Vertex{label='QCforCC-main:QCforCC-ejb:1.0-SNAPSHOT'}' introduces to cycle in the graph QCforCC-main:QCforCC-ejb:1.0-SNAPSHOT --> QCforCC-main:QCforCC-war:1.0-SNAPSHOT --> QCforCC-main:QCforCC-ejb:1.0-SNAPSHOT -      [Help 1] 
  [ERROR]
  [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. 
  [ERROR] Re-run Maven using the -X switch to enable full debug logging. 
  [ERROR]  
  [ERROR] For more information about the errors and possible solutions, please read the following articles: 
  [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectCycleException

  Process finished with exit code 1

And in EAR pom.xml I have:

<modules>
    <module>QCforCC-ejb</module>
    <module>QCforCC-war</module>
</modules>
<packaging>pom</packaging>

But if I change <packaging>pom</packaging> to <packaging>ear</packaging> IDEA show error in popup:

  Some problems were encountered while processing the POMs: 
  [WARNING] 'build.plugins.plugin.(groupId:artifactId)' must be unique but found duplicate declaration of plugin org.apache.maven.plugins:maven-ear-plugin @ line 41, column 21 
  [ERROR]   'packaging' with value 'ear' is invalid. Aggregator projects require 'pom' as packaging. @ line 12, column 16

Answer

sh0rug0ru picture sh0rug0ru · Oct 7, 2015

I highly suggest that you understand how multi-module builds work. The Sonatype book has a great chapter describing in great detail.

To build an EAR with an EJB and a WAR, you actually need three modules, for the EJB, WAR and EAR. The parent POM just holds everything together and has a packaging type of POM.

So the parent pom.xml should look like this:

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.sonatype.mavenbook.multi</groupId>
    <artifactId>simple-parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0</version>
    <name>Multi Chapter Simple Parent Project</name>

    <modules>
        <module>ejb-module</module>
        <module>war-module</module>
        <module>ear-module</module>
    </modules>
</project>

Then, each of the child POMs would look like this:

ejb-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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.sonatype.mavenbook.multi</groupId>
        <artifactId>simple-parent</artifactId>
      <version>1.0</version>
    </parent>
    <artifactId>ejb-module</artifactId>
    <packaging>ejb</packaging>
</project>

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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.sonatype.mavenbook.multi</groupId>
        <artifactId>simple-parent</artifactId>
        <version>1.0</version>
    </parent>

    <artifactId>war-module</artifactId>
    <packaging>war</packaging>
    <name>simple-webapp Maven Webapp</name>
    <dependencies>
        <dependency>
            <groupId>org.sonatype.mavenbook.multi</groupId>
            <artifactId>ejb-module</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>
</project>

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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.sonatype.mavenbook.multi</groupId>
        <artifactId>simple-parent</artifactId>
        <version>1.0</version>
    </parent>

    <artifactId>ear-module</artifactId>
    <packaging>ear</packaging>
    <name>EAR module</name>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ear-plugin</artifactId>
                <version>2.10.1</version>
                <configuration>
                    <ejbModule>
                        <groupId>org.sonatype.mavenbook.multi</groupId>
                        <artifactId>ejb-module</artifactId>
                        <bundleFilename>ejb-module.jar</bundleFilename>
                    </ejbModule>
                    <webModule>
                        <groupId>org.sonatype.mavenbook.multi</groupId>
                        <artifactId>war-module</artifactId>
                        <contextRoot>/foo</contextRoot>
                    </webModule>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>