How to conditionally set arg value of exec task?

后端 未结 2 1560
后悔当初
后悔当初 2020-12-19 02:42

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

相关标签:
2条回答
  • 2020-12-19 03:15

    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>
    
    0 讨论(0)
  • 2020-12-19 03:40

    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:
    
    0 讨论(0)
提交回复
热议问题