1. 程式人生 > >EAR專案構建的幾種方式

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打包時需要包含相關資源的配置檔案。樣例如下:

<?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等依賴,如下:

<dependency>
    	<groupId>com.test</groupId>
    	<artifactId>test</artifactId>
    	<version>0.0.1</version>
    	<type>war</type>
    </dependency>


3、新增maven-ear-plugin外掛,並配置

			<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進行配置

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


如此,可解除上述錯誤~

其他參考: