Springboot自定義打包

學習記錄1--Springboot的Maven自定義打包

在以往的開發中,Springboot應用默認打成一個jar,雖然方便可是會有不少問題,好比不方便修改配置文件,修改一個處代碼就要更新整包等等,而maven中也有這樣的插件能夠給咱們提供幫助linux

  • 複製引用依賴插件
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <!-- 指定輸出依賴的位置 -->
        <outputDirectory>lib</outputDirectory>
        <overWriteReleases>false</overWriteReleases>
        <overWriteSnapshots>false</overWriteSnapshots>
        <overWriteIfNewer>true</overWriteIfNewer>
    </configuration>
</plugin>
  • 使用jar插件打成jar包
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                 <!-- 指定依賴加載路徑 -->
                <classpathPrefix>lib/</classpathPrefix>
                 <!-- 指定主類 -->
                <mainClass>club.menglinking.wx.WxApplication</mainClass>
            </manifest>
        </archive>
        <excludes>
            <!-- 排除配置文件和靜態文件 -->
            <exclude>**/*.properties</exclude>
            <exclude>**/*.yml</exclude>
            <exclude>static/**</exclude>
            <exclude>templates/**</exclude>
        </excludes>
    </configuration>
</plugin>
  • 使用assembly自定義打包
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <descriptors>
             <!-- 打包配置文件的路徑 -->
            <descriptor>bin/package.xml</descriptor>
        </descriptors>
    </configuration>
    <executions>
        <execution>
            <!-- 綁定插件到生命週期 -->
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>
  • 打包示例配置文件
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <!-- 保證惟一 -->
    <id>weChat</id>
    <!-- 打成的分發包格式,推薦windows和linux雙平臺包 -->
    <formats>
        <format>zip</format>
        <format>tar.gz</format>
    </formats>

    <includeBaseDirectory>true</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>bin</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>*.bat</include>
                <include>*.sh</include>
                <include>*.xml</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>target</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>lib</directory>
            <outputDirectory>lib/</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>src/main/resources</directory>
            <outputDirectory>/config</outputDirectory>
            <includes>
                <include>*.properties</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>src/main/resources/static</directory>
            <outputDirectory>/static</outputDirectory>
            <includes>
                <include>**/*</include>
            </includes>
        </fileSet>
    </fileSets>
    <files>
        <file>
            <source>README.md</source>
            <outputDirectory>/</outputDirectory>
        </file>
    </files>
</assembly>

打包完成以後,配置文件,靜態文件,依賴和代碼分離,修改代碼後只須要把打成的jar覆蓋便可完成增量更新(在沒有新引入依賴的狀況下),生成的jar會遠遠小於springboot打的全量包spring

相關文章
相關標籤/搜索