2013-08-21 48 views
1

我有一个场景,我需要将目录从一个位置移动到另一个位置。如果目标文件夹中存在相同的目录,我需要将目录名称重命名为oldname_1。 所以我写了一个片段如下:如果其他条件在蚂蚁任务产生错误

<target name="Move"> 
    <IF> 
     <available file="${output.dir}" type="dir" /> 
     <then> 
      <echo message="Directory exists" /> 
      <rename src="${output.dir}" dest="${output.dir}_1"/> 
      <property name="newdirectory" value="${dest}"/> 
     </then> 
    <ELSE> 
     <echo message="Directory does not exist" /> 
    </ELSE> 
    </IF> 
     <move file="${newdirectory}" todir="C:\reports" /> 
    </target> 

我得到的错误是:

 
Problem: failed to create task 
Cause: The name is undefined. 
Action: Check the spelling. 
Action: Check that any custom tasks/types have been declared. 
Action: Check that any/declarations have taken place. 
+0

你们又添加了必要的对于[ant-contrib](http://ant-contrib.sourceforge.net/),''?请记住,Ant没有内置的本地if/else构造。 –

+0

是的,我也尝试过。有另一个错误说:[[taskdef]无法从资源net/sf/antcontrib/antcontrib.properties加载定义,无法找到它。“ – user2677829

回答

0

的,如果/ else结构是不蚂蚁的本地部分开箱即用,它提供通过ant-contrib项目,您需要下载JAR并将相关的<taskdef>添加到您的构建文件中。例如,如果你下载ant-contrib-1.0b3.jar,并把它放在一个在同一目录下的build.xml命名build目录,那么你可以说

<taskdef resource="net/sf/antcontrib/antcontrib.properties"> 
    <classpath> 
    <pathelement location="build/ant-contrib-1.0b3.jar"/> 
    </classpath> 
</taskdef> 
2

正如Ian Roberts已经提到的,你需要的蚂蚁的Contrib罐子,然后设置<taskdef/>指向这个罐子。我强烈建议将其放入您的项目中,并将其检入您的版本控制系统。这样,当有人签出您的项目时,他们已经安装了Ant-Contib.jar

我的标准是把为构建所需的所有可选的罐子(编制要求不罐子)目录${basedir}/antlib,然后把每个可选罐子在自己的目录,所以我会把ant-contrib-1.0b3.jar${basedir}/antlib/antcontrib

然后我定义任务是这样的:

<property name="antlib.dir"  value="${basedir}/antlib"/> 

<taskdef resource="net/sf/antcontrib/antlib.xml"> 
    <classpath> 
     <fileset dir="${antlib.dir}/antcontrib"/> 
    </classpath> 
</taskdef> 

这样一来,如果你更新的jar文件到Ant的罐子的Contrib的新版本,你只需将其插入目录。您不必更新build.xml

另请注意,我使用net/sf/antcontrib/antlib.xml而不是net/sf/antcontrib/antcontrib.propertiesXML文件是你应该使用。这方面的指示在tasks page上,与安装指导下的main page不同。原因是XML文件具有<for>任务的正确定义,属性文件不具有。


然而,还有另一种方式来做到ifunless在蚂蚁1.9.1无需可选的jar文件。这些是新的If and Unless entity attributes

这些可以放在所有任务,或子实体,并且通常可以代替蚂蚁的Contrib if/else东西:

<target name="move"> 
    <available file="${output.dir}" type="dir" 
     property="output.dir.exists"/> 
    <echo message"Directory exists" 
     if:true="output.dir.exists"/> 
    <move file="${output.dir}" tofile="${output.dir}_1" 
     if:true="output.dir.exists"/> 
    <property name="newdirectory" value="${dest}" 
     if:true="output.dir.exists"/> 
    <echo message="Directory does not exists" 
     unless:true="output.dir.exists"/> 
    <move file="${newdirectory}" todir="C:\reports" /> 
</target> 

不那么干净如你的榜样。不过,我会改用目标名称if=unless=参数:

<target name="move.test"> 
    <available file="${output.dir}" type="dir" 
     property="output.dir.exists"/> 
</target> 

<target name="move" 
    depends="move.test, move.exists, move.does.not exists"> 
    <move file="${newdirectory}" todir="C:\reports" /> 
</target> 

<target name="move.exists" 
    if="output.dir.exists"> 
    <echo message="Directory exists" /> 
    <move file="${output.dir}" tofile="${output.dir}_1"/> 
    <property name="newdirectory" value="${dest}"/> 
</move.exists/> 

<target name="move.does.not.exists" 
    unless="output.dir.exists"/> 
    <echo message="Directory does not exist" /> 
</target> 

如果没有回声的一切,结构会有点清洁:

<target name="move.test"> 
    <available file="${output.dir}" type="dir" 
     property="output.dir.exists"/> 
</target> 

<target name="move" 
    depends="move.test, backup"> 
    <move file="${newdirectory}" todir="C:\reports" /> 
</target> 

<target name="backup" 
    if="output.dir.exists"> 
    <move file="${output.dir}" tofile="${output.dir}_1"/> 
    <property name="newdirectory" value="${dest}"/> 
</move.exists/>