1、准备配置文件

Maven项目多环境打包配置_spring


application.yml配置:

spring:
profiles:
#对应pom中的配置
active: @spring.profiles.active@

2、配置pom.xml

<properties>
<maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
</properties>

<build>
<!-- 打包名格式:项目名-环境-打包时间.jar -->
<finalName>${project.artifactId}-${spring.profiles.active}-${maven.build.timestamp}</finalName>
<plugins>
<!-- maven 打包插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--打包时允许添加本地jar包-->
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
<!-- maven 打包时跳过测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<!-- 添加打包时间戳,没有需求可以不加 -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>timestamp-property</id>
<goals>
<goal>timestamp-property</goal>
</goals>
<configuration>
<name>module.build.timestamp</name>
<pattern>${maven.build.timestamp.format}</pattern>
<locale>zh_CN</locale>
<timeZone>Asia/Shanghai</timeZone>
</configuration>
</execution>
</executions>
</plugin>
</plugins>

<resources>
<resource>
<directory>src/main/resources</directory>
<!--开启过滤,用指定的参数替换directory下的文件中的参数-->
<filtering>true</filtering>
</resource>
</resources>
</build>

<!-- maven多环境打包配置 -->
<profiles>
<!-- 开发环境 -->
<profile>
<id>dev</id>
<properties>
<spring.profiles.active>dev</spring.profiles.active>
</properties>
<!-- 设置为默认环境 -->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!-- 生产环境 -->
<profile>
<id>prod</id>
<properties>
<spring.profiles.active>prod</spring.profiles.active>
</properties>
</profile>
<!-- 测试环境 -->
<profile>
<id>test</id>
<properties>
<spring.profiles.active>test</spring.profiles.active>
</properties>
</profile>
</profiles>

配置完成后IDEA的maven会出现勾选框

Maven项目多环境打包配置_启动项_02

3、配置打包启动项

Maven项目多环境打包配置_spring_03


配置完成后运行对应环境的启动项打包对应环境的包

异常

配置完以后运行或者clean后运行,可能会出现​​Do not use @ for indentation​​ 异常,尝试切换配置后点击reload按钮后再运行。

Maven项目多环境打包配置_启动项_04