18

例如,如果有环境变量,我想将属性Configuration设置为,如果没有这样的环境变量,我想将属性设置为其他常量值。${env:AAA}AAA

我如何在 Maven 2 中做到这一点?

4

3 回答 3

11

好像您有条件地激活了配置文件...

<profiles>
  <profile>
    <activation>
      <property>
        <name>environment</name>
        <value>test</value>
      </property>
    </activation>
    ...
  </profile>
</profiles>

当环境变量被定义为test以下命令中的值时,配置文件将被激活:

mvn ... -Denvironment=test

于 2013-01-20T22:46:07.043 回答
11

如果系统属性不太可能被接受,您可以简单地在 POM 文件中定义该属性并在需要时覆盖:

<project>
...
  <properties>
     <foo.bar>hello</foo.bar>
  </properties>
...
</project>

您可以在 POM 的其他地方通过引用来引用此属性${foo.bar}。要在命令行上覆盖,只需传递一个新值:

mvn -Dfoo.bar=goodbye ...
于 2013-01-21T14:53:59.793 回答
10

您可以使用 maven-antrun-plugin 有条件地设置属性。示例设置install.path+ 回显值:

<plugin>
    <!-- Workaround maven not being able to set a property conditionally based on environment variable -->
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <phase>validate</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <exportAntProperties>true</exportAntProperties>
                <target>
                    <property environment="env"/>
                    <condition property="install.path" value="${env.INSTALL_HOME}" else="C:\default-install-home">
                        <isset property="env.INSTALL_HOME" />
                    </condition>
                    <echo message="${install.path}"/>
                </target>
            </configuration>
        </execution>
    </executions>
</plugin>
于 2016-04-21T13:15:25.053 回答