无法在build.xml中添加if-block

时间:2014-07-11 08:04:50

标签: ant build.xml ant-contrib

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

<?xml version="1.0" ?>
<project default="main">

    <property name="buildsequence.property.file.fullpath" value="D:\testant\AntExample" />

    <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 Files\Java\jdk1.7.0_60\bin\javac"/>
    </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:\projects\Self\AntExample\build.xml
compile:
    [javac] Compiling 1 source file to D:\projects\Self\AntExample\src\org
compress:

BUILD FAILED
D:\projects\Self\AntExample\build.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:\testant\AntExample

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

正如@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扩展程序还支持更现代的antlib机制。以下示例演示了它如何使用命名空间,并且不需要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>