如何在maven中定义条件属性?
例如,如果有环境变量如何在maven中定义条件属性?,maven,maven-2,Maven,Maven 2,例如,如果有环境变量AAA,我希望将属性Configuration设置为${env:AAA},如果没有这样的环境变量,则将属性设置为其他常量值 在maven 2中我该怎么做?看起来好像你 环境 测试 ... 当环境变量定义为以下命令中的值test时,配置文件将被激活: mvn-Denvironment=test如果系统属性不可能被接受,您只需在POM文件中定义该属性,并在需要时进行覆盖: ... 你好 ... 您可以通过引用${foo.bar}在POM中的其他位置引用此属性。要在命令行上
AAA
,我希望将属性Configuration
设置为${env:AAA}
,如果没有这样的环境变量,则将属性设置为其他常量值
在maven 2中我该怎么做?看起来好像你
环境
测试
...
当环境变量定义为以下命令中的值test
时,配置文件将被激活:
mvn-Denvironment=test
如果系统属性不可能被接受,您只需在POM文件中定义该属性,并在需要时进行覆盖:
...
你好
...
您可以通过引用${foo.bar}
在POM中的其他位置引用此属性。要在命令行上重写,只需传递一个新值:
mvn-Dfoo.bar=再见。。。
您可以使用maven antrun插件有条件地设置属性。示例设置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>
org.apache.maven.plugins
maven antrun插件
1.8
验证
跑
真的
我不明白。你是说maven没有财产条件吗?如果我有10个属性,并且我希望允许通过环境覆盖每个属性,那么我应该定义多少个概要文件?取决于您的构建系统。如果这10个属性是互斥的,那么您似乎需要定义10个配置文件。但是,如果是这样的话,我建议您检查一下您的逻辑。为了测试它的缺失:!my.variable.which.can.be.missing
一个有用的替代方法是在调用mvn的任何位置执行该操作。例如,您的shell脚本中可能有更多的能力来有条件地设置环境变量。这就是我在读了这个答案后所做的。具体地说,我在我的gitlab.yml文件中添加了几行内容,导出了一个环境变量,然后通过-D标志将它们传递给mvn。所以您想有条件地设置属性吗?请注意,如果您试图在另一个maven antrun插件
中使用${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>