how to catch a return value from a ssh task ant
I have defined a macro in ANT that checks if a dir exists on a remote linux box:
<macrodef name="checkIfDirExists">
<attribute name="host" />
<attribute name="username" />
<attribute name="password" />
<attribute name="dir" />
<sequential>
<runcommand executable="[ -d @{dir} ]" host="@{host}" username="@{username}" password="@{password}"/>
</sequential>
</macrodef>
runcommand
is just a wrapper macro for sshexec
task that validates some additional stuff, but basically it's just an sshexec
.
Right now,开发者_StackOverflow if i run this, it works in a way that if the directory exists the build go on but if it doesn't exist the build fails since [ -d @{dir} ]
return value is 1.
I want to be able to check the return value so i can put it in a conditional
tag, for example if the dir exists, skip, and if it doesn't create it with mkdir.
Is this possible ?
This is a total stab in the dark, I don't know if ant will let you do this. However if it's invoking bash on the remote host, it should work.
<macrodef name="checkIfDirExists">
<attribute name="host" />
<attribute name="username" />
<attribute name="password" />
<attribute name="dir" />
<sequential>
<runcommand executable="[ -d @{dir} ] || mkdir @{dir}" host="@{host}" username="@{username}" password="@{password}"/>
</sequential>
</macrodef>
This way if the directory exists it will short circuit and return success. If it doesn't exist, it will call mkdir. If mkdir fails, then ant will fail.
To get the exit code of the command, use a combination of resultproperty
and failonerror
:
<sshexec command="[ -d @{dir} ]" ... failonerror="false" resultproperty="exitCode"/>
The exit code will be in the property named exitCode
after this.
See Ant sshexec docs. Works for Ant 1.9.4 and higher.
精彩评论