Maven打包之maven-assembly-plugin

本節主要學習 maven assembly 部件插件的使用。

打包是一個比較頭疼的事情,默認maven打包的結果只包含項目本身的代碼,如果要執行代碼,還得帶上依賴。如果希望將所有依賴放到指定的文件夾下,再在目錄下寫入shell腳本等之類的事情。maven-shade-plugin插件不能滿足需求,而maven-assembly-plugin插件能夠幫我們合理地歸檔文件。

1. 簡單的使用

assembly提供了幾種默認的打包方式,使用 descriptorRefs, descriptorRef 有 bin, jar-with-dependencies, src, project。【不建議使用】

<plugins>
    <plugin>
        <!-- NOTE: We don't need a groupId specification because the group is
             org.apache.maven.plugins ...which is assumed by default.
         -->
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
    </plugin>

2. descriptor使用原理

assembly允許用戶自定義歸檔文件格式,位置,結構等信息。允許配置並執行多個自定義配置。

所以每一個配置都需要一個唯一id來做區分。歸檔格式可以有tar.gz,dir,zip,war等等,允許同時歸檔出多種格式文件。對於打的jar包,可以指定輸出目錄。對於一組特定的文件,可以指定存放的文件夾。如果有單獨需要存放的文件,也可以單獨處理。

3. assembly配置

assembly配置分爲兩部分,一部分是assembly的配置文件,一部分是在pom.xml中的配置。

3.1 assembly配置文件

首先我們通過以下配置文件瞭解一下配置文件是怎麼工作的。

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd
http://maven.apache.org/ASSEMBLY/2.0.0 ">

    <!--唯一ID-->
    <id>assembly_test</id>

    <!--打包格式,允許同時有多個-->
    <formats>
        <format>tar.gz</format>
        <format>dir</format>
        <format>zip</format>
    </formats>

    <!--依賴jar包以及項目打包文件存儲文件-->
    <dependencySets>
        <dependencySet>
            <!--存儲在projectName-assembly-version/lib下-->
            <outputDirectory>lib</outputDirectory>
        </dependencySet>
    </dependencySets>

    <fileSets>
        <fileSet>
            <!--目錄路徑,如果不在這裏指定,而在include中指定,那麼其文件夾的也會被帶進去-->
            <directory>src/main/bin/</directory>
            <includes>
                <!--要哪些文件-->
                <include>*.*</include>
            </includes>
            <excludes>
                <!--不要哪些文件-->
                <exclude>*.no_need</exclude>
            </excludes>
            <!--文件的權限-->
            <fileMode>0755</fileMode>
            <!--輸出目錄 存儲在projectName-assembly-version/bin下-->
            <outputDirectory>bin</outputDirectory>
            <directoryMode>0755</directoryMode>

        </fileSet>
    </fileSets>

    <files>
        <!--針對單個文件-->
        <file>
            <!--源文件地址,相對於項目地址-->
            <source>pom.xml</source>
            <!--輸出目錄爲projectName-assembly-version/-->
            <outputDirectory>.</outputDirectory>
            <!--文件的權限-->
            <fileMode>0755</fileMode>
            <!--重命名爲-->
            <destName>pom.xml</destName>
        </file>
    </files>

</assembly>

3.2 pom中的配置

assembly插件在pom中配置較爲簡單,但需要配置maven自帶的插件實現main方法添加和classpath依賴。假設上邊的assembly配置文件在src/main/assembly/assembly.xml

pom配置內容爲

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.3.1</version>
            <configuration>
                <archive>
                    <manifest>
                        <!--運行jar包時運行的主類,要求類全名-->
                        <mainClass>com.gavinzh.learn.assembly.Main</mainClass>
                        <!-- 是否指定項目classpath下的依賴 -->
                        <addClasspath>true</addClasspath>
                        <!-- 指定依賴的時候聲明前綴 -->
                        <!--<classpathPrefix>./</classpathPrefix>-->
                    </manifest>
                </archive>
            </configuration>
        </plugin>

        <plugin>
            <!-- NOTE: We don't need a groupId specification because the group is
                 org.apache.maven.plugins ...which is assumed by default.
             -->
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
                <descriptors>
                    <descriptor>src/main/assembly/assembly.xml</descriptor>
                </descriptors>
            </configuration>

            <executions>
                <execution>
                    <id>assembly_test</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>


    </plugins>
</build>

4. 參考

Apache Maven Assembly Plugin

maven-assembly-plugin 簡書

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章