Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
650 views
in Technique[技术] by (71.8m points)

if statement - How to conditionally set arg value of exec task?

I've got an ant build script which I should modify. Specifically I should make a subversion checkout conditional: currently only the trunk gets checked out, the new version should checkout a given branch if needed.

<target name="do-svn-checkout" depends="init"
    <property name="branch" value=""/>
  <exec executable="svn">
    <arg value="checkout"/>
    <arg value="-r"/>
    <arg value="HEAD"/>
    <arg value="http://t01/java/trunk"/>
    <arg value="zzz"/>
    <arg value="--password"/>
    <arg value="xxx"/>
    <arg value="--username"/>
    <arg value="yyy"/>
  </exec>
</target>

The property branch will be set via the command line like for instance -Dbranch=mybranch.

If the property branch is empty, the trunk should be checked out, but if the property has any other value, the respective branch should be checked out, like http://t01/svn/hlfg/HLFG/java/branch/the-value-of-the-property. So depending on the property the respective arg-value of the svn call should be modified.

Is it possible to solve this with basic Ant or would I need to use an inline script?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When using Ant >= 1.9.3 it's a piece of cake with the new if/unless feature introduced with Ant 1.9.1 (but you should at least use Ant 1.9.3 because of bugs in Ant 1.9.1, see this answer for details)

Don't forget the namespaces to activate that feature, f.e. :

<project
  xmlns:if="ant:if"
  xmlns:unless="ant:unless"
>

 <property name="foobar" value=" "/>

 <echo if:blank="${foobar}">foobar blank !</echo>
 <echo unless:blank="${foobar}">foobar not blank !</echo>

</project>

in your case something like :

<target name="do-svn-checkout" depends="init"
 <property name="branch" value=""/>
 <exec executable="svn">
   <arg value="checkout"/>
   <arg value="-r"/>
   <arg value="HEAD"/>
   <arg value="http://t01/java/trunk" if:blank="${branch}">
   <arg value=".." unless:blank="${branch}">
   <arg value="zzz"/>
   <arg value="--password"/>
   <arg value="xxx"/>
   <arg value="--username"/>
   <arg value="yyy"/>
 </exec>
</target>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...