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

标签 java spring maven spring-boot maven-profiles

我有一个具有开发和生产设置的 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

然后我有一个以 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>

然后我按如下方式设置配置文件:

<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 配置文件”真的很烦人!

最佳答案

解决了!

Eclipse 正在阻碍;当构建发生时,Eclipse 正在加强默认配置文件;这发生在:

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

如果你关闭 eclipse 并从命令行运行它就可以正常工作。

如果您使用 eclipse,然后取消选中“首选项”->“常规”->“工作区”下的“访问时刷新”,则它可以工作。线索来自另一个堆栈溢出问题:Eclipse + maven : profile ignored

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

关于java - 从 POM 中的 Maven 配置文件进行 Spring Boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58825129/

相关文章:

java - 捕获异常,重新抛出断言

java - 使用 Spring 从环境变量获取 map 属性

java - Log4j2 - 未找到 Spring 测试记录器

java - JPA2.1 - 调用返回 CLOB 的存储过程

java - GitLab CI 与 Maven 集成测试 - Java

java - Maven 和 gwt 项目

Java:如何从 PrintStream 读取数据?

Java 异常和 throw 子句

java - AVRO avdl文件生成

maven - Tomcat - Maven 插件 : run webapps but not current project