【发布时间】:2023-04-01 11:07:01
【问题描述】:
我有一个具有 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
然后我有一个以 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 配置文件”真的很烦人!
【问题讨论】:
-
不要...请不要。这意味着您正在为不同的环境重建工件,并且基本上是将未经测试的文件提升到新环境。只需指定启动应用程序时要使用的配置文件。不要使用 maven 配置文件来设置弹簧配置文件(这些也是完全不同的东西!)。
-
那么我如何制作一个war文件以部署到prod中的tomcat服务器并在我的开发环境中运行tomcat实例?我无法告诉 tomcat 在启动时使用哪个配置文件。
-
我也不确定测试;当您进行测试时,您必须使用不同的应用程序属性来指向您的外部资源的测试副本,否则您将破坏生产。您不需要使用 Maven 配置文件来执行此操作,但它是同一件事 - 为 test/dev/prod 使用不同的应用程序属性文件。
-
是的,你可以。您可以将其包含在 server.xml 或特定于应用程序的 Context.xml 中。对于测试,您可以包含 @ActiveProfiles 或仅指定为上下文或环境变量。我强烈建议阅读有关配置文件的 Spring Boot 参考指南和 Spring 参考指南。
-
所以我编辑 tomcat 实例来定义要使用的 spring boot 配置文件?这看起来不像是单击部署?
标签: java spring maven spring-boot maven-profiles