Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 837
sers Avatar asked Sep 16 '14 09:09

sers


2 Answers

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>
like image 116
Rebse Avatar answered Nov 16 '22 01:11

Rebse


You could define a wrapper target which depends on two other targets - one of which does trunk checkout, the other of which does branch checkout - and each of which is conditional on existence of your optional branch property.

You could further abstract the exec call into a macrodef to which you pass the trunk or branch url.

For example:

<project name="test" default="do-svn-checkout">

    <target name="do-svn-checkout" depends="do-svn-trunk-checkout, do-svn-branch-checkout"/>

    <target name="do-svn-trunk-checkout" unless="branch">
        <svn-checkout svn-url="http://t01/svn/java/trunk"/>
    </target>

    <target name="do-svn-branch-checkout" if="branch">
        <svn-checkout svn-url="http://t01/svn/hlfg/HLFG/java/branch/${branch}"/>
    </target>

    <macrodef name="svn-checkout">
        <attribute name="svn-url"/>
        <sequential>
            <echo message="svn-url=@{svn-url}"/>
        </sequential>
    </macrodef>

</project>

Output with no branch property defined:

do-svn-trunk-checkout:
     [echo] svn-url=http://t01/svn/java/trunk

do-svn-branch-checkout:

do-svn-checkout:

Output with branch property defined:

do-svn-trunk-checkout:

do-svn-branch-checkout:
     [echo] svn-url=http://t01/svn/hlfg/HLFG/java/branch/mybranch

do-svn-checkout:
like image 38
sudocode Avatar answered Nov 16 '22 02:11

sudocode