1. 修改pom.xml
pom.xml中設置如下:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<!-- not append assembly id in release file name -->
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assemble/package.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<!-- not append assembly id in release file name -->
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assemble/package.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
其中<artifactId>maven-assembly-plugin</artifactId>的maven-assembly-plugin是這個插件的標準命名,在maven2.0.*中帶的默認版本是
appendAssemblyId屬性控制是否在生成的打包文件的文件名中包含assembly id。
descriptor屬性指定maven-assembly-plugin的配置文件,當然我設置的是src/main/assemble/package.xml.容許使用多個,功能強大當然用法也複雜,對於簡單情況一個足矣。
execution的設置是爲了將maven-assembly-plugin繼承到標準的maven打包過程中,這樣在運行maven-package時就會執行maven-assembly-plugin的操作,從而實現我們需要的自定義打包。
2. assemble descriptor file
我的src/main/assemble/package.xml內容如下:
<assembly 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/assembly-1.0.0.xsd">
<id>package</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>src/main/bin</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
<fileSet>
<directory>src/main/config</directory>
<outputDirectory>config</outputDirectory>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/assembly-1.0.0.xsd">
<id>package</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>src/main/bin</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
<fileSet>
<directory>src/main/config</directory>
<outputDirectory>config</outputDirectory>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>
詳細的語法不介紹了,請參考官方指南,有非常詳盡的說明:Assembly Descriptor Format reference
簡單解釋一下:
1) format
format=zip設置打包的最終文件格式爲zip.
支持的其他格式還有gz,tar,tar.gz,tar.bz2。
2) fileset
<fileSet>
<directory>src/main/bin</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
<directory>src/main/bin</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
將src/main/bin目錄下的文件打包到根目錄(/)下.
<fileSet>
<directory>src/main/config</directory>
<outputDirectory>config</outputDirectory>
</fileSet>
<directory>src/main/config</directory>
<outputDirectory>config</outputDirectory>
</fileSet>
將src/main/config目錄下的文件打包到config下.
3) dependencySets
<dependencySet>
<outputDirectory>lib</outputDirectory>
<scope>runtime</scope>
</dependencySet>
<outputDirectory>lib</outputDirectory>
<scope>runtime</scope>
</dependencySet>
將scope爲runtime的依賴包打包到lib目錄下。
總結一下,pom.xml中引入maven-assembly-plugin,然後assemble descriptor file按需設置,最後在eclipse中執行Run As -> Maven package,在target目錄下就會出現***.zip文件,裏面的格式和要求的完全一致。