【发布时间】:2013-01-20 22:38:14
【问题描述】:
例如,如果有环境变量AAA,我想将属性Configuration 设置为${env:AAA},如果没有这样的环境变量,我希望设置为其他常量值。
如何在 maven 2 中做到这一点?
【问题讨论】:
-
所以你想有条件地设置一个属性?
例如,如果有环境变量AAA,我想将属性Configuration 设置为${env:AAA},如果没有这样的环境变量,我希望设置为其他常量值。
如何在 maven 2 中做到这一点?
【问题讨论】:
好像你activate a profile conditionally...
<profiles>
<profile>
<activation>
<property>
<name>environment</name>
<value>test</value>
</property>
</activation>
...
</profile>
</profiles>
当环境变量被定义为值test 时,配置文件将被激活,如以下命令所示:
mvn ... -Denvironment=test
【讨论】:
<name>!my.variable.which.could.be.missing</name>earlyandoften.wordpress.com/2011/02/09/disable-maven-profile
如果系统属性不太可能被接受,您可以简单地在 POM 文件中定义该属性并在需要时覆盖:
<project>
...
<properties>
<foo.bar>hello</foo.bar>
</properties>
...
</project>
您可以通过引用${foo.bar} 在 POM 的其他地方引用此属性。要在命令行上覆盖,只需传递一个新值:
mvn -Dfoo.bar=goodbye ...
【讨论】:
您可以使用 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>
【讨论】:
maven-antrun-plugin 中使用${install.path},它们必须具有相同的版本。