在使用maven來管理項目時,項目除了web項目,還有可能爲控制檯程序,通常用於開發一些後臺服務的程序。最近在工做中也遇到了這種場景,使用quartz開發一個任務調度程序。程序中依賴不少jar包,項目的啓動時只須要初始化spring容器便可。html
使用一個簡單的基於spring框架的demo來作程序示例,來介紹maven assembly插件的使用方法。
項目中的代碼目錄以下:java
在以上代碼目錄中,assembly目錄下爲打包的描述文件,下面會詳細介紹該文件,bin目錄下爲啓動腳本以及readme等文件,main下爲maven標準中創建的文件,java代碼以及配置文件位於該目錄下。
打包完成後壓縮包目錄以下:linux
打包完成後,咱們能夠看到bin目錄來存放啓動腳本等文件,config爲配置文件,lib下爲運行時依賴的jar包。
使用maven assembly插件須要在pom文件中配置,添加這個插件web
plugin>
<artifactId>maven-assembly-plugin</artifactId> <version>2.4.1</version> <executions> <execution> <id>make-zip</id> <!-- 綁定到package生命週期階段上 --> <phase>package</phase> <goals> <!-- 綁定到package生命週期階段上 --> <goal>single</goal> </goals> <configuration> <descriptors> <!--描述文件路徑--> <descriptor>src/assembly/assembly.xml</descriptor> </descriptors> </configuration> </execution> </executions> </plugin>
其中execution節點,咱們配置了執行maven assembly插件的一些配置,descriptor節點配置指向assembly.xml的路徑。
在assembly.xml配置了,咱們打包的目錄以及相應的設置spring
assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd"> <id>distribution</id> <formats> <format>zip</format> </formats> <fileSets> <fileSet> <directory>${project.basedir}\src\main\resources</directory> <outputDirectory>\</outputDirectory> </fileSet> <fileSet> <directory>${project.basedir}\src\bin</directory> <outputDirectory>\bin</outputDirectory> </fileSet> </fileSets> <dependencySets> <dependencySet> <useProjectArtifact>true</useProjectArtifact> <outputDirectory>lib</outputDirectory> <!-- 將scope爲runtime的依賴包打包到lib目錄下。 --> <scope>runtime</scope> </dependencySet> </dependencySets></assembly>
assembly.xml的配置項很是多,能夠參考http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html
以上只是用了不多的一部分。
format設置包輸出的格式,以上格式設置的爲zip格式,目前還支持zip,tar,tar.gz,tar.bz2,jar,dir,war格式
fileSet定義代碼目錄中與輸出目錄的映射,在該節點下還有 <includes/>,<excludes/>兩個很是有用的節點。
好比:apache
fileSet>
<directory>${project.basedir}\src\main\resources</directory> <outputDirectory>\</outputDirectory> <includes> <include>some/path</include> </includes> <excludes> <exclude>some/path1</exclude> </excludes> </fileSet>
以上代碼表示歸檔時包括some/path,不包括some/path1
dependencySets節點下爲依賴設置
在上述配置中,表示全部運行時依賴的jar包歸檔到lib目錄下。在上述截圖中lib目錄下的文件就是全部依賴的jar包
更多節點的用法能夠去官網查詢
下面的文章會介紹打包後的包如何作成windows服務,linux服務。windows