无法将 spring boot 应用程序设置为 run/build 特定配置文件

Having trouble getting springboot app to run/build specific profile

我正在使用 Maven 作为我的构建工具。 对于 SpringBoot 中的配置文件管理,我使用的是 yml 文件。

对于我的 SpringBoot 应用程序,我设置了以下 application-*.yml 文件:

application.yml
application-local.yml
application-foobar.yml

我对应的pom.xml配置文件配置:

<profiles>
    <profile>
        <id>local</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <activatedProperties>local</activatedProperties>
        </properties>
    </profile>
    <profile>
        <id>foobar</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <properties>
            <activatedProperties>foobar</activatedProperties>
        </properties>
    </profile>
</profiles>

每当我尝试通过 Maven 打包或 运行 我的应用程序时:

> mvn package -P <any configured profile>

> mvn spring-boot:run -P <any configured profile>

该应用程序 运行,但它仅 运行 退回到默认配置文件 (application.yml)。 每次我尝试 运行 我配置的任何配置文件下的应用程序时,我都会收到以下日志条目:

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

我似乎无法在 Internet 上找到有关此问题的任何明确信息。 任何帮助将不胜感激。谢谢!

Maven build profiles 和 Spring bean profiles 是两个完全不同的概念,它们碰巧使用相同的名称但彼此不交互。

您使用 <profiles></profiles> 显示的 XML 配置 Maven build profiles,但不会对 Spring bean 配置文件产生任何影响。

根据 Spring Framework documentation on activating a profile and Spring Boot documentation on passing arguments when running an application,您可以 select 活动 Spring bean 配置文件,当 运行 应用程序使用 Maven 和这样的命令时:

> mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Dspring.profiles.active=local"