正确的答案是:ant verifyParameters -DrestoreValue=false
例如:ant verifyParameters -Drestoreval=false
如果参数拼写错误,我想抛出一个错误,即使我传递了多个参数,它也应该捕获所有参数并抛出错误。
发布于 2017-07-18 04:12:37
不可能检查所有拼写错误的参数名称,因为每个参数名称本身都是一个有效的参数。拼写错误的可能性太多了。
但您可以检查是否设置了正确的参数,如果没有设置,则失败。
下面是一个例子。如果未设置参数restoreValue,则主目标default被失败的从属目标check-parameter屏蔽。
<project name="option-test" default="default">
<!--
This is the main target. It depends on target check-parameter which fails,
if parameter restoreValue is not set.
-->
<target name="default" depends="check-parameter">
<echo message="Start build ..." />
<echo message="restoreValue = ${restoreValue}" />
</target>
<!--
This helper target sets property parameterok to true, if restoreValue is set.
And to false, otherwise.
-->
<target name="check-is-set">
<condition property="parameterok">
<isset property="restoreValue"/>
</condition>
</target>
<!--
This target depends on target check-is-set, which calculates the parameterok property.
The unless attribute evaluates the parameterok property, so that the target body
is only excuted, if paramterok=false.
So the build fails only if parameter restoreValue is not set.
-->
<target name="check-parameter" unless="${parameterok}" depends="check-is-set">
<fail message="Parameter restoreValue not set!" />
</target>
发布于 2017-07-18 10:25:53
这实际上是可能的,但它有点老套,而且不是Ant的一个真正的特性。
Ant可以访问用户在属性sun.java.command中调用的命令。使用一些if正则表达式工作,可以根据需要创建一个验证命令的条件:
<fail>
<condition>
<or>
<not>
<matches
string="${sun.java.command}"
pattern=" -DrestoreValue[ =]"
/>
</not>
<matches
string="${sun.java.command}"
pattern=" -D.+ -D"
/>
</or>
</condition>
</fail>如果您只是将它放在Ant脚本中的目标之外的任何位置,假设您希望每次都运行此检查,那么这种方法应该可以工作。如果您只希望它针对某些目标运行,我建议创建一个仅包含此条件故障的新目标,并使相关目标依赖于它。
https://stackoverflow.com/questions/45151683
复制相似问题