问题
For instance, I would like to have property Configuration
set to ${env:AAA}
if there is an environment variable AAA
and to some other constant value if there is no such environment variable.
How do I do it in maven 2?
回答1:
It appears as though you activate a profile conditionally...
<profiles>
<profile>
<activation>
<property>
<name>environment</name>
<value>test</value>
</property>
</activation>
...
</profile>
</profiles>
The profile will be activate when the environment variable is defined to the value test
as in the following command:
mvn ... -Denvironment=test
回答2:
On the off-chance that a system property is acceptable, you can simply define the property in your POM file and override when required:
<project>
...
<properties>
<foo.bar>hello</foo.bar>
</properties>
...
</project>
You can reference this property elsewhere in your POM by referring to ${foo.bar}
. To override on the command line, just pass a new value:
mvn -Dfoo.bar=goodbye ...
回答3:
You can set a property conditionally using maven-antrun-plugin. Example setting install.path
+ echoing the value:
<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>
来源:https://stackoverflow.com/questions/14430122/how-to-define-conditional-properties-in-maven