Springboot给了我们很多方便,不再需要单独部署一个tomcat,运行起来也很简单。但是对于配置文件因为项目经常修改的时候,却有些不太方便,每次改完配置都得重新打包。这里我也探索了一下将资源包分离的方式,请看下面的具体实现。
方式一、使用maven插件来打包
1.修改pom文件
首先添加几个插件,maven-jar-plugin
,maven-dependency-plugin
,maven-resources-plugin
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!--重写包含依赖,包含不存在的依赖,jar里没有pom里的依赖--> <includes> <include> <groupId>null</groupId> <artifactId>null</artifactId> </include> </includes> <layout>ZIP</layout> <!--使用外部配置文件,jar包里没有资源文件--> <addResources>true</addResources> <outputDirectory>${project.build.directory}</outputDirectory> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> <configuration> <!--<classifier>run</classifier>--> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <!--这里配置MANIFEST.MF文件,添加正确的依赖,Mainclass--> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <useUniqueVersions>false</useUniqueVersions> <mainClass>com.xxx.StarterApplication</mainClass> </manifest> <manifestEntries> <!--MANIFEST.MF 中 Class-Path 加入资源文件目录--> <Class-Path>./config/</Class-Path> </manifestEntries> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <!--这里是配置依赖jar包输出目录--> <outputDirectory> ${project.build.directory}/lib/ </outputDirectory> </configuration> </execution> </executions> </plugin> <plugin> <artifactId>maven-resources-plugin</artifactId> <executions> <execution> <id>copy-resources</id> <phase>package</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <resources> <!--这里是相关配置文件的输出目录,当然你可以过滤一些文件--> <resource> <directory>src/main/resources</directory> <!--这里是忽略的文件(类型)--> <!--<excludes> <exclude>**/*.xml</exclude> </excludes>--> <!--这里是需要导出的文件(类型)--> <!--<includes> <include>*.properties</include> <include>*.yml</include> </includes>--> </resource> </resources> <!--输出目录--> <outputDirectory>${project.build.directory}/config</outputDirectory> </configuration> </execution> </executions> </plugin> </plugins> </build>
2.打包
然后执行mvn clear
和mvn install
或直接mvn clean install
,之后可以在target
目录看到相应的包和文件
打开config
目录看一下
原来resource
的配置文件都打包进来了,当然lib
也是一样的。
3.执行
可以将这几个目录和文件拷贝出来,然后编写启动脚本
@echo off java -jar xxx-service.jar
保存为startup.bat
,双击文件即可执行.然后可以试着修改一下application.yml
的配置再次运行看看效果
方式二、使用assembly插件
1.修改pom文件
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <useUniqueVersions>false</useUniqueVersions> <mainClass>com.xxx.StarterApplication</mainClass> </manifest> <manifestEntries> <!--MANIFEST.MF 中 Class-Path 加入资源文件目录--> <Class-Path>./resource/</Class-Path> </manifestEntries> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <!-- The configuration of the plugin --> <configuration> <!-- Specifies the configuration file of the assembly plugin --> <descriptors> <descriptor>src/main/package.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> <resources> <!--这里过滤xml文件,比如mybatis等配置还是打进jar包--> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <!--这里过滤xml文件,比如mybatis等配置还是打进jar包--> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.xml</include> </includes> </resource> <!--依赖jar包--> <resource> <directory>lib/</directory> <targetPath>lib</targetPath> <includes> <include>**/*.jar</include> </includes> </resource> </resources> </build>
package.xml文件
然后在src/main/
目录新建一个package.xml
文件
<?xml version="1.0" encoding="UTF-8"?> <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>package</id> <formats> <!--ZIP格式,还可以添加tar.gz等格式--> <format>zip</format> </formats> <includeBaseDirectory>true</includeBaseDirectory> <fileSets> <!--*.properties,*.yml配置文件输出到config目录--> <fileSet> <directory>${project.build.directory}/classes</directory> <outputDirectory>config</outputDirectory> <includes> <include>*.properties</include> <include>*.yml</include> </includes> </fileSet> <!-- 把项目自己编译出来的jar文件,打包进zip文件的根目录 --> <fileSet> <directory>${project.build.directory}</directory> <outputDirectory>/</outputDirectory> <includes> <include>*.jar</include> </includes> </fileSet> </fileSets> </assembly>
2.打包
然后执行mvn clear
和mvn install
或直接mvn clean install
,之后可以在target
目录看到相应的包和文件
多了一个zip
的包,解压出来可以看到
config
目录里面的文件结构
3.执行
@echo off java -jar xxx-service.jar --spring.config.location=config/application.yml
保存为startup.bat
,试着修改一下application.yml
的配置再次运行看看效果
参考:
https://www.jianshu.com/p/dbdece9062b3
https://blog.csdn.net/u013314786/article/details/81120240
2020-11-05 补充
一个项目一个jar包的配置
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.2.6.RELEASE</version> <configuration> <fork>true</fork> <!-- 如果没有该配置,devtools不会生效 --> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.1.0</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> <warName>${project.artifactId}</warName> </configuration> </plugin> </plugins> <finalName>${project.artifactId}</finalName> </build>