无法让Springboot应用程序运行/构建特定配置文件

问题描述

我正在使用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

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

解决方法

Maven构建配置文件和Spring bean配置文件是两个完全独立的概念,碰巧使用相同的名称,但彼此之间不交互。

您使用<profiles></profiles>显示的XML配置了Maven build profiles,但对Spring bean配置文件没有任何影响。

对于Spring Framework documentation on activating a profileSpring Boot documentation on passing arguments when running an application,在使用Maven通过以下命令运行应用程序时,可以选择活动的Spring bean配置文件:

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...