springboot可以从 pom 文件中获取参数,这样可以 在打包时,根据环境(开发、测试)指定参数,就不用再 运行jar 包再指定参数了。

最常用的就是,激活配置文件参数:spring.profiles.active=activeProperty

用法:

1)springboot 配置文件

#propertis文件方式
spring.profiles.active=@activatedProperties@


#yaml文件方式
spring:
profiles:
active: @activatedProperties@


2)pom文件

 <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>



  1. 打包

# -P 后的 参数是 profile 中的 id。 默认是 activeByDefault 为 true的 dev
# 测试打包
# springboot 配置文件 application-test.properoties 或 application-test.yaml 生效
mvn clean package -P test

# 生产打包
# springboot 配置文件 application-prod.properoties 或 application-prod.yaml 生效
mvn clean package -P prod


以上演示了 pom 自定义参数 activatedProperties ,被 springboot读取的实例。