我有我的build设置,以便我通过命令行传递我的变量:
mvn clean install -DsomeVariable=data
在我的 pom 中,我有:
<someTag>${someVariable}</someTag>
这工作正常,但我想确定 someVariable 是否未在命令行中指定,然后默认它以便我的脚本可以继续。
这可以在Maven中完成吗?
最佳答案
您可以在 properties
中指定默认属性值POM 文件的部分:
<properties>
<someVariable>myVariable</someVariable>
</properties>
如果要确保属性值为 永远 在命令行上提供,然后您可以使用 maven-enforcer-plugin。
这是一个显示如何强制系统属性存在的链接 -> http://maven.apache.org/enforcer/enforcer-rules/requireProperty.html
我将在此处逐字复制 XML,以防上述链接失效。
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.0.1</version>
<executions>
<execution>
<id>enforce-property</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireProperty>
<property>basedir</property>
<message>You must have a basedir!</message>
<regex>\d</regex>
<regexMessage>You must have a digit in your baseDir!</regexMessage>
</requireProperty>
<requireProperty>
<property>project.version</property>
<message>"Project version must be specified."</message>
<regex>(\d|-SNAPSHOT)$</regex>
<regexMessage>"Project version must end in a number or -SNAPSHOT."</regexMessage>
</requireProperty>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
关于maven - 如何在 Maven 中识别和设置缺少的环境属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8043313/