2014-07-11 24 views
0

我无法执行if任务。 我的代码是:无法在build.xml中添加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

+1

使用if任务不是ant方式。蚂蚁的方法是根据这个目标制定一个目标验证和其他目标,并且只有在'除非'属性被设置为真的情况下才会触发'if'或''。请参阅https://ant.apache.org/manual/targets.html –

回答

1

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

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

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

下面的示例下载缺少的罐子,并调用所需的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机制。以下示例演示了它如何使用名称空间并且不需要任务定义。

<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> 
+0

此代码表示无法加载[taskdef]无法加载资源net/sf/antcontrib/antlib.xml中的定义。它不能被发现。<?XML版本= “1.0”?> <项目默认为 “主”> \t \t <的taskdef资源= “网/平方英尺/ antcontrib/antlib.xml” \t \t CLASSPATH =” D:\ RTC \ eclipse_rtc4 \ plugins \ org.apache.ant_1.7.1.v20100518-1145 \ lib \ ant-contrib-0.6“/> \t \t \t 构建.jar文件。 user3616128

+0

@ user3616128我不是ant-contrib扩展的粉丝。如果你真的想使用它,我强烈建议你使用更现代的版本。 V0.6于10年前发布...(2004)。我怀疑这可能是你的问题。 –

+0

@ user3616128在我的示例中,我提供了ANT构建如何下载和安装最新版本的ant-contrib的示例。我发现它使构建更加便携。 –