gpt4 book ai didi

java - 默认情况下,Maven 中是否可以同时激活多个配置文件?

转载 作者:行者123 更新时间:2023-11-30 06:44:52 27 4
gpt4 key购买 nike

我在 Maven 中有以下配置文件设置:

<profiles>
<profile>
<id>prod</id>
<properties>
<environment>prod</environment>
<serverAddress>https://example.com/v1</serverAddress>
<pubNubSubscribeKey>blah-blah</pubNubSubscribeKey>
<sentryDsn>https://username:password@sentry.io/id</sentryDsn>
</properties>
</profile>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<environment>dev</environment>
<serverAddress>http://localhost:8080/v1</serverAddress>
<pubNubSubscribeKey>blah blah</pubNubSubscribeKey>
<sentryDsn/>
</properties>
</profile>
<profile>
<id>win32</id>
<activation>
<os>
<family>Windows</family>
<arch>x86</arch>
</os>
</activation>
</profile>
<profile>
<id>win64</id>
<activation>
<os>
<family>Windows</family>
<arch>amd64</arch>
</os>
</activation>
</profile>
</profiles>

有四个配置文件,但两个架构之一和两个 prod/dev 配置文件之一应该同时处于 Activity 状态。 dev 配置文件默认配置为 Activity ,当 win64 由于激活条件而激活时,dev 配置文件变为非 Activity 状态。

有没有办法保持 dev 配置文件处于 Activity 状态,除非 prod 配置文件变为 Activity 状态?

本质上,我想要这样的逻辑:

------------------------
| | win32 | win64 |
------------------------
| dev | | X |
------------------------
| prod | | |
------------------------

我在 src/main/resources 中有一个 application.properties 模板,看起来像这样:

version=${version}
environment=${environment}
server.address=${serverAddress}
pubnub.keys.subscribe=${pubNubSubscribeKey}
sentry.dsn=${sentryDsn}

然后使用过滤器:

<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>application.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>

我用 proddev 的正确值填充它。如果没有发生,应用程序将无法启动。我无法在我的计算机上中继 setting.xmldev 的环境变量处于 Activity 状态,因为这意味着它不会在其他计算机上处​​于 Activity 状态并且它会非常令人困惑。

最佳答案

activeByDefault 是一个回退,如果没有其他配置文件被激活,它就会被激活。

要激活多个配置文件,您可以将它们添加到 settings.xmlactiveProfiles 部分 - 这对于本地开发设置配置文件到 dev 或其他机器上的 prod

您可以使用 CLI 参数(例如 -P 选项)或通过 System-Property-Activation 和 -Dpropname=value 来触发它。系统属性选项还有一个否定版本,如果属性未设置或没有特定值,则会触发配置文件。这很可能用于实现非此即彼的开关,如果未设置属性,则触发默认值:

<profile>
<id>dev</id>
<activation>
<property>
<name>!prod</name> <!-- deactivated if system property prod is set, otherwise activated -->
</property>
</activation>
</profile>
<profile>
<id>prod</id>
<activation>
<property>
<name>prod</name>
</property>
</activation>
</profile>

停用开发/激活产品

mvn ... -Dprod=true

除了这些选项之外,似乎没有直接的方法可以无条件地激活一个配置文件并结合另一个有条件触发的配置文件。

但是有一个小的解决方法可以有条件地激活一个配置文件,条件总是为真,那就是使用文件激活和一个总是存在的文件,比如 pom.xml 或 -如果你的 poms 位于不同的位置,. 也可以工作

<profile>
<id>dev</id>
<activation>
<file>
<exists>pom.xml</exists>
</file>
</activation>
</profile>

关于java - 默认情况下,Maven 中是否可以同时激活多个配置文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50004331/

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