多环境打包部署
参考资料:
(180条消息) maven-assembly-plugin插件_dependencysets_Doctor舒畅的博客-CSDN博客
虽然前面的工程已经可以提供接口了,但是还不算完整的具备基础工程能力。
工程包含CICD,能够适配多环境配置和打出可用的二进制包才算完整
- 配置文件
- pom多profile配置
- 其他打包配置
- 常见启动命令
- 手工发包部署(本质上自动化程序只不过是手工上的封装而已)
config目录下增加配置文件
- application.properties
- application-sit.properties
也有一些喜欢使用yml,但是经过自动化发现,并不太好用。
- yml 层次分明,但是格式要求严格
- properties没有缩进问题,但是不太好归类
application.properties
spring.profiles.active=@active.profile@ server.servlet.context-path=/test server.port=10004 server.servlet.encoding.charset=utf-8
application-sit.properties暂时空白
启动:
配置已经生效了
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.3.8.RELEASE) 2023-05-03 19:54:00.553 INFO 12596 --- [ main] com.wht.test.TestAppStart : Starting TestAppStart on DESKTOP-TVKIOH9 with PID 12596 (D:\workspace\java\SpringBootWebTest\target\classes started by 13355 in D:\workspace\java\SpringBootWebTest) 2023-05-03 19:54:00.553 INFO 12596 --- [ main] com.wht.test.TestAppStart : No active profile set, falling back to default profiles: default 2023-05-03 19:54:01.708 INFO 12596 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 10004 (http) 2023-05-03 19:54:01.724 INFO 12596 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2023-05-03 19:54:01.724 INFO 12596 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.41] 2023-05-03 19:54:01.823 INFO 12596 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/test] : Initializing Spring embedded WebApplicationContext 2023-05-03 19:54:01.823 INFO 12596 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1222 ms 2023-05-03 19:54:01.978 INFO 12596 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2023-05-03 19:54:02.137 INFO 12596 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 10004 (http) with context path '/test' 2023-05-03 19:54:02.153 INFO 12596 --- [ main] com.wht.test.TestAppStart : Started TestAppStart in 2.027 seconds (JVM running for 2.509)
pom中体现多环境
这样就支持多环境了
<profiles> <profile> <id>sit</id> <properties> <profies.active>sit</profies.active> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> </profiles>
打包支持多环境
主要build这里有大量的插件,后续补充基础基础知识
<build> <finalName>test</finalName> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <excludes> <exclude>**/*.xlsx</exclude> <exclude>application.properties</exclude> <exclude>application-sit.properties</exclude> </excludes> </resource> <resource> <directory>src/main/resources</directory> <filtering>false</filtering> <includes> <include>**/*.xlsx</include> </includes> </resource> <resource> <directory>config</directory> <filtering>true</filtering> <includes> <include>**/*.xlsx</include> <include>application.properties</include> <include>application-sit.properties</include> </includes> <targetPath>${project.build.directory}/config</targetPath> </resource> </resources> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <configuration> <appendAssemblyId>false</appendAssemblyId> <descriptors> <descriptor>/src/main/assembly/descriptor.xml</descriptor> </descriptors> <encoding>UTF-8</encoding> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </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> <type>jar</type> <includeTypes>jar</includeTypes> <outputDirectory>${project.build.directory}/libs</outputDirectory> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> </plugin> </plugins> </build>
descriptor.xml配置
为了支持打出jar包和tar.gz包
<assembly> <id>descriptor</id> <formats> <format>tar.gz</format> </formats> <fileSets> <fileSet> <directory>./src/main/shell</directory> <outputDirectory>/libs</outputDirectory> <fileMode>755</fileMode> <includes> <include>*.sh</include> </includes> </fileSet> <fileSet> <directory>./target</directory> <outputDirectory>/libs</outputDirectory> <includes> <include>*.jar</include> </includes> </fileSet> <fileSet> <directory>./target/config</directory> <outputDirectory>/libs/config</outputDirectory> <includes> <include>*.properties</include> <include>*.ds</include> </includes> <directoryMode>0600</directoryMode> <fileMode>0600</fileMode> </fileSet> </fileSets> </assembly>
启动脚本control.sh
每个包中放一个启动脚本是否方便OS类部署
对于自动化部署可以参考
#!/bin/bash JAR_NAME=test.jar CURPATH=$(cd "$(dirname "$0")";pwd) cd $CURPATH pid=`ps -ef|grep ${JAR_NAME}|grep -V grep|grep -v kill |awk '{print $2}'` if [ ${pid} ]; then echo "strop ${JAR_NAME}....................." kill -9 $pid else echo "${JAR_NAME} is not running.................." fi sleep 2 pid=`ps -ef|grep ${JAR_NAME}|grep -V grep|grep -v kill |awk '{print $2}'` if [ ${pid} ]; then echo "${JAR_NAME} still running....................." else echo "${JAR_NAME} is not running.................." fi sleep 2 nohup java -jar -Dserver.port=1005 ${JAR_NAME} --spring.profiles.active=$1 >test.log 2>&1 & echo "start ${JAR_NAME} ..............." sleep 3 tail -1000f test.log
linux手工部署
访问验证
问题记录及处理
1、打包失败 lombak无法编译
mvn clean package -Psit
- lombak内部写死了很多虚拟机基础工具,他的编译依赖javac真正的版本
- mvn说白了立即窗口可以运行是因为他是外部软件,依赖windows path中指定的java版本
- windows中有个默认的oracle下的jre,可能是17版本,要从系统环境变量配置path中删除
- 配置自己的java版本的环境变量
- 再次运行 mv命令
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· 单线程的Redis速度为什么快?
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码