EAR项目构建的几种方式

本文介绍如何在Eclipse中通过传统WebProject方式及使用maven-ear-plugin插件构建EAR项目,涉及配置步骤、常见问题及解决方案。

PS:说实话,在写这篇帖子之前,我也没用过EAR,因此该贴仅是记录学习过程用~有什么不对的地方,或者欠缺的,还请各位看官斧正,先谢过各位了~~ 一、基于传统WebProject方式 在eclipse中,右键new > project > Dynamic web project , 在弹出的对话框中输入项目名称testWeb,注意,在EAR membership中勾选上“Add project to an EAR”,并输入准备创建的EAR的名称,例如TestEAR, 再创建几个Web project,此时可以选择需要加入的EAR的名称,这里,我们都选择TestEAR, 每个web project,我们都可以随意的写些测试页面,以供测试使用。 在TestEAR项目上,右键 > properties 弹出的面板中,进入 project facets条目,可以看到,该项目特征为EAR,且被选中。 在该EAR项目目录下的EarContent文件夹下创建META-INF/application.xml,该文件是设置ear打包时需要包含相关资源的配置文件。样例如下: [html] view plain copy print?

<?xml version="1.0" encoding="UTF-8"?>

<application id="testEAR" version="1.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/application_1_4.xsd">
<display-name>TestEAR</display-name>
<module id="WebModule_testA">
<web>
<web-uri>testA.war</web-uri>
<context-root>.testA</context-root>
</web>
</module>

<module id="WebModule_testB">  
    <web>  
        <web-uri>testB.war</web-uri>  
        <context-root>.testB</context-root>  
    </web>  
</module>  
  
<module id="WebModule_testC">  
    <web>  
        <web-uri>testC.war</web-uri>  
        <context-root>.testC</context-root>  
    </web>  
</module>  

</application>

display-name设置部署显示的名称,module定义一个被包含的资源,可以是EJB的jar,也可以是一个子系统的war,上面这个例子中都是引入的war,因此用web作为标签。web-uri声明被引入的war路径,context-root声明访问路径(相对于该EAR项目)。

在TestEAR项目上右键 export 选择EAR file,即可导出ear文件,用压缩包程序打开TestEAR.ear文件可以看到该ear包含的war文件。

二、基于maven-ear-plugin插件构建EAR项目 1、创建一个maven webapp,将packaging类型选为ear <packaging>ear</packaging> 2、添加包含的war、jar等依赖,如下: [html] view plain copy print? <dependency>
<groupId>com.test</groupId>
<artifactId>test</artifactId>
<version>0.0.1</version>
<type>war</type>
</dependency>

3、添加maven-ear-plugin插件,并配置 [html] view plain copy print? <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.10</version>
<configuration>
<!-- 打包指定目录到lib -->
<defaultLibBundleDir>lib/</defaultLibBundleDir>
<!-- 将多个war的共享包提取到父级别 -->
<skinnyWars>true</skinnyWars>
<modules>
<webModule>
<moduleId>webdemo-framework</moduleId>
<groupId>com.test</groupId>
<artifactId>framework</artifactId>
<bundleFileName>test-framework.war</bundleFileName>
</webModule>
<webModule>
<moduleId>webdemo-test</moduleId>
<groupId>com.test</groupId>
<artifactId>test</artifactId>
<bundleFileName>test.war</bundleFileName>
</webModule>
</modules>
</configuration>

</plugin>

4、很大可能在配置完上面的内容后,pom文件会报错,例如: Maven Project Build Lifecycle Mapping Problem Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-ear-plugin:2.10:generate-application-xml (execution: default-generate-application-xml, phase: generate-resources) 解决方案如下:(参考帖子>>传送门) 在plugins同级增加一组pluginManagement标签,在插件管理中,对lifecycle-mapping进行配置 [html] view plain copy print? <build>

    <pluginManagement>  
        <plugins>  
            <plugin>  
                <groupId>org.eclipse.m2e</groupId>  
                <artifactId>lifecycle-mapping</artifactId>  
                <version>1.0.0</version>  
                <configuration>  
                    <lifecycleMappingMetadata>  
                        <pluginExecutions>  
                            <pluginExecution>  
                                <pluginExecutionFilter>  
                                    <groupId>org.apache.maven.plugins</groupId>  
                                    <artifactId>maven-ear-plugin</artifactId>  
                                    <versionRange>[2.4.0,)</versionRange>  
                                    <goals>  
                                        <goal>generate-application-xml</goal>  
                                    </goals>  
                                </pluginExecutionFilter>  
                                <action>  
                                    <ignore />  
                                </action>  
                            </pluginExecution>  
                        </pluginExecutions>  
                    </lifecycleMappingMetadata>  
                </configuration>  
            </plugin>  
        </plugins>  
    </pluginManagement>  
    <plugins>  
          
    </plugins>  
</build>  

如此,可解除上述错误~

转载于:https://my.oschina.net/fastjrun/blog/712860

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值