ant任务中的if-else条件产生错误
我有一个场景,需要将目录从一个位置移动到另一个位置。如果目标文件夹中存在相同的目录,我需要将目录名重命名为oldname_1。 所以我写了一个片段如下:ant任务中的if-else条件产生错误,ant,build.xml,ant-contrib,Ant,Build.xml,Ant Contrib,我有一个场景,需要将目录从一个位置移动到另一个位置。如果目标文件夹中存在相同的目录,我需要将目录名重命名为oldname_1。 所以我写了一个片段如下: <target name="Move"> <IF> <available file="${output.dir}" type="dir" /> <then> <echo message="Directory exists" /&
<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.
问题:无法创建任务
原因:名称未定义。
措施:检查拼写。
操作:检查是否已声明任何自定义任务/类型。
措施:检查是否发生了任何/声明。
if/else构造不是Ant现成的本机部分,它是由项目提供的,您需要下载一个JAR并将相关的
添加到构建文件中。例如,如果您下载并将其放入名为build
的目录中,该目录与build.xml位于同一目录中,那么您可以说
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="build/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
如前所述,您需要Ant Contrib jar,然后将
设置为指向此jar。我强烈建议将它放在项目中,并将其检查到版本控制系统中。这样,当有人签出您的项目时,他们已经安装了Ant-Contib.jar
我的标准是将构建所需的所有可选jar(不是编译所需的jar)放在目录${basedir}/antlib
中,然后将每个可选jar放在自己的目录中,因此我将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>
不像你的例子那么干净。但是,我会在目标名称上使用if=
和除非=
参数:
<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/>
您是否为添加了必要的
?请记住,Ant没有内置本机if/else构造。是的,我也尝试过。另一个错误是“[taskdef]无法从资源网/sf/antcontrib/antcontrib.properties加载定义。找不到它。”
<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/>