如何配置依赖于EJB JAR的WAR,以及创建组合的EAR文件。

3
我是新手,正在学习Maven。我在Eclipse中有两个项目,一个是包含UI内容的retailproducts,另一个是ejb项目retailservice。我为这两个项目编写了单独的pom文件,并能够生成war和jar文件。我的问题如下:
a) 我需要将jar文件配置到war文件中,因为jar文件包含Web应用程序所需的ejb内容 b) 创建war文件后,我需要创建ear文件,其中包含war文件和jar文件。
请问我应该如何操作?
以下是两个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>
  <groupId>RetailProducts</groupId>
  <artifactId>RetailProducts</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>RetailProducts</name>

  <dependencies>

        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.1.7</version>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.1.7</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
        </dependency>
                <!-- Tomcat 6 need this -->
        <dependency>
            <groupId>com.sun.el</groupId>
            <artifactId>el-ri</artifactId>
            <version>1.0</version>
        </dependency>

    </dependencies>

    <build>
        <finalName>JavaServerFaces</finalName>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-war-plugin</artifactId>
      <version>2.1.1</version>
      <!-- <configuration> section added to pick up the WEB-INF/web.xml inside WebContent -->
      <configuration>
         <webResources>
            <resource>
               <directory>WebContent</directory>
            </resource>
         </webResources>
      </configuration>
   </plugin>
        </plugins>
    </build>
</project>


<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>
  <groupId>RetailService</groupId>
  <artifactId>RetailService</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>RetailService</name>
  <dependencies>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.15</version>
        <exclusions>
            <exclusion>
                <groupId>javax.mail</groupId>
                <artifactId>mail</artifactId>
            </exclusion>
            <exclusion>
                <groupId>javax.jms</groupId>
                <artifactId>jms</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.sun.jdmk</groupId>
                <artifactId>jmxtools</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.sun.jmx</groupId>
                <artifactId>jmxri</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.6.10.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
        <version>3.5.6-Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>3.3.0.ga</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>3.6.10.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.0-api</artifactId>
        <version>1.0.1.Final</version>
    </dependency> 
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.5.8</version>
    </dependency>    
    <dependency>
        <groupId>javax.ejb</groupId>
        <artifactId>ejb-api</artifactId>
        <version>3.0</version>
        <scope>provided</scope>
    </dependency>
  </dependencies> 
  <build>
    <sourceDirectory>ejbModule</sourceDirectory>
    <resources>
      <resource>
        <directory>ejbModule</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.0</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

你想要的是将一个项目构建的JAR文件添加到另一个项目中吗? - Francisco Spaeth
那个JAR包含会话Bean和JPA数据持久化文件,我正在使用JSF,其中托管Bean将依赖于EJB层,因为我从托管Bean中调用远程EJB。 - developer
1
好的,你能否将依赖项 <groupId>RetailService</groupId><artifactId>RetailService</artifactId><version>0.0.1-SNAPSHOT</version> 添加到 RetailProducts 项目中呢? - Francisco Spaeth
是的,那是唯一的解决方案。 - developer
你的答案在这里: https://dev59.com/2H_aa4cB1Zd3GeqP0Trs#31790288 - Sri
1个回答

6

当构建包含WAR和EJB模块的Java EE 5.0应用程序时,通常有3个Maven模块来定义:

  1. Web应用程序
  2. EJBs
  3. EAR

您已经拥有前两个,但是没有EAR模块。它应该类似于这样:

<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>Retail</groupId>
    <artifactId>RetailEAR</artifactId>
    <packaging>ear</packaging>
    <version>0.0.1-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ear-plugin</artifactId>
                <version>2.8</version>
                <configuration>
                    <defaultLibBundleDir>lib</defaultLibBundleDir>
                    <skinnyWars>true</skinnyWars>
                    <version>5</version>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>RetailService</groupId>
            <artifactId>RetailService</artifactId>
            <type>ejb</type>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>RetailProducts</groupId>
            <artifactId>RetailProducts</artifactId>
            <type>war</type>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

当您打包EAR模块时,它会处理Web应用程序的依赖项。参数skinnyWars控制是否应在WAR内部复制依赖项。通常不希望将它们放在WAR内部,因为Web应用程序可以从EAR本身访问它们。


谢谢!我没有在EAR项目的pom.xml中包含EJB依赖项,而是将其作为WAR项目的pom.xml中的依赖项包含。这导致EJB无法启动。将EJB依赖项添加回EAR项目的pom.xml后,一切恢复正常。此外,skinnyWars是一个不错的配置。 - asgs
@fnt 你需要在每个模块上执行 "mvn install" 吗? - user3111525
@user3111525 执行它是为了什么? - ᄂ ᄀ
@fnt 如果我在RetailProducts模块上进行了更改,那么在打包我的ear文件之前,我是否需要将其安装到本地仓库中? - user3111525
@user3111525 我不知道你的交付程序。这是关于Maven的通用问题,我认为它与我对原始问题的答案无关。 Maven构建生命周期在http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html 中有详细描述。 - ᄂ ᄀ

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接