java - 从 Windows 系统变量激活 Maven 配置文件不起作用

标签 java spring maven spring-boot

我有以下 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明确使用测试配置文件。
enter image description here

根据文档,这应该覆盖 dev我的属性文件中的值

http://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html

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/

相关文章:

java - 将 Selenium 作为预构建事件运行时,Visual Studio 2010 挂起

java - 新的抽象类和实现

java - 以编程方式为 FrameLayout 的子项设置 layout_gravity?

java - model.addAttribute() 参数

java - Spring Data MongoDB 审计不适用于嵌入式文档

java - tomcat web应用程序的主类

java - 是否有一种标准方法来验证数组中多个项目之间的约束?

android - 使用 Gradle 和 Android Studio 将所有模块 Artifact 安装到自定义 Maven 存储库

java - 使用 maven selenium 插件启动浏览器 session 时无法删除文件

java - 如何使用 Maven 构建同一项目的 2 个版本