springboot 项目默认打成一个 jar 包,在多环境时不友好,需要将依赖的第三方 jar 及 resources 目录下的配置文件打包到与 jar 包同级目录下,方便环境变更,具体操作如下:
打包到外部目录
-
pom.xml 需修改
<build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <appendAssemblyId>false</appendAssemblyId> <descriptors> <descriptor>src/main/assembly/assembly.xml</descriptor> </descriptors> <outputDirectory>${project.build.directory}/gwall/</outputDirectory> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> <!-- 打包成 jar 文件,并指定 lib 文件夹以及 resources 资源文件夹 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>com.xxl.job.admin.XxlJobAdminApplication</mainClass> <!-- 依赖前缀 --> <classpathPrefix>lib/</classpathPrefix> <addClasspath>true</addClasspath> </manifest> <manifestEntries> <Class-Path>resources/</Class-Path> </manifestEntries> </archive> </configuration> </plugin> </plugins> </build>
-
添加配置文件
assembly.xml 文件内容如下:<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>distribution</id> <!-- 输出格式 zip 最终结果生成 zip --> <formats> <format>tar.gz</format> </formats> <includeBaseDirectory>false</includeBaseDirectory> <!-- 设置需要输出文件 --> <fileSets> <fileSet> <directory>src/main/bin</directory> <outputDirectory>/</outputDirectory> <fileMode>0755</fileMode> </fileSet> <fileSet> <directory>src/main/resources/</directory> <outputDirectory>/resources</outputDirectory> <fileMode>0644</fileMode> </fileSet> </fileSets> <dependencySets> <dependencySet> <!-- 依赖包的输出目录 --> <outputDirectory>/lib</outputDirectory> <scope>runtime</scope> <excludes> <exclude>${project.groupId}:${project.artifactId}</exclude> </excludes> </dependencySet> <dependencySet> <!--jar 包的输出目录 --> <outputDirectory>/</outputDirectory> <includes> <include>${project.groupId}:${project.artifactId}</include> </includes> </dependencySet> </dependencySets></assembly>
start.sh 文件内容如下:
#!/bin/bash curpath=`pwd` app=xxl-job-admin-2.1.0.jar echo 'curpath:$curpath app:$app' ps -aux|grep $curpath/$app|grep -v grep|awk '{print $2}'|xargs -r kill -9 nohup java -jar $curpath/$app > $curpath/nohup.out 2>&1 &
start.bat 文件内容如下:
@echo off & setlocal enabledelayedexpansion java -jar xxl-job-admin-2.1.0.jar pause
最终结果如下:
注意问题
logback 不生效
主要是使用 logback-spring.xml
另外需要在 application.yml 中配置logging: config: config/logback-spring.xml
参见:
https://my.oschina.net/u/2022…