SpringBoot迭代發佈JAR瘦身配置(續:將lib文件夾壓縮打包) 原 薦

上次寫了篇 《SpringBoot迭代發佈JAR瘦身配置》,但有一個問題,所有的第三方JAR位於lib目錄中,不利於傳輸到服務器,因此應該考慮將此目錄壓縮打包,再傳輸到服務器,服務器解壓即可使用。

經過一番google,找到類似的plugin(maven-assembly-plugin),看官網的介紹:

The Assembly Plugin for Maven is primarily intended to allow users to aggregate the project output along with its dependencies, modules, site documentation, and other files into a single distributable archive.

大致意思就是:Assembly Plugin 主要是爲了允許用戶將項目輸出及其依賴項、模塊、文檔和其他文件聚合到一個可發佈的文件中。通俗一點就是可以定製化自己想要打包的文件,支持打包的格式有:zip、tar、tar.gz (or tgz)、tar.bz2 (or tbz2)、tar.snappy、tar.xz (or txz)、jar、dir、war。

接下來我就根據自己的需求定製我想要的包,我想把 target/lib 目錄壓縮打包,

  • 在pom的配置如下:
<!-- lib 文件夾壓縮打包 -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <finalName>${project.artifactId}-libs</finalName>
        <encoding>UTF-8</encoding>
        <descriptors>
            <descriptor>assembly.xml</descriptor>
        </descriptors>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>
  • 在項目根目錄下新建文件assembly.xml,內容如下:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/3.1.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/3.1.0 http://maven.apache.org/xsd/assembly-3.1.0.xsd">

    <formats>
        <format>tar.gz</format>
    </formats>
    <id>${project.version}</id>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>target/lib</directory>
            <outputDirectory>lib</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

到此,配置就寫完了,執行打包命令:mvn clean package -Dmaven.test.skip=true,就可以在target目錄下看到我們的壓縮文件:

組合上篇 《SpringBoot迭代發佈JAR瘦身配置的配置,就可以實現把spring boot 項目打包成thin jar 以及把第三方jar打成壓縮文件。當然,我這裏用maven-assembly-plugin,根本就是大材小用,還有點違背此plugin的初衷,我只是取巧了而已。另外,如果用jenkins發佈的話,用命令將lib目錄壓縮打包也是可以的,有時候解決問題的辦法很多很多,任君選擇!

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