EAR项目构建的几种方式

PS:说实话,在写这篇帖子以前,我也没用过EAR,所以该贴仅是记录学习过程用~有什么不对的地方,或者欠缺的,还请各位看官斧正,先谢过各位了~~ 1、基于传统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?html

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

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

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

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

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

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

三、添加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>maven

</plugin> 学习

四、很大可能在配置完上面的内容后,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>

如此,可解除上述错误~

相关文章
相关标签/搜索