在我的ant脚本中,我想在满足条件时退出(停止执行构建)而不会失败。我试过用:
<if>
<equals arg1="${variable1}" arg2="${variable2}" />
<then>
<fail status="0" message="No change, exit" />
</then>
</if>
Ant脚本在条件下停止但构建失败。我希望停止构建但没有错误。我在Jenkins中使用“Invoke Ant”步骤。
感谢。
答案 0 :(得分:12)
使用builtin(自JDK 6开始)javascript引擎, 不需要额外的库(antcontrib等)!
的 1。 Java System.exit()
<script language="javascript">
self.log("Ending intentionally \n...\n..\n.");
java.lang.System.exit(0);
</script>
在Eclipse中运行时可能会出现BUILD FAILED: 建筑失败 javax.script.ScriptException:sun.org.mozilla.javascript.internal.WrappedException: 包装org.eclipse.ant.internal.launching.remote.AntSecurityException =&GT;然后使用Ant api
的 2。 Ant api
<script language="javascript">
self.log("Ending intentionally \n...\n..\n.");
project.fireBuildFinished(null);
</script>
输出Eclipse,不打印BUILD SUCCESSFUL:
[script] Ending intentionally
[script] ...
[script] ..
[script] .
Total time: 410 milliseconds
输出控制台,BUILD SUCCESSFUL打印2次:
[script] Ending intentionally
[script] ...
[script] ..
[script] .
BUILD SUCCESSFUL
Total time: 0 seconds
BUILD SUCCESSFUL
Total time: 0 seconds
包装一个macrodef以便重用,f.e。 :
<macrodef name="stopbuild">
<attribute name="condition"/>
<attribute name="paramtype" default="math"/>
<attribute name="param1"/>
<attribute name="param2"/>
<attribute name="when" default="true"/>
<sequential>
<script language="javascript">
falsecondition = false;
switch ("@{condition}")
{
case "lt" :
b = "@{paramtype}" == "math" ? parseInt("@{param1}") < parseInt("@{param2}") : "@{param1}" < "@{param2}";
break;
case "gt" :
b = "@{paramtype}" == "math" ? parseInt("@{param1}") > parseInt("@{param2}") : "@{param1}" > "@{param2}";
break;
case "eq" :
b = "@{paramtype}" == "math" ? parseInt("@{param1}") == parseInt("@{param2}") : "@{param1}" == "@{param2}";
break;
default:
self.log("Wrong condition : @{condition}, supported: lt, gt, eq");
falsecondition = true;
}
if(!falsecondition && b == java.lang.Boolean.valueOf("@{when}")) {
self.log("Stopping Build because @{param1} @{condition} @{param2} is @{when} !!");
java.lang.System.exit(0);
// alternatively use
//project.fireBuildFinished(null);
}
</script>
</sequential>
</macrodef>
实施例
<!-- compare int, default paramtype=math and when=true -->
<stopbuild param1="10" param2="11" condition="lt"/>
输出:
[script] Stopping Build because 10 lt 11 is true !!
<!-- compare strings, default when=true -->
<stopbuild param1="abc" param2="abcd" paramtype="foo" condition="lt"/>
输出:
[script] Stopping Build because abc lt abcd is true !!
答案 1 :(得分:10)
我建议通过重新考虑你的方法来重构你的蚂蚁脚本。如果您通过“在满足某个条件时执行构建”而不是“如果满足另一个条件时构建失败”来解决您的问题,则更容易实现:
<!-- add on top of your build file -->
<if>
<equals arg1="${variable1}" arg2="${variable2}" />
<then>
<property name="runBuild" value="true"/>
</then>
<else>
<property name="runBuild" value="false"/>
</else>
</if>
<!-- add to the target that shall be executed conditionally -->
<target name="myTarget" if="${runBuild}">
...
<!-- exit message as separate target -->
<target name="exitTarget" unless="${runBuild}">
<echo message="No Change, exit" />
</target>
答案 2 :(得分:0)
内部<fail>
抛出BuildException
以使构建过程失败,因此,只要您使用此任务,您将获得有错误的ant构建出口。
就我个人而言,我不知道是否还有“正常”退出构建的任何其他任务,但是自己编写一个并不难。
编写执行以下代码的任务应该有效......
public void execute() {
// do something as you like
System.exit(0);
}