无法在build.xml中添加if块



我无法执行if任务。我的代码是:

<?xml version="1.0" ?>
<project default="main">
    <property name="buildsequence.property.file.fullpath" value="D:testantAntExample" />
    <target name="main" depends="compile, compress" description="Main target">
        <echo>
            Building the .jar file.
        </echo>
    </target>
    <target name="compile" description="Compilation target">
        <javac srcdir="src/org" destdir="src/org" fork="yes" executable="C:Program FilesJavajdk1.7.0_60binjavac"/>
    </target>
  <target name="compress" description="Compression target">
        <jar jarfile="Project.jar" basedir="src/org" includes="*.class" />
        <if>
            <available file="${buildsequence.property.file.fullpath}" />
            <then>              
                <echo message="File exist"/>
            </then>
            <else>
                <echo message="File do not exist" />                
            </else>
        </if>
  </target>
</project>

错误:

Buildfile: D:projectsSelfAntExamplebuild.xml
compile:
    [javac] Compiling 1 source file to D:projectsSelfAntExamplesrcorg
compress:
BUILD FAILED
D:projectsSelfAntExamplebuild.xml:19: 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.

注意:文件存在于D:testantAntExample

ifant-contrib的一部分,它必须存在于类路径中。下载后,您可以将其放在Ant lib文件夹(anthome/lib(中,然后您需要通过在构建文件的开头添加以下行来导入任务:

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

正如@manouti所解释的,if任务是一个外部扩展,而不是核心ANT的一部分。

以下示例下载丢失的jar并调用必要的taskdef语句:

<project default="runif">
  <target name="init" description="Download dependencies and setup tasks">
    <mkdir dir="${user.home}/.ant/lib"/>
    <get dest="${user.home}/.ant/lib/ant-contrib.jar" src="http://search.maven.org/remotecontent?filepath=ant-contrib/ant-contrib/1.0b3/ant-contrib-1.0b3.jar"/>
    <taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
  </target>
  <target name="runif" depends="init" description="Example running the ant-contrib if statement">
    <if>
      <available file="file.txt" />
      <then>              
        <echo message="File exist"/>
      </then>
      <else>
        <echo message="File do not exist" />                
      </else>
    </if>
  </target>
</project>

ant contrib扩展还支持更现代的ant lib机制。下面的示例演示了它如何使用名称空间并且不需要taskdef。

<project default="runif" xmlns:contrib="antlib:net.sf.antcontrib">
  <available classname="net.sf.antcontrib.logic.IfTask" property="if.task.exists"/>
  <target name="init" description="Download missing dependencies" unless="if.task.exists">
    <mkdir dir="${user.home}/.ant/lib"/>
    <get dest="${user.home}/.ant/lib/ant-contrib.jar" src="http://search.maven.org/remotecontent?filepath=ant-contrib/ant-contrib/1.0b3/ant-contrib-1.0b3.jar"/>
    <fail message="ant-contrib installed run the build again"/>
  </target>
  <target name="runif" depends="init" description="Example running the ant-contrib if statement">
    <contrib:if>
      <available file="file.txt" />
      <then>              
        <echo message="File exist"/>
      </then>
      <else>
        <echo message="File do not exist" />                
      </else>
    </contrib:if>
  </target>
</project>

相关内容

  • 没有找到相关文章

最新更新