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

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

问题描述

我正在尝试使用Maven 3在Spring Boot应用程序中设置活动配置文件。

在我的pom.xml中,我设置默认活动配置文件和属性 spring .profiles.active 开发

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)


我还应该做些什么来使我的SpringBoot活动配置文件使用Maven配置文件?

任何帮助高度赞赏。

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天全站免登陆