参考资料放在前边:
https://zhuanlan.zhihu.com/p/368567423
SpringBoot - 使用assembly进行项目打包教程1(启动脚本、读外部配置文件)
Springboot 打包部署:使用 assembly 打成 tar.gz 压缩包部署在 Linux_springboot 打包成tar.gz_Nelson.Feng的博客-CSDN博客
项目目录结构:
assembly.xml内容如下:
<?xml version="1.0" encoding="utf-8"?>
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<!-- id 标识符,添加到生成文件名称的后缀符。如果指定 id 的话(这里指定的是项目的版本),目标文件则是 ${artifactId}-${id}.jar。【如terminal-dispatch-5.0.0.0.jar】 -->
<id>${project.version}</id>
<!-- 指定打包格式。maven-assembly-plugin插件支持的打包格式有zip、tar、tar.gz (or tgz)、tar.bz2 (or tbz2)、jar、dir、war,可以同时指定多个打包格式 -->
<formats>
<format>tar.gz</format>
</formats>
<!-- 指定打的包是否包含打包层目录(比如finalName是terminal-dispatch,当值为true,所有文件被放在包内的terminal-dispatch目录下,否则直接放在包的根目录下)-->
<includeBaseDirectory>true</includeBaseDirectory>
<!-- 指定将工程依赖的包打到包里的指定目录下 -->
<dependencySets>
<dependencySet>
<useProjectArtifact>true</useProjectArtifact> <!-- 指定打包时是否包含工程自身生成的jar包 -->
<outputDirectory>lib</outputDirectory> <!-- 指定将这些依赖包打到包里lib目录下 -->
<scope>runtime</scope> <!-- 用于管理依赖的部署,runtime表示只在运行时使用 -->
</dependencySet>
</dependencySets>
<!--
0755->即用户具有读/写/执行权限,组用户和其它用户具有读写权限;
0644->即用户具有读写权限,组用户和其它用户具有只读权限;
-->
<!-- 指定要包含的文件集,可以定义多个fileSet -->
<fileSets>
<fileSet>
<directory>src/main/assembly/bin</directory> <!-- 指定归档文件(要打的jar包)要包含的目录(下的文件及文件夹) -->
<outputDirectory>bin</outputDirectory> <!-- 指定要将当前目录(<directory>标签中的目录放在归档文件(要打的jar包)bin目录下) -->
<includes>
<!--<include>terminal-dispatch</include> <!– 精确控制要包含的文件,<exclude>用于精确控制要排除的文件 –>
<include>server</include>-->
<include>*.*</include>
</includes>
<lineEnding>unix</lineEnding>
<filtered>true</filtered><!-- 是否进行属性替换 -->
<fileMode>0755</fileMode> <!-- 设置文件 UNIX 属性,是一种读写权限 -->
</fileSet>
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>config</outputDirectory>
<fileMode>0644</fileMode>
<filtered>true</filtered><!-- 是否进行属性替换 -->
</fileSet>
<fileSet>
<directory>target</directory>
<outputDirectory>lib</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>logs</outputDirectory>
<fileMode>0755</fileMode>
<excludes>
<exclude>**/*</exclude>
</excludes>
</fileSet>
</fileSets>
</assembly>
pom.xml中插件配置如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor><!--指定的描述符文件-->
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id><!--名字任意 -->
<phase>package</phase><!-- 绑定到package生命周期阶段上 -->
<goals>
<goal>single</goal><!-- 该打包任务只运行一次 -->
</goals>
</execution>
</executions>
</plugin>
start.bat内容如下:
echo off
set APP_NAME=module-system-1.0.1.jar
set LOG_IMPL_FILE=log4j2.xml
set LOGGING_CONFIG=
if exist ../config/%LOG_IMPL_FILE% (
set LOGGING_CONFIG=-Dlogging.config=../config/%LOGGING_CONFIG%
)
set CONFIG= -Dlogging.path=../logs %LOGGING_CONFIG% -Dspring.config.location=../config/
set DEBUG_OPTS=
if ""%1"" == ""debug"" (
set DEBUG_OPTS= -Xloggc:../logs/gc.log -verbose:gc -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=../logs
goto debug
)
set JMX_OPTS=
if ""%1"" == ""jmx"" (
set JMX_OPTS= -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9888 -Dcom.sun.management.jmxremote.ssl=FALSE -Dcom.sun.management.jmxremote.authenticate=FALSE
goto jmx
)
echo "Starting the %APP_NAME%"
java -Xms512m -Xmx512m -server %DEBUG_OPTS% %JMX_OPTS% %CONFIG% -jar ../lib/%APP_NAME%
echo "java -Xms512m -Xmx512m -server %DEBUG_OPTS% %JMX_OPTS% %CONFIG% -jar ../lib/%APP_NAME%"
goto end
:debug
echo "debug"
java -Xms512m -Xmx512m -server %DEBUG_OPTS% %CONFIG% -jar ../lib/%APP_NAME%
goto end
:jmx
java -Xms512m -Xmx512m -server %JMX_OPTS% %CONFIG% -jar ../lib/%APP_NAME%
goto end
:end
pause
打包目录如图:
启动服务方法一:
java -jar E:\module-main-test\module-system\target\module-system-1.0.1\lib\module-system-1.0.1.jar
这种方法用的端口号是项目里的端口号。
方法二:
双击start.bat
这种方法用的端口号是解压后的tar.gz包(E:\module-main-test\module-system\target\module-system-1.0.1\config\application.yaml里的端口号)