如何使用Maven使用EJB和WAR构建EAR项目?

时间:2015-10-07 11:10:15

标签: maven java-ee ear

我尝试使用EJB和WAR创建EAR项目,但我遇到了一些问题。 我从Java EE 6 EAR Archetype

创建了主项目
<dependency>
    <groupId>org.codehaus.mojo.archetypes</groupId>
    <artifactId>ear-javaee6</artifactId>
    <version>1.5</version>
</dependency>

然后我从Java EE 6 EJB JAR Archetype创建了EJB模块:

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

然后我从Javax Faces WAR Archetype创建了第二个模块:

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

然后我将依赖项添加到主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>

然后我尝试构建项目 - 使用maven clean和instal。 但我有一个错误:

  [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

在EAR pom.xml我有:

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

但如果我将<packaging>pom</packaging>更改为<packaging>ear</packaging> 弹出窗口中的IDEA显示错误:

  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

2 个答案:

答案 0 :(得分:10)

我强烈建议您了解多模块构建的工作原理。 Sonatype的书非常详细地描述了chapter

要使用EJB和WAR构建EAR,实际上需要三个模块,用于EJB,WAR和EAR。父POM只是把所有东西放在一起,包装类型为POM。

所以父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>

    <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>

然后,每个子POM都是这样的:

EJB模块/ 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>

战模块/ 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>

耳模块/ 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>

答案 1 :(得分:0)

> 1. parent  QCforCC-parent : 

    <artifactId>QCforCC-parent</artifactId>
    <packaging>pom</packaging>
    ...
    ...
    <modules>
        <module>QCforCC-ear</module>
        <module>QCforCC-ejb</module>
        <module>QCforCC-war</module>
    </modules>

> 2. QCforCC-ear :

<artifactId>QCforCC-ear</artifactId>
<packaging>ear</packaging>
...
...
 <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-ear-plugin</artifactId>
        <configuration>
          <version>5</version>
          <displayName>XXXXXXXX</displayName>  
          <modules>
             <webModule>             
                <moduleId>WebModule_XXX</moduleId>
                <groupId>${project.groupId}</groupId>
                <artifactId>>QCforCC-war</artifactId>   
               <contextRoot>XXXXXXXX</contextRoot>
             </webModule>  
                <jarModule>
                            <groupId>${project.groupId}</groupId>
                            <artifactId>QCforCC-ejb</artifactId>
                 </jarModule>            
          </modules>  
        </configuration>
      </plugin>
    </plugins>