使用Ant条件标记



我想做一些类似的事情

<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

这是正确的做法吗?

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

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

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

只是一个建议。你可以在没有ant contrib的情况下使用if/excess对ant 1.9.1以后的版本做同样的事情(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>

相关内容

  • 没有找到相关文章

最新更新