maven可以动态更换配置文件,但是不同profile打包使用的命令不同, dev,test,prod就要打包3次, 如果项目是springboot的项目,就可以使用spring.profiles.active来进行一次打包,多个环节都可以运行。

 

下面就介绍下步骤:

(1), 创建application.properties(或者yml), 同时创建多个profile的application.properties文件,已  application-${profile}.properties的格式命名,如下图,创建了dev,test,prod这3个profile的配置文件

 

springboot中如何动态更换 配置文件  spring.profiles.active

 

(2),在各个profile的文件中设置属性,属性名保持一致

springboot中如何动态更换 配置文件  spring.profiles.active

(3),在application.properties(非profile的)中,设置默认激活的profile

springboot中如何动态更换 配置文件  spring.profiles.active

(4),在代码用使用@Value来获取配置文件中的属性

springboot中如何动态更换 配置文件  spring.profiles.active

(5),使用mvn clean package来打包,可以看到4个配置文件都被打包classpath路径下

springboot中如何动态更换 配置文件  spring.profiles.active

(6),使用java -jar命令来运行(不指定profile,默认是dev)

springboot中如何动态更换 配置文件  spring.profiles.active

 

(7),使用  java -jar  加上 --spring.profiles.active=test 来运行, 注意是2个-

springboot中如何动态更换 配置文件  spring.profiles.active

 

相关文章:

  • 2021-07-03
  • 2023-01-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-07
  • 2021-09-27
猜你喜欢
  • 2023-04-10
  • 2021-06-30
  • 2021-03-03
  • 2021-09-14
  • 2023-03-17
  • 2022-12-23
  • 2021-07-30
相关资源
相似解决方案