如何在Maven中识别和设置缺少的环境属性? [英] How to identify and set a missing environment property in Maven?
问题描述
mvn clean install - DsomeVariable = data
在我的pom我有:
< someTag> $ {someVariable}< / someTag>
这工作正常,但是我想确定在命令行中是否没有指定someVariable,然后默认它,以便我的脚本可以继续。
可以在Maven中完成吗?
您可以在POM文件的属性
部分中指定默认属性值:
<性状>
< someVariable> myVariable< / someVariable>
< / properties>
如果要确保属性值始终为始终一个命令行,那么你可以使用maven-enforcer-plugin。
这是一个链接,显示如何强制执行系统属性存在 - > http://maven.apache.org/enforcer/enforcer-rules/requireProperty.html
我将在这里复制XML,以防上述错误。
<项目>
[...]
< build>
< plugins>
< plugin>
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-enforcecer-plugin< / artifactId>
< version> 1.0.1< / version>
<执行>
< execution>
< id> enforce-property< / id>
< goals>
< goal> enforce< / goal>
< / goals>
< configuration>
< rules>
< requireProperty>
< property> basedir< / property>
< message>你必须有一个基础!< / message>
< regex> \d< / regex>
< regexMessage>您的baseDir中必须有一个数字!< / regexMessage>
< / requireProperty>
< requireProperty>
< property> project.version< / property>
< message>必须指定项目版本。< / message>
< regex>(\d | -SNAPSHOT)$< / regex>
< regexMessage>项目版本必须以数字或-SNAPSHOT结尾。< / regexMessage>
< / requireProperty>
< / rules>
< fail> true< / fail>
< / configuration>
< / execution>
< / executions>
< / plugin>
< / plugins>
< / build>
[...]
< / project>
I have my build set-up so that I pass in my variable via the command line:
mvn clean install -DsomeVariable=data
In my pom I have:
<someTag>${someVariable}</someTag>
This works fine, but I would like to identify if someVariable is not specified on the command line, and then default it so that my script can continue.
Can this be done in Maven?
You can specify default property value in the properties
section of your POM file:
<properties>
<someVariable>myVariable</someVariable>
</properties>
If you want to make sure that the property value is ALWAYS supplied on a command line, then you can use maven-enforcer-plugin.
Here is a link that shows how to enforce system property presence -> http://maven.apache.org/enforcer/enforcer-rules/requireProperty.html
I'll just copy the XML verbatim here in case the above link goes bad.
<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中识别和设置缺少的环境属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!