发布时间:
Application.yaml #
@profile.name@
从 Maven
文件读取配置,当使用指定环境 -P
参数时,打包时会替换 active
为对应配置文件
主配置文件 application.yaml
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
profiles:
active: '@profile.name@'
开发 application-dev.yaml
server:
port: 8081
测试 application-test.yaml
server:
port: 8082
生产 application-prod.yaml
server:
port: 8083
项目 pom 文件 pom.xml
Maven #
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.7.RELEASE</version>
<configuration>
<fork>true</fork>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
<!-- 开启 filtering 否则不会替换 application.yaml 中占位符号 -->
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<!-- 多环境配置 -->
<profiles>
<profile>
<id>dev</id>
<activation>
<!-- 默认开启开发环境 -->
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profile.name>dev</profile.name>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<profile.name>test</profile.name>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<profile.name>prod</profile.name>
</properties>
</profile>
</profiles>
命令打包 #
清除已编译内容
mvn -DskipTests=true clean
指定环境(-P)打包 dev
mvn -DskipTests=true package -P dev