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

pup*_*eno 6 java maven

我在 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>
Run Code Online (Sandbox Code Playgroud)

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

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

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

------------------------
|      | win32 | win64 |
------------------------
| dev  |       |   X   |
------------------------
| prod |       |       |
------------------------
Run Code Online (Sandbox Code Playgroud)

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

version=${version}
environment=${environment}
server.address=${serverAddress}
pubnub.keys.subscribe=${pubNubSubscribeKey}
sentry.dsn=${sentryDsn}
Run Code Online (Sandbox Code Playgroud)

然后使用过滤器:

<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>
Run Code Online (Sandbox Code Playgroud)

prod我用 或的正确值填充它dev。如果没有发生,应用程序将无法启动。我无法依赖我的计算机setting.xml或环境变量来dev激活它,因为这意味着它不会在其他计算机上处​​于活动状态,并且会非常混乱。

Ger*_*cke 6

activeByDefault是在没有激活其他配置文件的情况下激活的后备方案。

要激活多个配置文件,您可以将它们添加到activeProfiles您的部分- 这对于本地开发将配置文件设置到其他计算机或在其他计算机上settings.xml有意义。devprod

您可以使用 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>
Run Code Online (Sandbox Code Playgroud)

停用开发/激活产品

mvn ... -Dprod=true
Run Code Online (Sandbox Code Playgroud)

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

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

<profile>
    <id>dev</id>
    <activation>
       <file>
           <exists>pom.xml</exists>
       </file>
    </activation>
</profile>
Run Code Online (Sandbox Code Playgroud)