Maven3種打包方式之一maven-assembly-plugin的使用


在Maven中,主要有3個插件可以用來打包:

  • maven-jar-plugin,默認的打包插件,用來打普通的project JAR包;
  • maven-shade-plugin,用來打可執行JAR包,也就是所謂的fat JAR包;
  • maven-assembly-plugin,支持自定義的打包結構,也可以定制依賴項等。

我們日常使用的以maven-assembly-plugin為最多,因為大數據項目中往往有很多shell腳本、SQL腳本、.properties及.xml配置項等,采用assembly插件可以讓輸出的結構清晰而標准化。
要使用該插件,就在項目pom文件中加入以下內容。

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>${maven-assembly-plugin.version}<version>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <!-- 綁定到package生命周期 -->
                        <phase>package</phase>
                        <goals>
                            <!-- 只運行一次 -->
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!-- 配置描述符文件 -->
                    <descriptor>src/main/assembly/assembly.xml</descriptor>
                    <!-- 也可以使用Maven預配置的描述符
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs> -->
                </configuration>
            </plugin>
        </plugins>
    </build>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

assembly插件的打包方式是通過descriptor(描述符)來定義的。
Maven預先定義好的描述符有bin,src,project,jar-with-dependencies等。比較常用的是jar-with-dependencies,它是將所有外部依賴JAR都加入生成的JAR包中,比較傻瓜化。
但要真正達到自定義打包的效果,就需要自己寫描述符文件,格式為XML。下面是我們的項目中常用的一種配置。

<assembly>
    <id>assembly</id>

    <formats>
        <format>tar.gz</format>
    </formats>

    <includeBaseDirectory>true</includeBaseDirectory>

    <fileSets>
        <fileSet>
            <directory>src/main/bin</directory>
            <includes>
                <include>*.sh</include>
            </includes>
            <outputDirectory>bin</outputDirectory>
            <fileMode>0755</fileMode>
        </fileSet>
        <fileSet>
            <directory>src/main/conf</directory>
            <outputDirectory>conf</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>src/main/sql</directory>
            <includes>
                <include>*.sql</include>
            </includes>
            <outputDirectory>sql</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>target/classes/</directory>
            <includes>
                <include>*.properties</include>
                <include>*.xml</include>
                <include>*.txt</include>
            </includes>
            <outputDirectory>conf</outputDirectory>
        </fileSet>
    </fileSets>

    <files>
        <file>
            <source>target/${project.artifactId}-${project.version}.jar</source>
            <outputDirectory>.</outputDirectory>
        </file>
    </files>

    <dependencySets>
        <dependencySet>
            <unpack>false</unpack>
            <scope>runtime</scope>
            <outputDirectory>lib</outputDirectory>
        </dependencySet>
    </dependencySets>
</assembly>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

id與formats

formats是assembly插件支持的打包文件格式,有zip、tar、tar.gz、tar.bz2、jar、war。可以同時定義多個format。
id則是添加到打包文件名的標識符,用來做后綴。
也就是說,如果按上面的配置,生成的文件就是a r t i f a c t I d − {artifactId}-artifactId−{version}-assembly.tar.gz。

fileSets/fileSet
用來設置一組文件在打包時的屬性。

directory:源目錄的路徑。
includes/excludes:設定包含或排除哪些文件,支持通配符。
fileMode:指定該目錄下的文件屬性,采用Unix八進制描述法,默認值是0644。
outputDirectory:生成目錄的路徑。

files/file
與fileSets大致相同,不過是指定單個文件,並且還可以通過destName屬性來設置與源文件不同的名稱。
dependencySets/dependencySet
用來設置工程依賴文件在打包時的屬性。也與fileSets大致相同,不過還有兩個特殊的配置:

unpack:布爾值,false表示將依賴以原來的JAR形式打包,true則表示將依賴解成*.class文件的目錄結構打包。
scope:表示符合哪個作用范圍的依賴會被打包進去。compile與provided都不用管,一般是寫runtime。

按照以上配置打包好后,將.tar.gz文件上傳到服務器,解壓之后就會得到bin、conf、lib等規范化的目錄結構,十分方便。

參考
https://www.jianshu.com/p/e581fff1cf87
https://cloud.tencent.com/developer/article/1354070


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM