从POM中的Maven配置文件进行Spring Boot

我有一个具有dev和prod设置的spring boot应用程序。遵循在搜索过程中找到的多条指令,我有一个application.properties文件,该文件具有:

#-----------this will load the prod or dev according to the @activatedProperties@ value which is set from the pom when built
spring.profiles.active=@activatedProperties@

然后有两个文件:

  • application-dev.properties
  • application.prod.properties

然后我有一个pom,它以spring-boot pom作为父项

<parent>
    <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

然后我将配置文件设置如下:

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <activatedProperties>dev</activatedProperties>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <activatedProperties>prod</activatedProperties>
        </properties>
    </profile>
</profiles>

如果我然后按如下所示执行构建:

mvn -DskipTests -Pprod clean compile package

完成此操作后,application.properties文件显示:

#-----------this will load the prod or dev according to the environment
spring.profiles.active=dev

请注意,它没有按要求使用prod,而是使用dev。实际上,无论我做什么,它都只是执行activeByDefault配置文件。有没有其他人看到这一点,对发生的事情有任何想法。正如您可以想象的那样,让部署说明说“编辑POM文件以将activeByDefault属性从dev移到prod配置文件”确实很烦人!

bonwemeters 回答:从POM中的Maven配置文件进行Spring Boot

解决了!

Eclipse正在阻碍您的发展;当构建发生时,eclipse增强了默认配置文件;发生在:

版本:2019-09 R(4.13.0) 版本号:20190917-1200

如果您关闭eclipse并从命令行运行,它将正常工作。

如果您使用eclipse,则取消选中“首选项”->“常规”->“工作区”下的“刷新访问权限”,那么它将起作用。提示来自另一个堆栈溢出问题:Eclipse + maven : profile ignored

感谢所有花时间回复的人。

本文链接:https://www.f2er.com/3114902.html

大家都在问