Gos*_*sia 8 ant build ant-contrib
在我的ant脚本中,我想在满足条件时退出(停止执行构建)而不会失败.我试过用:
<if>
<equals arg1="${variable1}" arg2="${variable2}" />
<then>
<fail status="0" message="No change, exit" />
</then>
</if>
Run Code Online (Sandbox Code Playgroud)
Ant脚本在条件下停止,但构建失败.我希望停止构建但没有错误.我在Jenkins中使用"Invoke Ant"步骤.
谢谢.
Reb*_*bse 12
使用内置(自JDK 6)javascript引擎,不需要额外的库(antcontrib ..等)!
1. Java System.exit()
<script language="javascript">
self.log("Ending intentionally \n...\n..\n.");
java.lang.System.exit(0);
</script>
Run Code Online (Sandbox Code Playgroud)
你可能会在Eclipse中运行时失败BUILD:构建失败javax.script.ScriptException:sun.org.mozilla.javascript.internal.WrappedException:裹org.eclipse.ant.internal.launching.remote.AntSecurityException =>则使用Ant api而不是
2. Ant api
<script language="javascript">
self.log("Ending intentionally \n...\n..\n.");
project.fireBuildFinished(null);
</script>
Run Code Online (Sandbox Code Playgroud)
输出Eclipse,不打印BUILD SUCCESSFUL:
[script] Ending intentionally
[script] ...
[script] ..
[script] .
Total time: 410 milliseconds
Run Code Online (Sandbox Code Playgroud)
输出控制台,BUILD SUCCESSFUL打印2次:
[script] Ending intentionally
[script] ...
[script] ..
[script] .
BUILD SUCCESSFUL
Total time: 0 seconds
BUILD SUCCESSFUL
Total time: 0 seconds
Run Code Online (Sandbox Code Playgroud)
包装在macrodef中以供重用,fe:
<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>
Run Code Online (Sandbox Code Playgroud)
例子
<!-- compare int, default paramtype=math and when=true -->
<stopbuild param1="10" param2="11" condition="lt"/>
Run Code Online (Sandbox Code Playgroud)
输出:
[script] Stopping Build because 10 lt 11 is true !!
Run Code Online (Sandbox Code Playgroud)
<!-- compare strings, default when=true -->
<stopbuild param1="abc" param2="abcd" paramtype="foo" condition="lt"/>
Run Code Online (Sandbox Code Playgroud)
输出:
[script] Stopping Build because abc lt abcd is true !!
Run Code Online (Sandbox Code Playgroud)
Mrs*_*ang 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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12307 次 |
| 最近记录: |