一,pom.xml配置
local代表本地环境
dev代表开发环境
pro代表生产环境
设置默认环境使用dev
<!--环境配置-->
<profiles>
<profile>
<id>local</id>
<properties>
<!-- 环境标识,需要与配置文件的名称相对应 -->
<activatedProperties>local</activatedProperties>
</properties>
</profile>
<profile>
<id>dev</id>
<properties>
<activatedProperties>dev</activatedProperties>
</properties>
<activation>
<!-- 默认环境 -->
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>pro</id>
<properties>
<activatedProperties>pro</activatedProperties>
</properties>
</profile>
</profiles>
<build>
<resources>
<resource>
<!--配置文件路径 -->
<directory>src/main/resources</directory> <!--这里对应项目存放配置文件的目录-->
<!--开启filtering功能 -->
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<!--创建一个自动可执行的jar或war文件 -->
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
二,yml文件配置
spring:
profiles:
active: @activatedProperties@
三,mvn编译命令
-P dev 指定使用dev配置
-P pro 指定使用pro配置
-P local 指定使用local配置
mvn clean install package -P pro -Dmaven.test.skip=true
如不指定 默认使用dev配置
mvn clean install package -Dmaven.test.skip=true
四,验证
启动后会输出使用的配置