ANT: Using conditional tags,

后端 未结 6 1525
生来不讨喜
生来不讨喜 2021-01-02 03:36

I would like to do something like this


    
        

        
相关标签:
6条回答
  • 2021-01-02 03:42

    Alternately, once can include the following line in your ANT script

    <taskdef resource="net/sf/antcontrib/antlib.xml" />
    

    As long as ant-contribs is in your path, nothing else is needed

    This solution is a bit cleaner, as one gains access to ALL the tags, not just the ones manually specified

    0 讨论(0)
  • 2021-01-02 03:48

    I got the same issue. I resolved in the following way. Sometimes this might be compatibility problem.

    • Right click on build.xml.
    • Go to "Run As" --> 2 Ant... Select Classpath tab check Ant Home version (Sometimes eclipse selects default ant version).
    • If the version listed is different, then change Ant Home Classpath to C:\XXXX\ant\X.X.X.
    • Finally click on the User Entries --> Add External JARS..--> add ant-contrib.x.x.jar form C:\XXXX\ant\X.X.X\ant-contrib\ directory.
    0 讨论(0)
  • 2021-01-02 03:50

    Same issue resolved in that way. I put at the start of the buid XML file the following lines:

    <taskdef resource="net/sf/antcontrib/antlib.xml">
      <classpath>
        <pathelement location="your/path/to/ant-contrib-${version}.jar" />
      </classpath>
    </taskdef>
    
    0 讨论(0)
  • 2021-01-02 03:54

    On the tasks tab for your Ant Runtime do you see the 'if' task?

    0 讨论(0)
  • 2021-01-02 03:59

    The standard ant way would be something like =

     <target name="check">
      <condition property="delbuild">
        <available file="${build}" type="dir"/>
      </condition>
     </target>
    
     <target name="delbuild" depends="check" if="delbuild">
     <delete dir="${build}"/>
        <!-- .. -->
     </target>
    

    A snippet with the Ant Plugin Flaka, a recent alternative to antcontrib. Installation in Eclipse as usual via
    Preferences | Ant | Runtime | Global Entries | ant-flaka-1.02.-1.02.jar =

    <project xmlns:fl="antlib:it.haefelinger.flaka">
      <!-- some standalone if construct -->
      <fl:when test=" '${build}'.isdir ">
        <delete dir="${build}"/>
      </fl:when>
    
        <!-- some if/then/else construct -->
        <fl:choose>
         <!-- if -->
         <when test=" '${buildtype}' eq 'prod' ">
            <!-- then -->
            <echo>..starting ProductionBuild</echo>
         </when>
         <when test=" '${buildtype}' eq 'test' ">
            <!-- then -->
        <echo>..starting TestBuild</echo>
       </when>
        <!-- else -->
         <otherwise>
          <fl:unless test="has.property.dummybuild">
           <fail message="No valid buildtype !, found => '${buildtype}'"/>
          </fl:unless>
            <echo>.. is DummyBuild</echo>
         </otherwise>
        </fl:choose>
    </project>
    

    output with ant -f build.xml -Dbuildtype=prod or
    ant -f build.xml -Dbuildtype=prod -Ddummybuild=whatever

    [echo] ..starting ProductionBuild
    

    output with typo => ant - build.xml -Dbuildtype=testt

    BUILD FAILED
    /home/rosebud/workspace/AntTest/build.xml:21: No valid buildtype !, found => 'testt'
    

    output with ant -f build.xml -Ddummybuild=whatever

    [echo] .. is DummyBuild
    
    0 讨论(0)
  • 2021-01-02 03:59

    export ANT_HOME=/path/to/ant/apache-ant-1.8.2 I couldn't get it to work until I had ANT_HOME set properly. Java kept picking another ant installed on the box.

    0 讨论(0)
提交回复
热议问题