maven assembly plugin使用__maven

來源:互聯網
上載者:User
使用情境

在使用maven來管理項目時,項目除了web項目,還有可能為控制台程式,一般用於開發一些後台服務的程式。最近在工作中也遇到了這種情境,使用quartz開發一個任務發送器。程式中依賴很多jar包,項目的啟動時只需要初始化spring容器即可。 使用方法

使用一個簡單的基於spring架構的demo來做程式樣本,來介紹maven assembly外掛程式的使用方法。
項目中的代碼目錄如下:


在以上代碼目錄中,assembly目錄下為打包的描述檔案,下面會詳細介紹該檔案,bin目錄下為啟動指令碼以及readme等檔案,main下為maven標準中建立的檔案,java代碼以及設定檔位於該目錄下。
打包完成後壓縮包目錄如下:


打包完成後,我們可以看到bin目錄來存放啟動指令碼等檔案,config為設定檔,lib下為運行時依賴的jar包。
使用maven assembly外掛程式需要在pom檔案中配置,添加這個外掛程式

            <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配置了,我們打包的目錄以及相應的設定

<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/>兩個非常有用的節點。
比如:

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

更多節點的用法可以去官網查詢
http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html

下面的文章會介紹打包後的包如何做成windows服務,linux服務。 參考資料

http://maven.apache.org/plugins/maven-assembly-plugin/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.