使用Maven构建和部署J2EE应用程序的EAR文件



这篇文章,主要是技术上的整理,用来mark一下,用的时候参考。


一、新建项目

新建一个空的Maven Project项目

使用Maven构建和部署J2EE应用程序的EAR文件_第1张图片


二、放入依赖

注:ear部署时如果里面有entity,会发生错误,所以不要把entity放到依赖项中,部署的时候先部实体,然后再部ear

将需要打入EAR中的jarwardependencies-dependency的形式写到pom.xml文件中,如:



三、配置加载顺序

现在打出来的ear包中的application中只有war的配置信息,而没有jar的配置信息,而我们如果想控制这些jar的配置信息,就必须把jar的信息配置到application.xml中,而application.xml是可以通过此插件生成的,所以在pom.xml文件中配置即可,配置方式如下:

使用Maven构建和部署J2EE应用程序的EAR文件_第2张图片

四、运行

如果现在打包,会报一个没有发现application.xml的错误,这个文件需要我们自己创建么?答案是不需要:

1.       运行ear: generate-application-xml,会生成相应的application.xml文件,下面是我的pom生成的application.xml文件(格式有改动):

使用Maven构建和部署J2EE应用程序的EAR文件_第3张图片


2.       同时,此命令还生成一个jboss-app.xml的文件,里面简单一一句话,是说要JBoss按照application.xml里面的声明顺序加载

使用Maven构建和部署J2EE应用程序的EAR文件_第4张图片


3.       现在可以执行生成ear的命令了:ear:ear,坐等成功吧~~


五、 更改pompackaging

现在如果你直接Install的话,会发现并不是打成的ear,而是打了jar,需要将packaging改为ear即可。

但改后会发现pom上面报错,你在eclipse应用广场搜一下m2e-wtp,然后安装即可。

注:我的完整pom.xml

[html]  view plain copy print ?
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.          <modelVersion>4.0.0</modelVersion>  
  4.          <parent>  
  5.                    <groupId>com.tgb</groupId>  
  6.                    <artifactId>gxpt</artifactId>  
  7.                    <version>0.0.1-SNAPSHOT</version>  
  8.                    <relativePath>../gxpt/pom.xml</relativePath>  
  9.          </parent>  
  10.          <artifactId>gxpt_a_ear</artifactId>  
  11.          <packaging>ear</packaging>  
  12.    
  13.          <dependencies>  
  14.                   <dependency>  
  15.                             <groupId>com.tgb</groupId>  
  16.                             <artifactId>gxpt_entity_qx</artifactId>  
  17.                             <version>0.0.1-SNAPSHOT</version>  
  18.                             <type>jar</type>  
  19.                    </dependency>  
  20.                    <dependency>  
  21.                             <groupId>com.tgb</groupId>  
  22.                             <artifactId>gxpt_common_tool</artifactId>  
  23.                             <version>0.0.1-SNAPSHOT</version>  
  24.                             <type>jar</type>  
  25.                    </dependency>  
  26.                    <dependency>  
  27.                             <groupId>com.tgb</groupId>  
  28.                             <artifactId>gxpt_common_eao</artifactId>  
  29.                             <version>0.0.1-SNAPSHOT</version>  
  30.                             <type>jar</type>  
  31.                    </dependency>  
  32.                    <dependency>  
  33.                             <groupId>com.tgb</groupId>  
  34.                             <artifactId>gxpt_common_eao_impl</artifactId>  
  35.                             <version>0.0.1-SNAPSHOT</version>  
  36.                             <type>jar</type>  
  37.                    </dependency>  
  38.                    <dependency>  
  39.                             <groupId>com.tgb</groupId>  
  40.                             <artifactId>gxpt_mgr_qx_module</artifactId>  
  41.                             <version>0.0.1-SNAPSHOT</version>  
  42.                             <type>jar</type>  
  43.                    </dependency>  
  44.                    <dependency>  
  45.                             <groupId>com.tgb</groupId>  
  46.                             <artifactId>gxpt_mgr_qx_module_impl</artifactId>  
  47.                             <version>0.0.1-SNAPSHOT</version>  
  48.                             <type>jar</type>  
  49.                    </dependency>  
  50.                    <dependency>  
  51.                             <groupId>com.tgb</groupId>  
  52.                             <artifactId>gxpt_web_qx_module</artifactId>  
  53.                             <version>0.0.1-SNAPSHOT</version>  
  54.                             <type>war</type>  
  55.                    </dependency>  
  56.          </dependencies>  
  57.    
  58.          <build>  
  59.                    <plugins>  
  60.                             <plugin>  
  61.                                      <artifactId>maven-ear-plugin</artifactId>  
  62.                                      <version>2.9</version>  
  63.                                      <configuration>  
  64.                                               <packagingIncludes>META-INF/**,**/gxpt_*.jar,**/gxpt_*.war</packagingIncludes>  
  65.                                                <jboss>  
  66.                                                         <version>5</version>  
  67.                                                         <module-order>strict</module-order>  
  68.                                                </jboss>  
  69.                                                <modules>  
  70.                                                         <jarModule>  
  71.                                                                  <groupId>com.tgb</groupId>  
  72.                                                                  <artifactId>gxpt_entity_qx</artifactId>  
  73.                                                                  <includeInApplicationXml>true</includeInApplicationXml>  
  74.                                                         </jarModule>  
  75.                                                         <jarModule>  
  76.                                                                  <groupId>com.tgb</groupId>  
  77.                                                                  <artifactId>gxpt_common_tool</artifactId>  
  78.                                                                  <includeInApplicationXml>true</includeInApplicationXml>  
  79.                                                         </jarModule>  
  80.                                                         <jarModule>  
  81.                                                                  <groupId>com.tgb</groupId>  
  82.                                                                  <artifactId>gxpt_common_eao</artifactId>  
  83.                                                                  <includeInApplicationXml>true</includeInApplicationXml>  
  84.                                                         </jarModule>  
  85.                                                         <jarModule>  
  86.                                                                  <groupId>com.tgb</groupId>  
  87.                                                                  <artifactId>gxpt_common_eao_impl</artifactId>  
  88.                                                                  <includeInApplicationXml>true</includeInApplicationXml>  
  89.                                                         </jarModule>  
  90.                                                         <jarModule>  
  91.                                                                  <groupId>com.tgb</groupId>  
  92.                                                                  <artifactId>gxpt_mgr_qx_module</artifactId>  
  93.                                                                  <includeInApplicationXml>true</includeInApplicationXml>  
  94.                                                         </jarModule>  
  95.                                                         <jarModule>  
  96.                                                                  <groupId>com.tgb</groupId>  
  97.                                                                  <artifactId>gxpt_mgr_qx_module_impl</artifactId>  
  98.                                                                  <includeInApplicationXml>true</includeInApplicationXml>  
  99.                                                         </jarModule>  
  100.                                                         <webModule>  
  101.                                                                  <groupId>com.tgb</groupId>  
  102.                                                                  <artifactId>gxpt_web_qx_module</artifactId>  
  103.                                                                  <includeInApplicationXml>true</includeInApplicationXml>  
  104.                                                         </webModule>  
  105.                                                </modules>  
  106.                                      </configuration>  
  107.                             </plugin>  
  108.                    </plugins>  
  109.          </build>  
  110. </project>  

六、部署到JBoss

1、首先确认你连接的仓库,默认的中央仓库上面是没有JBoss的包的,需要配置一下开源中国的私服库。详细请查看:http://maven.oschina.net/help.html,官方给的配置很详细。

2、配置Cargo

[html]  view plain copy print ?
  1. <plugin>  
  2.          <groupId>org.codehaus.cargo</groupId>  
  3.          <artifactId>cargo-maven2-plugin</artifactId>  
  4.          <version>1.4.5</version>  
  5.          <configuration>  
  6.                    <container>  
  7.                             <containerId>jboss51x</containerId>  
  8.                             <type>remote</type>  
  9.                    </container>  
  10.                    <configuration>  
  11.                             <type>runtime</type>  
  12.                             <properties>  
  13.                                      <cargo.remote.username>admin</cargo.remote.username>  
  14.                                      <cargo.remote.password>admin</cargo.remote.password>  
  15.                                      <cargo.hostname>192.168.24.48</cargo.hostname>  
  16.                                      <cargo.rmi.port>1099</cargo.rmi.port>  
  17.                             </properties>  
  18.                    </configuration>  
  19.          </configuration>  
  20.          <dependencies>  
  21.                    <dependency>  
  22.                             <groupId>org.jboss.integration</groupId>  
  23.                             <artifactId>jboss-profileservice-spi</artifactId>  
  24.                             <version>5.1.0.GA</version>  
  25.                    </dependency>  
  26.                    <dependency>  
  27.                             <groupId>org.jboss.jbossas</groupId>  
  28.                             <artifactId>jboss-as-client</artifactId>  
  29.                             <version>5.1.0.GA</version>  
  30.                             <type>pom</type>  
  31.                    </dependency>  
  32.          </dependencies>  
  33. </plugin>  

3、将此项目执行clean install后,然后就可以执行cargo:deploy了,通过此命令就可以将项目部署到远程JBoss中了。


需要注意的一点,如果用到了spring,通过jndi查找ejb,那么

由于EJBEAR中被部署后,绑定的JNDI前面会加上EAR的包名,如:

之前的CommonEao部署之后的JNDI名字为:common_EaoBean/remote,而现在会变成:gxpt_a_ear/commonEaoBean/remote


七、总结:

做j2ee企业级开发,构建EAR是一项基本功。EAR只是一种技术规范,根据不同的项目环境,会有不同的用法,在我们的项目中,适当的时候,会采用EAR作为作为组件的部署单位,让EAR包含一组EJB颗粒,起到分类的作用,同时也用于打包与组件相关联的辅助资源。配合Jenkins自动部署,使用maven cargo插件非常便利的就可以将项目部署到远程的jboss中。

这篇文章,主要是技术上的整理,用来mark一下,用的时候参考。


一、新建项目

新建一个空的Maven Project项目

使用Maven构建和部署J2EE应用程序的EAR文件_第5张图片


二、放入依赖

注:ear部署时如果里面有entity,会发生错误,所以不要把entity放到依赖项中,部署的时候先部实体,然后再部ear

将需要打入EAR中的jarwardependencies-dependency的形式写到pom.xml文件中,如:



三、配置加载顺序

现在打出来的ear包中的application中只有war的配置信息,而没有jar的配置信息,而我们如果想控制这些jar的配置信息,就必须把jar的信息配置到application.xml中,而application.xml是可以通过此插件生成的,所以在pom.xml文件中配置即可,配置方式如下:

使用Maven构建和部署J2EE应用程序的EAR文件_第6张图片

四、运行

如果现在打包,会报一个没有发现application.xml的错误,这个文件需要我们自己创建么?答案是不需要:

1.       运行ear: generate-application-xml,会生成相应的application.xml文件,下面是我的pom生成的application.xml文件(格式有改动):

使用Maven构建和部署J2EE应用程序的EAR文件_第7张图片


2.       同时,此命令还生成一个jboss-app.xml的文件,里面简单一一句话,是说要JBoss按照application.xml里面的声明顺序加载

使用Maven构建和部署J2EE应用程序的EAR文件_第8张图片


3.       现在可以执行生成ear的命令了:ear:ear,坐等成功吧~~


五、 更改pompackaging

现在如果你直接Install的话,会发现并不是打成的ear,而是打了jar,需要将packaging改为ear即可。

但改后会发现pom上面报错,你在eclipse应用广场搜一下m2e-wtp,然后安装即可。

注:我的完整pom.xml

[html]  view plain copy print ?
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.          <modelVersion>4.0.0</modelVersion>  
  4.          <parent>  
  5.                    <groupId>com.tgb</groupId>  
  6.                    <artifactId>gxpt</artifactId>  
  7.                    <version>0.0.1-SNAPSHOT</version>  
  8.                    <relativePath>../gxpt/pom.xml</relativePath>  
  9.          </parent>  
  10.          <artifactId>gxpt_a_ear</artifactId>  
  11.          <packaging>ear</packaging>  
  12.    
  13.          <dependencies>  
  14.                   <dependency>  
  15.                             <groupId>com.tgb</groupId>  
  16.                             <artifactId>gxpt_entity_qx</artifactId>  
  17.                             <version>0.0.1-SNAPSHOT</version>  
  18.                             <type>jar</type>  
  19.                    </dependency>  
  20.                    <dependency>  
  21.                             <groupId>com.tgb</groupId>  
  22.                             <artifactId>gxpt_common_tool</artifactId>  
  23.                             <version>0.0.1-SNAPSHOT</version>  
  24.                             <type>jar</type>  
  25.                    </dependency>  
  26.                    <dependency>  
  27.                             <groupId>com.tgb</groupId>  
  28.                             <artifactId>gxpt_common_eao</artifactId>  
  29.                             <version>0.0.1-SNAPSHOT</version>  
  30.                             <type>jar</type>  
  31.                    </dependency>  
  32.                    <dependency>  
  33.                             <groupId>com.tgb</groupId>  
  34.                             <artifactId>gxpt_common_eao_impl</artifactId>  
  35.                             <version>0.0.1-SNAPSHOT</version>  
  36.                             <type>jar</type>  
  37.                    </dependency>  
  38.                    <dependency>  
  39.                             <groupId>com.tgb</groupId>  
  40.                             <artifactId>gxpt_mgr_qx_module</artifactId>  
  41.                             <version>0.0.1-SNAPSHOT</version>  
  42.                             <type>jar</type>  
  43.                    </dependency>  
  44.                    <dependency>  
  45.                             <groupId>com.tgb</groupId>  
  46.                             <artifactId>gxpt_mgr_qx_module_impl</artifactId>  
  47.                             <version>0.0.1-SNAPSHOT</version>  
  48.                             <type>jar</type>  
  49.                    </dependency>  
  50.                    <dependency>  
  51.                             <groupId>com.tgb</groupId>  
  52.                             <artifactId>gxpt_web_qx_module</artifactId>  
  53.                             <version>0.0.1-SNAPSHOT</version>  
  54.                             <type>war</type>  
  55.                    </dependency>  
  56.          </dependencies>  
  57.    
  58.          <build>  
  59.                    <plugins>  
  60.                             <plugin>  
  61.                                      <artifactId>maven-ear-plugin</artifactId>  
  62.                                      <version>2.9</version>  
  63.                                      <configuration>  
  64.                                               <packagingIncludes>META-INF/**,**/gxpt_*.jar,**/gxpt_*.war</packagingIncludes>  
  65.                                                <jboss>  
  66.                                                         <version>5</version>  
  67.                                                         <module-order>strict</module-order>  
  68.                                                </jboss>  
  69.                                                <modules>  
  70.                                                         <jarModule>  
  71.                                                                  <groupId>com.tgb</groupId>  
  72.                                                                  <artifactId>gxpt_entity_qx</artifactId>  
  73.                                                                  <includeInApplicationXml>true</includeInApplicationXml>  
  74.                                                         </jarModule>  
  75.                                                         <jarModule>  
  76.                                                                  <groupId>com.tgb</groupId>  
  77.                                                                  <artifactId>gxpt_common_tool</artifactId>  
  78.                                                                  <includeInApplicationXml>true</includeInApplicationXml>  
  79.                                                         </jarModule>  
  80.                                                         <jarModule>  
  81.                                                                  <groupId>com.tgb</groupId>  
  82.                                                                  <artifactId>gxpt_common_eao</artifactId>  
  83.                                                                  <includeInApplicationXml>true</includeInApplicationXml>  
  84.                                                         </jarModule>  
  85.                                                         <jarModule>  
  86.                                                                  <groupId>com.tgb</groupId>  
  87.                                                                  <artifactId>gxpt_common_eao_impl</artifactId>  
  88.                                                                  <includeInApplicationXml>true</includeInApplicationXml>  
  89.                                                         </jarModule>  
  90.                                                         <jarModule>  
  91.                                                                  <groupId>com.tgb</groupId>  
  92.                                                                  <artifactId>gxpt_mgr_qx_module</artifactId>  
  93.                                                                  <includeInApplicationXml>true</includeInApplicationXml>  
  94.                                                         </jarModule>  
  95.                                                         <jarModule>  
  96.                                                                  <groupId>com.tgb</groupId>  
  97.                                                                  <artifactId>gxpt_mgr_qx_module_impl</artifactId>  
  98.                                                                  <includeInApplicationXml>true</includeInApplicationXml>  
  99.                                                         </jarModule>  
  100.                                                         <webModule>  
  101.                                                                  <groupId>com.tgb</groupId>  
  102.                                                                  <artifactId>gxpt_web_qx_module</artifactId>  
  103.                                                                  <includeInApplicationXml>true</includeInApplicationXml>  
  104.                                                         </webModule>  
  105.                                                </modules>  
  106.                                      </configuration>  
  107.                             </plugin>  
  108.                    </plugins>  
  109.          </build>  
  110. </project>  

六、部署到JBoss

1、首先确认你连接的仓库,默认的中央仓库上面是没有JBoss的包的,需要配置一下开源中国的私服库。详细请查看:http://maven.oschina.net/help.html,官方给的配置很详细。

2、配置Cargo

[html]  view plain copy print ?
  1. <plugin>  
  2.          <groupId>org.codehaus.cargo</groupId>  
  3.          <artifactId>cargo-maven2-plugin</artifactId>  
  4.          <version>1.4.5</version>  
  5.          <configuration>  
  6.                    <container>  
  7.                             <containerId>jboss51x</containerId>  
  8.                             <type>remote</type>  
  9.                    </container>  
  10.                    <configuration>  
  11.                             <type>runtime</type>  
  12.                             <properties>  
  13.                                      <cargo.remote.username>admin</cargo.remote.username>  
  14.                                      <cargo.remote.password>admin</cargo.remote.password>  
  15.                                      <cargo.hostname>192.168.24.48</cargo.hostname>  
  16.                                      <cargo.rmi.port>1099</cargo.rmi.port>  
  17.                             </properties>  
  18.                    </configuration>  
  19.          </configuration>  
  20.          <dependencies>  
  21.                    <dependency>  
  22.                             <groupId>org.jboss.integration</groupId>  
  23.                             <artifactId>jboss-profileservice-spi</artifactId>  
  24.                             <version>5.1.0.GA</version>  
  25.                    </dependency>  
  26.                    <dependency>  
  27.                             <groupId>org.jboss.jbossas</groupId>  
  28.                             <artifactId>jboss-as-client</artifactId>  
  29.                             <version>5.1.0.GA</version>  
  30.                             <type>pom</type>  
  31.                    </dependency>  
  32.          </dependencies>  
  33. </plugin>  

3、将此项目执行clean install后,然后就可以执行cargo:deploy了,通过此命令就可以将项目部署到远程JBoss中了。


需要注意的一点,如果用到了spring,通过jndi查找ejb,那么

由于EJBEAR中被部署后,绑定的JNDI前面会加上EAR的包名,如:

之前的CommonEao部署之后的JNDI名字为:common_EaoBean/remote,而现在会变成:gxpt_a_ear/commonEaoBean/remote


七、总结:

做j2ee企业级开发,构建EAR是一项基本功。EAR只是一种技术规范,根据不同的项目环境,会有不同的用法,在我们的项目中,适当的时候,会采用EAR作为作为组件的部署单位,让EAR包含一组EJB颗粒,起到分类的作用,同时也用于打包与组件相关联的辅助资源。配合Jenkins自动部署,使用maven cargo插件非常便利的就可以将项目部署到远程的jboss中。

你可能感兴趣的:(使用Maven构建和部署J2EE应用程序的EAR文件)