springboot项目打包发布流程
1、概述
记录jar包linux和windows发布全过程,源码在gitee中托管。
本文使用assembly.xml进行打包,使用sh命令在linux中发布,使用bat命令在windows发布。
下面介绍整个过程
项目地址:https://gitee.com/mfengyu415/SpringBoot-Learn.git 的springboot-12-assembly这个module
2、创建项目
1、创建一个普通的springboot项目
2、添加HellowController
@RestController
public class HelloController {
@RequestMapping("hello")
public String hello(){
return "hello";
}
}
3、测试运行
3、maven-assembly-plugin配置
1、添加pom依赖
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<!-- 打tar.gz包 -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>make-targz</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/../release/tar.gz</outputDirectory>
</configuration>
</execution>
</executions>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>${basedir}/assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>
</plugins>
</build>
2、配置assembly.xml
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>release</id>
<formats>
<format>tar.gz</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.basedir}/src/main/script</directory>
<outputDirectory></outputDirectory>
<includes>
<include>*.sh</include>
<include>*.bat</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.basedir}/target</directory>
<outputDirectory></outputDirectory>
<includes>
<include>springboot-12-assembly*.jar</include>
</includes>
</fileSet>
<!--devops仅用于开发和test环境, stage环境和线上环境会由部署系统自动生成devops并覆盖-->
<fileSet>
<directory>${project.basedir}/src/main/resources/devops</directory>
<outputDirectory>devops</outputDirectory>
<includes>
<include>*.properties</include>
</includes>
</fileSet>
</fileSets>
<files>
<file>
<outputDirectory></outputDirectory>
<source>${project.basedir}/src/main/resources/application.properties</source>
<destName>application.properties</destName>
</file>
</files>
</assembly>
3、添加脚本
main下创建script文件夹,放sh和bat脚本
4、Linux的sh脚本
sh脚本使用notepad转换为unix脚本,否则无法执行
1、启动脚本(start.sh)
#!/bin/sh
source /etc/profile
base_dir=$(cd "$(dirname "$0")"; pwd)
jar_file=`ls $base_dir -t| grep "^springboot-12-assembly.*\.jar$"`
server_name="springboot-12-assembly"
launcher_daemon_out="server.out"
if [ ! -f "$jar_file" ]
then
echo "can not found jar file , failed to start server! "
exit 1
fi
pid=`ps -ef | grep "serverName=$server_name" | grep -v "grep" | awk '{print $2}'`
if [ "$pid" = "" ];then
nohup java -DserverName=$server_name -Dbasedir=$base_dir -Djava.security.egd=file:/dev/./urandom -Dloader.path=. $jvm_args -jar $jar_file >"$launcher_daemon_out" 2>&1 < /dev/null &
else
echo "$server_name is running"
fi
2、停止脚本(stop.sh)
#!/bin/sh
server_name="springboot-12-assembly"
pid=`ps -ef | grep "serverName=$server_name" | grep -v "grep" | awk '{print $2}'`
if [ "$pid" = "" ]
then
echo "$server_name is not running"
else
kill -9 $pid
echo "kill pid:$pid"
echo "$server_name stop success"
fi
5、windows的bat脚本
1、启动脚本(start.bat)
@echo off
%1 mshta vbscript:CreateObject("WScript.Shell").Run("%~s0 ::",0,FALSE)(window.close)&&exit
java -jar -Dfile.encoding=utf-8 springboot-12-assembly-0.0.1-SNAPSHOT.jar >StartupLog.log 2>&1 &
exit
2、停止脚本(stop.bat)
echo -------WechatApp stop--------
set port=8081
for /f "tokens=1-5" %%i in ('netstat -ano^|findstr ":%port%"') do (taskkill /f /pid %%m)
echo -------WechatApp end------------------
@pause
6、打包
方法一、idea中直接运行maven的package
方法二、命令
mvn clean package -Dmaven.test.skip=true -Pstage -s D:\mygit\SpringBoot-Learn\springboot-07-jpa\assembly.xml
7、windows下部署
直接运行start.start后台运行输入日志查看StartupLog.log
运行stop.bat命令结束运行
8、Linux下部署
运行start.sh后台运行,server.out查看运行日志
sh start.sh
运行stop.sh结束运行
sh stop.sh
9、linunx下开放端口
- 重新打开防火墙:
systemctl start firewalld - 添加8888端口的访问权限,这里会永久生效
firewall-cmd --zone=public --add-port=8888/tcp --permanent - 重新载入防火墙,添加的端口才能生效
firewall-cmd --reload - 循环2-3步,添加想要的端口,然后关闭防火墙
systemctl stop firewalld - 查询防火墙开放端口
sudo iptables-save
10、测试
windows中直接浏览器打开
http://127.0.0.1:8082/hello 返回hello
linux下
curl http://127.0.0.1:8082/hello 返回hello
11、源码地址
项目地址:https://gitee.com/mfengyu415/SpringBoot-Learn.git 的springboot-12-assembly这个module