【问题标题】:Using Ant condition tag使用 Ant 条件标签
【发布时间】:2013-08-07 17:12:00
【问题描述】:

我想做这样的事情

<target name="init" description="Create build directories.
    >
    <mkdir dir="bin" />
    <mkdir dir="dist" />
            <!-- Check for the presence of dependent JARs -->
            <condition property="dependent.files.available">
               <and>
                 <available file="../../xx.jar" />
                 <available file="../../yy.jar" />
               </and>
            </condition>
</target>

<!--- Compile -->
<target name="compile" depends="init" description="Compile java sources 
    and create classes">
         <if>
           <isset property="dependent.files.available"/>
             <then>
       <javac srcdir="src" destdir="bin" classpathref="ext_jar_classpath"/>
             </then>
             <else>
                <echo message="Either xx.jar or yy.jar not found"/>
             </else>
         </if>  
</target>

当我尝试编译代码时,它给了我以下错误

Problem: failed to create task or type if
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place

这是正确的做法吗?

【问题讨论】:

  • 给出的解决方案 here 肯定会对您有所帮助。

标签: java ant ant-contrib


【解决方案1】:

您需要让ant-contrib jar 在运行时可见,或者按照链接中的说明正确配置它。

事情归结为让Ant加载任务定义,所以如果你把ant-contrib放在ant/lib中,你只需要

<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>

【讨论】:

    【解决方案2】:

    只是一个建议。从 ant 1.9.1 开始,您可以使用 if/unless 而不使用 ant-contrib (http://ant.apache.org/manual/targets.html) 来做同样的事情

    你的目标是

    <target name="init" description="Create build directories.
        >
        <mkdir dir="bin" />
        <mkdir dir="dist" />
                <!-- Check for the presence of dependent JARs -->
                <condition property="dependent.files.available">
                   <and>
                     <available file="../../xx.jar" />
                     <available file="../../yy.jar" />
                   </and>
                </condition>
    </target>
    
    <!--- Compile -->
    <target name="compile" depends="init" description="Compile java sources 
        and create classes" if="dependent.files.available">
    
           <javac srcdir="src" destdir="bin" classpathref="ext_jar_classpath"/>
    
    </target>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-19
      • 2018-11-19
      • 1970-01-01
      • 1970-01-01
      • 2021-09-30
      • 1970-01-01
      • 2013-09-20
      • 2010-10-30
      相关资源
      最近更新 更多