简体   繁体   English

Spring 从 POM 中的 Maven 配置文件引导

[英]Spring Boot from Maven Profile in POM

I have a spring boot application that has dev and prod settings.我有一个具有 dev 和 prod 设置的 spring 引导应用程序。 Following multiple instructions found during searches I have an application.properties file which has:按照搜索过程中发现的多个说明,我有一个 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@

Then there are two files:然后有两个文件:

  • application-dev.properties应用程序-dev.properties
  • application.prod.properties应用程序.prod.properties

I then have a pom which has the spring-boot pom as a parent然后我有一个以 spring-boot pom 作为父级的 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>

I then set the profiles up as follows:然后我将配置文件设置如下:

<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>

If I then execute a build as follows:如果我然后按如下方式执行构建:

mvn -DskipTests -Pprod clean compile package

After doing this the application.properties file shows:执行此操作后,application.properties 文件显示:

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

Note that it did not use prod as requested but instead dev.请注意,它没有按要求使用 prod,而是使用 dev。 In fact it simply execute the activeByDefault profile not matter what I do.事实上,无论我做什么,它都只是执行 activeByDefault 配置文件。 Has anybody else seen this and have any ideas as to what is happening.有没有其他人看到这个并且对正在发生的事情有任何想法。 As you can imagine it is really annoying to have the deploy instructions say 'edit the POM file to move the activeByDefault property from the dev to prod profile'!正如您可以想象的那样,让部署说明说“编辑 POM 文件以将 activeByDefault 属性从 dev 移动到 prod 配置文件”真的很烦人!

Solved!解决了!

Eclipse is getting in the way; Eclipse 碍事; as the build happens eclipse is reinforcing the default profile;随着构建的发生,eclipse 正在加强默认配置文件; this happens in:这发生在:

Version: 2019-09 R (4.13.0) Build id: 20190917-1200版本:2019-09 R (4.13.0) 内部版本号:20190917-1200

If you close eclipse and run from the command line it works just fine.如果您关闭 eclipse 并从命令行运行它就可以了。

If you use eclipse then unchecking the Refresh on access under Preferences -> General -> Workspace then it works.如果您使用 eclipse 然后取消选中 Preferences -> General -> Workspace 下的 Refresh on access 则它可以工作。 The clue was from another stack overflow question: Eclipse + maven: profile ignored线索来自另一个堆栈溢出问题: Eclipse + maven:配置文件被忽略

Thanks to all who took the time to reply.感谢所有花时间回复的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM