maven 开发 ejb ear

基于EJB开发业务简介

在复杂的企业IT架构中 web模块主要用途其实在展现层,它接收到用户的请求后委托后端的 ejb 完成业务逻辑(将业务交由ejb处理,可方便地获得保障业务事务及分布式事务、多客户端调用等好处),然后将ejb的处理结果以可视化形式展现给用户,如此的分层架构可以方便地对ejb业务层实施可复用和分布式。


ejb通常都是jar文件,web通常是war文件,一般都是独立部署,但是,有的时候我们也希望将某些ejb和web打包的一起部署,这就是企业应用ear。


maven 支持 ear 的管理


假设要完成的ear中包行一个ejb和一个web,名称分别为:ejb3和webdemo,两个模块独立开发完成后执行 install 安装到本地maven库,建立一个ear模块(其实就是普通的maven项目,只是设置pom打包类型<packaging>ear</packaging>),如果要开发一个ejb,那么就设置<packaging>ejb</packaging>。


1.开发一个ejb

名称ejb3,其pom大致为:

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"  
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  5.     <parent>  
  6.         <artifactId>eden</artifactId>  
  7.         <groupId>com.conquer.eden</groupId>  
  8.         <version>1.0</version>  
  9.     </parent>  
  10.     <modelVersion>4.0.0</modelVersion>  
  11.   
  12.     <packaging>ejb</packaging>  
  13.   
  14.     <artifactId>ejb3</artifactId>  
  15.   
  16.     <dependencies>  
  17.         <dependency>  
  18.             <groupId>javax.ejb</groupId>  
  19.             <artifactId>ejb-api</artifactId>  
  20.             <version>3.0</version>  
  21.             <scope>provided</scope>  
  22.         </dependency>  
  23.         <!--<dependency>-->  
  24.             <!--<groupId>org.eclipse.persistence</groupId>-->  
  25.             <!--<artifactId>javax.persistence</artifactId>-->  
  26.             <!--<version>2.0.5</version>-->  
  27.             <!--<scope>provided</scope>-->  
  28.         <!--</dependency>-->  
  29.     </dependencies>  
  30.     <build>  
  31.         <finalName>${artifactId}</finalName>  
  32.         <plugins>  
  33.             <plugin>  
  34.                 <!--<groupId>org.apache.maven.plugins</groupId>-->  
  35.                 <artifactId>maven-ejb-plugin</artifactId>  
  36.                 <version>2.5</version>  
  37.                 <configuration>  
  38.                     <ejbVersion>3.0</ejbVersion>  
  39.   
  40.                     <!--<filterDeploymentDescriptor>true</filterDeploymentDescriptor>-->  
  41.   
  42.                     <!-- this is false by default -->  
  43.                     <generateClient>true</generateClient>  
  44.                     <!--用下面的方式进行 客户端生成定制-->  
  45.                     <clientIncludes>  
  46.                         <!-- 包含 test 下面的所有文件和文件夹 -->  
  47.                         <!--<clientInclude>test/**</clientInclude>-->  
  48.                         <!-- 包含 com/example 下面所有的文件,PS:不含文件夹-->  
  49.                         <!--<clientInclude>com/example/*</clientInclude>-->  
  50.                     </clientIncludes>  
  51.                     <clientExcludes>  
  52.                         <!-- 不包含 com/example 下的所有文件-->  
  53.                         <!--<clientExclude>com/example/*</clientExclude>-->  
  54.                         <!-- this will exclude all files and directories with the name sparrow under com/jack -->  
  55.                         <!--<clientExclude>com/jack/**/sparrow</clientExclude>-->  
  56.                     </clientExcludes>  
  57.                 </configuration>  
  58.             </plugin>  
  59.         </plugins>  
  60.     </build>  
  61. </project>  

以上的pom文件需要注意的点:

1.打包类型:

<packaging>ejb</packaging>
2.EJB插件

<artifactId>maven-ejb-plugin</artifactId>
3.是否生成EJB客户端jar

<generateClient>true</generateClient>

关于 <generateClient> 的介绍:

<generateClient>用于生成简单版的ejb客户端,通常只包含客户端使用到的接口而不包含实现类,

他的默认排除规则是:

  • **/*Bean.class
  • **/*CMP.class
  • **/*Session.class
  • **/package.html
所以加入我们的实现类是以 Bean 结尾的话,在打包成的*-client.jar中会自动去除,

当然,该规则也可以定制,可以定制需要添加和排除的文件,如下:

[html]  view plain copy
  1. <configuration>  
  2.   <clientIncludes>  
  3.      <!-- this will include all files and directories under com/foo/bar -->  
  4.      <clientInclude>com/foo/bar/**</clientInclude>  
  5.      <!-- this will include all files and directories under com/foo/acme -->  
  6.      <clientInclude>com/foo/acme/**</clientInclude>  
  7.      <!-- this will include all files under com/example -->  
  8.      <clientInclude>com/example/*</clientInclude>  
  9.   </clientIncludes>  
  10.   <clientExcludes>  
  11.      <!-- this will exclude all files under com/example -->  
  12.      <clientExclude>com/example/*</clientExclude>  
  13.      <!-- this will exclude all files and directories with the name  
  14.           sparrow under com/jack -->  
  15.      <clientExclude>com/jack/**/sparrow</clientExclude>  
  16.   </clientExcludes>  
  17. </configuration>  

最后:在模块中添加此client的依赖时候,其类型要指定为ejb-client,如

<dependency>
    <groupId>com.conquer.eden</groupId>
    <artifactId>xaejb</artifactId>
    <version>1.0</version>
    <!--<classifier>client</classifier>-->
    <type>ejb-client</type>
</dependency>

官网参考:

http://maven.apache.org/plugins/maven-ejb-plugin/

http://maven.apache.org/plugins/maven-ejb-plugin/examples/generating-ejb-client.html

ejb的java代码:

[java]  view plain copy
  1. public interface TestDAOEJB {  
  2.     String insertData(String data) throws Exception;  
  3.     String insertData2(String data) throws Exception;  
  4.     String insertData3(String data) throws Exception;  
  5. }  
[java]  view plain copy
  1. import javax.ejb.*;  
  2. import javax.naming.Context;  
  3. import javax.naming.InitialContext;  
  4. import javax.sql.DataSource;  
  5. import java.io.IOException;  
  6. import java.sql.Connection;  
  7. import java.sql.Statement;  
  8. import java.util.Properties;  
  9.   
  10. //@Stateless  
  11. //@Stateless(mappedName = "TestDAOBean")  
  12. @Stateful  
  13. @Remote(value = TestDAOEJB.class)  
  14. public class TestDAOBean implements TestDAOEJB {  
  15.   
  16.     private static String otherNode1;  
  17.     private static String otherNode2;  
  18.     static {  
  19.         try {  
  20.             Properties properties = new Properties();  
  21.             properties.load(TestDAOBean.class.getResourceAsStream("/config.properties"));  
  22.             otherNode1 = properties.getProperty("otherNode1");  
  23.             otherNode2 = properties.getProperty("otherNode2");  
  24.         } catch (IOException e) {  
  25.             e.printStackTrace();  
  26.         }  
  27.     }  
  28.   
  29.     /** 
  30.      * CREATE TABLE wangpl(name VARCHAR2(200)) 
  31.      */  
  32.   
  33.     @Override  
  34.     @TransactionAttribute(TransactionAttributeType.REQUIRED)//这是默认值,可以省略,或修改为其它事务策略  
  35.     public String insertData(String data) throws Exception {  
  36.         String returnString = "insertData-OK";  
  37.         Connection con = null;  
  38.         Statement statement = null;  
  39.         try {  
  40.             DataSource dataSource = (DataSource) new InitialContext().lookup("DataSource1");  
  41.             con = dataSource.getConnection();  
  42.             statement = con.createStatement();  
  43.             String sql = "INSERT INTO wangpl VALUES('" + data + ":insertData')";  
  44.             statement.execute(sql);  
  45.         } finally {  
  46.             // close xxx  
  47.         }  
  48.   
  49.         // 调用远程节点  
  50.         Properties p = new Properties();  
  51.         p.put(Context.INITIAL_CONTEXT_FACTORY, "com.xxx.RemoteInitialContextFactory");  
  52.         p.put(Context.PROVIDER_URL, otherNode1);  
  53.         InitialContext ctx = new InitialContext(p);  
  54.         TestDAOEJB remote = (TestDAOEJB) ctx.lookup("TestDAOBeanRemote");  
  55.         remote.insertData2(data + "1");  
  56.   
  57.         return returnString;  
  58.     }  
  59.     @Override  
  60.     @TransactionAttribute(TransactionAttributeType.SUPPORTS)  
  61.     public String insertData2(String data) throws Exception {  
  62.         String returnString = "insertData2-OK";  
  63.         Connection con = null;  
  64.         Statement statement = null;  
  65.         try {  
  66.             DataSource dataSource = (DataSource) new InitialContext().lookup("DataSource1");  
  67.             con = dataSource.getConnection();  
  68.             statement = con.createStatement();  
  69.             String sql = "INSERT INTO wangpl VALUES('" + data + ":insertData2')";  
  70.             statement.execute(sql);  
  71.         } finally {  
  72.             // close xxx  
  73.         }  
  74.   
  75.         // 调用远程ejb  
  76.         Properties p = new Properties();  
  77.         p.put(Context.INITIAL_CONTEXT_FACTORY, "com.xxx.RemoteInitialContextFactory");  
  78.         p.put(Context.PROVIDER_URL, otherNode2);  
  79.         InitialContext ctx = new InitialContext(p);  
  80.         TestDAOEJB remote = (TestDAOEJB) ctx.lookup("TestDAOBeanRemote");  
  81.         remote.insertData3(data + "1");  
  82.   
  83.         if (data.equals("11")) {  
  84.             throw new RuntimeException("************* Runtime Exception **************");  
  85.         }  
  86.         return returnString;  
  87.     }  
  88.     @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)  
  89.     public String insertData3(String data) throws Exception {  
  90.         String returnString = "insertData3-OK";  
  91.         Connection con = null;  
  92.         Statement statement = null;  
  93.         try {  
  94.             DataSource dataSource = (DataSource) new InitialContext().lookup("DataSource1");  
  95.             con = dataSource.getConnection();  
  96.             statement = con.createStatement();  
  97.             String sql = "INSERT INTO wangpl VALUES('" + data + ":insertData3')";  
  98.             statement.execute(sql);  
  99.         } finally {  
  100.             // close xxx  
  101.         }  
  102.   
  103.         if (data.equals("211")) {  
  104.             throw new RuntimeException("************* Runtime Exception **************");  
  105.         }  
  106.   
  107.         return returnString;  
  108.     }  
  109. }  

2.开发web模块

其pom大致如下:

[html]  view plain copy
  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/maven-v4_0_0.xsd">  
  3.     <parent>  
  4.         <artifactId>eden</artifactId>  
  5.         <groupId>com.conquer.eden</groupId>  
  6.         <version>1.0</version>  
  7.     </parent>  
  8.     <modelVersion>4.0.0</modelVersion>  
  9.     <artifactId>webdemo</artifactId>  
  10.     <packaging>war</packaging>  
  11.     <name>webdemo</name>  
  12.     <build>  
  13.         <finalName>${artifactId}</finalName>  
  14.     </build>  
  15.     <dependencies>  
  16.         <dependency>  
  17.             <groupId>javax.servlet</groupId>  
  18.             <artifactId>javax.servlet-api</artifactId>  
  19.             <version>3.1.0</version>  
  20.             <scope>provided</scope>  
  21.         </dependency>  
  22.         <dependency>  
  23.             <groupId>com.conquer.eden</groupId>  
  24.             <artifactId>ejb3</artifactId>  
  25.             <version>1.0</version>  
  26.             <!--<classifier>client</classifier>-->  
  27.             <type>ejb-client</type>  
  28.          </dependency>  
  29.     </dependencies>  
  30. </project>  
以上pom,要注意的是添加ejb的client依赖,指明类型是: <type>ejb-client</type>

web层业务代码 ,可以使用 @EJB  @Resource 的注解方式,或者手工: new InitialContext(xx)来访问ejb。


3.开发ear

其pom文件为:

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"  
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  5.     <parent>  
  6.         <artifactId>eden</artifactId>  
  7.         <groupId>com.conquer.eden</groupId>  
  8.         <version>1.0</version>  
  9.     </parent>  
  10.     <modelVersion>4.0.0</modelVersion>  
  11.     <packaging>ear</packaging>  
  12.   
  13.     <artifactId>ear</artifactId>  
  14.   
  15.     <dependencies>  
  16.         <dependency>  
  17.             <groupId>com.conquer.eden</groupId>  
  18.             <artifactId>ejb3</artifactId>  
  19.             <version>1.0</version>  
  20.             <type>ejb</type>  
  21.         </dependency>  
  22.         <dependency>  
  23.             <groupId>com.conquer.eden</groupId>  
  24.             <artifactId>webdemo</artifactId>  
  25.             <version>1.0</version>  
  26.             <type>war</type>  
  27.         </dependency>  
  28.     </dependencies>  
  29.   
  30.     <build>  
  31.         <finalName>${artifactId}</finalName>  
  32.         <plugins>  
  33.             <plugin>  
  34.                 <artifactId>maven-ear-plugin</artifactId>  
  35.                 <version>2.10</version>  
  36.                 <configuration>  
  37.                     <!--<generateModuleId>true</generateModuleId>-->  
  38.   
  39.                     <!--指定打包范围,避免无用jar混乱加入-->  
  40.                     <!--<packagingIncludes>META-INF/**,**/acme-*.jar,**/acme-*.war</packagingIncludes>-->  
  41.                     <!--<packagingExcludes></packagingExcludes>-->  
  42.   
  43.                     <!--打包指定目录lib-->  
  44.                     <defaultLibBundleDir>lib/</defaultLibBundleDir>  
  45.                     <!--将多个war的共享包提取到父级别-->  
  46.                     <skinnyWars>true</skinnyWars>  
  47.   
  48.                     <!--<includeLibInApplicationXml>true</includeLibInApplicationXml>-->  
  49.   
  50.                     <!--jboss 定制-->  
  51.                     <!--<jboss>-->  
  52.                         <!--<version>5</version>-->  
  53.                         <!--<module-order>strict</module-order>-->  
  54.                         <!--<unauthenticated-principal>guest</unauthenticated-principal>-->  
  55.                         <!--<loader-repository>com.foo:loader=foo-application-1.0.ear</loader-repository>-->  
  56.                     <!--</jboss>-->  
  57.   
  58.                     <modules>  
  59.   
  60.                         <!--jarModule:用于添加第三方库,配合includeInApplicationXml,可使其像一个开发的模块-->  
  61.                         <jarModule>  
  62.                             <!--<groupId>com.conquer.eden</groupId>-->  
  63.                             <!--<artifactId>ejb3</artifactId>-->  
  64.                             <!--<bundleFileName>ejb3.jar</bundleFileName>-->  
  65.   
  66.                             <!--<includeInApplicationXml>true</includeInApplicationXml>-->  
  67.   
  68.                             <!--<bundleDir>APP-INF/lib</bundleDir> 只更改位置,名称保持不变-->  
  69.                             <!--<uri>APP-INF/lib/anotherName-1.2.3.jar</uri> 可定制位置和名称,需要指定名称-->  
  70.                             <!--<excluded>true</excluded>-->  
  71.   
  72.                             <!--<moduleId>ejb3-id</moduleId>-->  
  73.                         </jarModule>  
  74.                         <ejbModule>  
  75.                             <groupId>com.conquer.eden</groupId>  
  76.                             <artifactId>ejb3</artifactId>  
  77.                             <bundleFileName>ejb3.jar</bundleFileName>  
  78.                         </ejbModule>  
  79.                         <webModule>  
  80.                             <groupId>com.conquer.eden</groupId>  
  81.                             <artifactId>webdemo</artifactId>  
  82.                             <bundleFileName>webdemo.war</bundleFileName>  
  83.   
  84.                             <!--<moduleId>webdemo-id</moduleId>-->  
  85.                         </webModule>  
  86.                     </modules>  
  87.                     <!--<security>-->  
  88.                         <!--<security-role id="SecurityRole_1234">-->  
  89.                             <!--<role-name>manager</role-name>-->  
  90.                         <!--</security-role>-->  
  91.                         <!--<security-role id="SecurityRole_5678">-->  
  92.                             <!--<description>My cool description</description>-->  
  93.                             <!--<role-name id="RoleName_12">teller</role-name>-->  
  94.                         <!--</security-role>-->  
  95.                     <!--</security>-->  
  96.   
  97.                     <!--<version>6</version>-->  
  98.                     <!--<env-entries>-->  
  99.                         <!--<env-entry>-->  
  100.                             <!--<description>A complete entry.</description>-->  
  101.                             <!--<env-entry-name>complete</env-entry-name>-->  
  102.                             <!--<env-entry-type>java.lang.Integer</env-entry-type>-->  
  103.                             <!--<env-entry-value>4</env-entry-value>-->  
  104.                         <!--</env-entry>-->  
  105.                         <!--<env-entry>-->  
  106.                             <!--<env-entry-name>no-type</env-entry-name>-->  
  107.                             <!--<env-entry-value>4</env-entry-value>-->  
  108.                         <!--</env-entry>-->  
  109.                         <!--<env-entry>-->  
  110.                             <!--<env-entry-name>no-value</env-entry-name>-->  
  111.                             <!--<env-entry-type>java.lang.String</env-entry-type>-->  
  112.                         <!--</env-entry>-->  
  113.                     <!--</env-entries>-->  
  114.   
  115.                 </configuration>  
  116.             </plugin>  
  117.         </plugins>  
  118.     </build>  
  119. </project>  

以上pom要注意的是:

1.添加的两个依赖一个是ejb,一个是war包,指明的类型分别是:

<type>ejb</type>和<type>war</type>
2.EAR的打包插件

<artifactId>maven-ear-plugin</artifactId>

3.添加的ejb和war需要分别指明:<ejbModule> 和 <webModule> 并且都可以定制打进ear的名称,通过:<bundleFileName>


执行ear模块的package目标或执行ear模块maven的ear:ear可完成 ear的打包工作。

关于 maven-ear-plugin ,官网参考:http://maven.apache.org/plugins/maven-ear-plugin/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
将Eclipse中的EAR项目转换为Maven项目,需要进行以下步骤: 1. 首先,确保你的Eclipse安装了Maven插件。如果没有安装,可以从Eclipse Marketplace搜索"Maven"进行安装。 2. 在Eclipse中,右键点击EAR项目,选择Export -> General -> Archive File,将EAR项目导出为zip文件。 3. 解压缩zip文件,将解压后的文件夹中的META-INF文件夹删除。 4. 在解压后的文件夹中,创建一个名为pom.xml的文件,并编辑该文件,添加Maven项目的基本信息,如下所示: ``` <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>com.example</groupId> <artifactId>example-ear</artifactId> <version>1.0</version> <packaging>ear</packaging> <dependencies> <!-- Add your project dependencies here --> </dependencies> </project> ``` 注意,需要根据你的项目信息修改groupId、artifactId、version等字段。 5. 将EAR项目中的模块(如ejb、web等)分别转换为Maven项目,具体步骤可以参考之前回答的问题。 6. 在Maven项目的pom.xml文件中,添加EAR项目的依赖,如下所示: ``` <dependency> <groupId>com.example</groupId> <artifactId>example-ear</artifactId> <version>1.0</version> <type>ear</type> </dependency> ``` 7. 在Eclipse中,右键点击Maven项目,选择Maven -> Update Project,更新项目依赖关系。 通过以上步骤,就可以将EAR项目转换为Maven项目了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值