我有以下 Maven 配置文件配置
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
<property>
<name>spring.profiles.active</name>
<value>dev</value>
</property>
</activation>
</profile>
<profile>
<id>test</id>
<activation>
<property>
<name>spring.profiles.active</name>
<value>test</value>
</property>
</activation>
</profile>
</profiles>
我在application.properties
中有这个
spring.profiles.active=dev
所以dev
是我对 spring 和 Maven 配置文件的默认设置。
然后我就有了 Windows 系统变量 SPRING_PROFILES_ACTIVE
设置值为 test
明确使用测试配置文件。
根据文档,这应该覆盖 dev
我的属性文件中的值
69.5 Set the active Spring profiles
The Spring Environment has an API for this, but normally you would set a System property (spring.profiles.active) or an OS environment variable (SPRING_PROFILES_ACTIVE). E.g. launch your application with a -D argument (remember to put it before the main class or jar archive):
$ java -jar -Dspring.profiles.active=production demo-0.0.1-SNAPSHOT.jar
In Spring Boot you can also set the active profile in application.properties, e.g.
spring.profiles.active=production
A value set this way is replaced by the System property or environment variable setting, but not by the SpringApplicationBuilder.profiles() method. Thus the latter Java API can be used to augment the profiles without changing the defaults.
See Chapter 25, Profiles in the ‘Spring Boot features’ section for more information.
现在,当我运行 Spring 应用程序时,它使用 test
Spring 中的配置文件,但是 Maven 配置文件激活不会接收到此信息,并且仍在使用 dev
简介。
我尝试过设置 SPRING_PROFILES_ACTIVE
如 <property>
中的名称元素以确保这不是小写字母 .
的问题版本和大写 _
版本,但这没有帮助。
如果我在使用 -Dspring.profiles.active=test
运行 Maven 时提供变量,它确实可以工作。 .
非常感谢任何帮助。
编辑:显然,当我向 tomcat 部署 war 时,这也不起作用,其中 catalina.properties
包含spring.profiles.active=test
。同样的事情,Spring使用test
但 Maven 仍然停留在 dev
.
最佳答案
如何使用环境变量激活配置文件?
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
<property>
<name>spring.profiles.active</name>
<value>dev</value>
</property>
</activation>
</profile>
<profile>
<id>test</id>
<activation>
<property>
<name>env.SPRING_PROFILES_ACTIVE</name>
<value>test</value>
</property>
</activation>
</profile>
</profiles>
关于java - 从 Windows 系统变量激活 Maven 配置文件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48693617/