Mava製作包
<!-- /frame contents --> 按照Maven的標準做法,該功能以插件的方式提供,這個插件就是Maven Assembly Plugin,它的網址是:
http://maven.apache.org/plugins/maven-assembly-plugin/
1.預備工作
下載最新Maven 2的軟件包,安裝後將其bin路徑註冊到系統的查找路徑中。
執行下面的命令創建一個供測試用的待發布web項目:
命令行
mvn archetype:create
-DgroupId=de.focusdv.bcs -DartifactId=myweb
-DarchetypeArtifactId=maven-archetype-webapp
這會產生下面的目錄結構:
Java 代碼
myweb
deploy <-- 存放用於製作發行包的Maven配置文件。
output <-- 預期存放我們即將創建的發行包,假設是myweb-1.0-SNAPSHOT.zip。在真實的項目
中,將這個目錄單獨放置到其他地方也許會更好。
dist <-- 存放打包文件,假設這裏是myweb-1.0-SNAPSHOT.war
src
main
java
esources
webapp
test
target
pom.XML
除了deploy和dist目錄由我們自己手動創建外,其餘部分爲Maven按照標準的目錄佈局自動產生。
接下來我們的任務就是將myweb下面的src目錄、dist目錄,以及pom.xml文件打包到myweb-1.0-SNAPSHOT.zip中,供我們的客戶使用。
2.編寫配置文件
首先在deploy目錄中編寫Maven項目的配置文件pom.xml:
xml 代碼
<project>
<modelVersion>4.0.0<!--</span-->modelVersion>
<groupId>com.mycompany<!--</span-->groupId>
<artifactId>myweb<!--</span-->artifactId>
<name>myweb<!--</span-->name>
<version>1.0-SNAPSHOT<!--</span-->version>
<url>com.mycompany<!--</span-->url>
<build>
<!-- 發行包的輸出路徑 -->
<Directory>output<!--</span-->directory>
<plugins>
<!-- 對Maven Assembly插件進行配置 -->
<plugin>
<artifactId>maven-assembly-plugin<!--</span-->artifactId>
<configuration>
<!-- 在生成的發行包名稱中不包含assemblyId(這裏是“distribution”) -->
<appendAssemblyId>false<!--</span-->appendAssemblyId>
<!-- 指定唯一的描述文件 -->
<descriptors>
<descriptor>distribution.xml<!--</span-->descriptor>
<!--</span-->descriptors>
<!--</span-->configuration>
<!--</span-->plugin>
<!--</span-->plugins>
<!--</span-->build>
<!--</span-->project>
xml 代碼 主要信息已經註釋到代碼中了。
然後在相同目錄下編寫在pom.xml中指定的名爲distribution.xml的文件:
xml 代碼
<assembly>
<id>distribution<!--</span-->id>
<formats>
<!-- 指定發行包使用zip格式 -->
<format>zip<!--</span-->format>
<!--</span-->formats>
<!-- 在發行包中使用根目錄,按照慣例,這裏是 myweb-1.0-SNAPSHOT-->
<includeBaseDirectory>true<!--</span-->includeBaseDirectory>
<!-- 將要打包到發行包中的內容 -->
<fileSets>
<fileSet>
<directory>dist<!--</span-->directory>
<directory>src<!--</span-->directory>
<includes>
<include>*.xml<!--</span-->include>
<!--</span-->includes>
<outputDirectory>/<!--</span-->outputDirectory>
<!--</span-->fileSet>
<!--</span-->fileSets>
<!--</span-->assembly>
重要信息同樣已經包含到註釋中了。
3.製作發行包
編寫好這兩個文件之後,在命令行中切換到deploy目錄下,執行下面的命令:
mvn assembly:assembly
假如一切順利,應該能在output目錄下找到myweb-1.0-SNAPSHOT.zip文件,我們的發行包已經整裝待發了
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.