SpringBoot 项目的 Maven 多环境打包配置

时间:2022-07-28
本文章向大家介绍SpringBoot 项目的 Maven 多环境打包配置,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1. 在pom.xml文件中配置

<profiles>
	<profile>
		<id>dev</id>
		<activation>
			<activeByDefault>true</activeByDefault>
		</activation>
		<properties>
			<activatedProperties>dev</activatedProperties>
		</properties>
	</profile>
	<profile>
		<id>test</id>
		<properties>
			<activatedProperties>test</activatedProperties>
		</properties>
	</profile>
	<profile>
		<id>prod</id>
		<properties>
			<activatedProperties>prod</activatedProperties>
		</properties>
	</profile>
</profiles>

配置finalName打包后文件名中包含环境信息

<build>
	<finalName>springboot-demo-${activatedProperties}</finalName>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
	</plugins>
</build>

2. 在application.properties中配置当前激活的环境

spring.profiles.active=@activatedProperties@

3. 使用 mvn 命令进行打包

mvn clean package -P dev
mvn clean package -P test
mvn clean package -P prod

在 eclipse 中配置

4. 使用 java -jar 命令选择不同环境启动

java -jar project-demo.jar --spring.profiles.active=prod