學習記錄1--Springboot的Maven自定義打包 在以往的開發中,Springboot應用預設打成一個jar,雖然方便但是會有很多問題,比如不方便修改配置文件,修改一個處代碼就要更新整包等等,而maven中也有這樣的插件可以給我們提供幫助 複製引用依賴插件 <plugin> <groupId ...
學習記錄1--Springboot的Maven自定義打包
在以往的開發中,Springboot應用預設打成一個jar,雖然方便但是會有很多問題,比如不方便修改配置文件,修改一個處代碼就要更新整包等等,而maven中也有這樣的插件可以給我們提供幫助
- 複製引用依賴插件
<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打的全量包