gpt4 book ai didi

java - Maven如何设置java中可见的属性

转载 作者:行者123 更新时间:2023-11-30 04:00:40 25 4
gpt4 key购买 nike

这是我在 pom.xml 中的配置文件:

<!-- Production Profile -->
<profile>
<id>production</id>
<activation>
<property>
<name>profile</name>
<value>prod</value>
</property>
</activation>
<properties>
<build.profile.id>prod</build.profile.id>
</properties>

</profile>

<!-- Development Profile -->
<profile>
<id>development</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<build.profile.id>dev</build.profile.id>
</properties>
</profile>

<!-- Integration Test Profile -->
<!-- Use mvn -Dprofile=test -->
<profile>
<id>integration-test</id>
<activation>
<property>
<name>profile</name>
<value>test</value>
</property>
</activation>
<properties>
<!-- Used to locate the profile specific configuration file -->
<build.profile.id>test</build.profile.id>
<!-- Only integration tests are run. -->
<skip.integration.tests>false</skip.integration.tests>
<skip.unit.tests>true</skip.unit.tests>
</properties>
</profile>

接下来在我的 spring 上下文配置文件中我有:

@PropertySource("classpath:properties/app-${profile:dev}.properties")

所以这些技巧允许我使用不同的配置运行我的项目。我还有 3 个配置属性文件,该文件中有:

spring.profiles.active=integration-test
spring.profiles.active=prod
spring.profiles.active=dev

这个配置完美。 Activity 配置文件按照我的预期进行了更改,例如,当我运行 mvn clean install -Dprofile=test 时,我正在使用 app-test.properties 文件,并且我的 Activity 配置文件为 集成测试

但是这个技巧有一个问题。当有人运行 mvn install -Pintegration-test 时, Activity 配置文件是 dev 而不是integration-test。所以可以运行mvn install -Pintegration-test然后设置属性配置文件来测试??

最佳答案

您不应该在每个环境中使用 Maven 配置文件。使用 Maven,您可以构建 jar/war/ear。不同的环境不应该导致另一个 Artifact 。因此,您应该将配置拉到此 Artifact 之外。 How to pass value to maven pom.xml at rum time from java file?描述了如何解决这个问题的很多选项。

关于java - Maven如何设置java中可见的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22080489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com