SpringBoot项目使用maven-assembly-plugin插件多环境打包
1.创建SpringBoot项目并在pom.xml文件中添加maven-assembly-plugin配置
<!-- 多环境配置 -->
<profiles>
<!-- 开发环境 -->
<profile>
<id>dev</id>
<properties>
<profileActive>dev</profileActive>
</properties>
<!-- 默认激活的环境 -->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!-- 生产环境 -->
<profile>
<id>prod</id>
<properties>
<profileActive>prod</profileActive>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
</profiles>
<build>
<!-- 打包后的jar包名称 -->
<finalName>spring-boot-demo</finalName>
<!-- 打包配置 -->
<plugins>
<!-- maven 打包时跳过测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- <!– 指定该Main Class为全局的唯一入口 –>-->
<!-- <mainClass>com.xhs.ToolsApp</mainClass> -->
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<!--可以把依赖的包都打包到生成的Jar包中-->
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 个性化打包 -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<!-- 打包文件名字不包含 assembly.xml 中 id -->
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<!-- 资源配置 -->
<resources>
<resource>
<directory>src/main/java</directory>
<filtering>true</filtering>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>*.yml</include>
<include>*.xml</include>
</includes>
</resource>
</resources>
</build>
2.创建 在src/main/assembly目录下创建assembly.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<assembly>
<!-- 打包文件名的标识符,用来做后缀-->
<id>make-assembly</id>
<!-- 打包的类型,如果有N个,将会打N个类型的包 -->
<formats>
<format>tar.gz</format>
<format>zip</format>
</formats>
<!-- 压缩包下是否生成和项目名相同的根目录 -->
<includeBaseDirectory>true</includeBaseDirectory>
<!-- 用来设置一组文件在打包时的属性。-->
<fileSets>
<!-- 0755->即用户具有读/写/执行权限,组用户和其它用户具有读写权限;-->
<!-- 0644->即用户具有读写权限,组用户和其它用户具有只读权限;-->
<!-- 将src/bin目录下的jar启动脚本输出到打包后的目录中 -->
<fileSet>
<directory>${basedir}/src/main/bin</directory>
<outputDirectory>${file.separator}</outputDirectory>
<fileMode>0755</fileMode>
<includes>
<include>**.sh</include>
<include>**.bat</include>
</includes>
</fileSet>
<!-- 把项目的配置文件,打包进压缩文件的config目录 -->
<fileSet>
<directory>${basedir}/src/main/resources</directory>
<outputDirectory>config</outputDirectory>
<fileMode>0644</fileMode>
<includes>
<include>*.properties</include>
<include>*.yml</include>
<include>*.xml</include>
</includes>
</fileSet>
<!-- 把项目自己编译出来的jar文件,打包进zip文件的根目录 -->
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>${file.separator}</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
</assembly>
3.在src/main/bin创建在linux环境启动jar包的脚本
#!/bin/bash
# jar包名称
JAR_FILE="spring-boot-demo.jar"
# pid 名称
PID_FILE="spring-boot-demo.pid"
start() {
if [ -f "$PID_FILE" ]; then
echo "应用程序已在运行,PID: $(cat $PID_FILE) .........."
else
# 后台启动jar包,并将启动日志输出到log.log文件中
nohup java -jar -Dloader.path=.,3rd-li $JAR_FILE >> log.log 2>&1 &
echo $! > $PID_FILE
echo "应用程序已成功启动,PID: $(cat $PID_FILE) .........."
fi
}
stop() {
if [ -f "$PID_FILE" ]; then
kill -9 $(cat $PID_FILE)
rm $PID_FILE
echo "应用程序已成功停止.........."
else
echo "应用程序未运行.........."
fi
}
restart() {
echo "正在重启.........."
stop
start
echo "重启成功.........."
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo "使用: $0 {start|stop|restart} 命令"
exit 1
;;
esac
4.配置application.yml文件
#application.yml
spring:
profiles:
active: @profileActive@
#application-dev.yml
server:
port: 8001
spring:
application:
name: spring-boot-demo
#application-prod.yml
server:
port: 8001
spring:
application:
name: spring-boot-demo
5.启动项目
6.打包
mvn clean package -P prod
7.打包后的目录结构
8.上传到linux服务器并启动jar包文章来源:https://www.toymoban.com/news/detail-650196.html
# 解压tar包
tar -zxf spring-boot-demo.tar.gz
#启动jar包
sh app.sh start
#查看日志
tail -f log.log
9.调用接口测试
10.源码地址
https://gitee.com/xhs101/spring-boot-demo文章来源地址https://www.toymoban.com/news/detail-650196.html
到了这里,关于【Maven】SpringBoot项目使用maven-assembly-plugin插件多环境打包的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!