通过 Maven 在 SpringBoot 中配置活动配置文件 [英] Configure active profile in SpringBoot via Maven

查看:44
本文介绍了通过 Maven 在 SpringBoot 中配置活动配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Maven 3 在 Spring Boot 应用程序中设置活动配置文件.
在我的 pom.xml 中,我将 default active profile 和属性 spring.profiles.active 设置为 development:

I'm trying to set an active profile in Spring Boot application using Maven 3.
In my pom.xml I set default active profile and property spring.profiles.active to development:

<profiles>
    <profile>
        <id>development</id>
        <properties>
            <spring.profiles.active>development</spring.profiles.active>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
</profiles>

但每次运行我的应用程序时,我都会在日志中收到以下消息:

but every time I run my application, I receive the following message in logs:

No active profile set, falling back to default profiles: default

并且 SpringBoot 配置文件设置为默认值(读取 application.properties 而不是 application-development.properties)

我还应该怎么做才能使用 Maven 配置文件设置我的 SpringBoot 活动配置文件?
非常感谢任何帮助.

and the SpringBoot profile is set to default (reads application.properties instead application-development.properties)

What else should I do to have my SpringBoot active profile set using Maven profile?
Any help highly appreciated.

推荐答案

Maven 配置文件和 Spring 配置文件是两个完全不同的东西.您的 pom.xml 定义了 spring.profiles.active 变量,该变量在构建过程中可用,但在运行时不可用.这就是为什么只激活默认配置文件的原因.

The Maven profile and the Spring profile are two completely different things. Your pom.xml defines spring.profiles.active variable which is available in the build process, but not at runtime. That is why only the default profile is activated.

如何将 Maven 配置文件与 Spring 绑定?

您需要将构建变量传递给您的应用程序,以便它在启动时可用.

You need to pass the build variable to your application so that it is available when it is started.

  1. 在您的 application.properties 中定义一个占位符:

spring.profiles.active=@spring.profiles.active@

@spring.profiles.active@ 变量必须与 Maven 配置文件中声明的属性匹配.

The @spring.profiles.active@ variable must match the declared property from the Maven profile.

在 pom.xml 中启用资源过滤:

Enable resource filtering in you pom.xml:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    …
</build>

执行构建时,src/main/resources 目录中的所有文件都将由 Maven 处理,并且 application.properties 中的占位符将替换为您在 Maven 配置文件中定义的变量.

When the build is executed, all files in the src/main/resources directory will be processed by Maven and the placeholder in your application.properties will be replaced with the variable you defined in your Maven profile.

有关更多详细信息,您可以转到我的帖子我在这里描述了这个用例.

For more details you can go to my post where I described this use case.

这篇关于通过 Maven 在 SpringBoot 中配置活动配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆