仅当文件存在时,Ant任务才能运行Ant目标?

是否有一个ANT任务只有在给定的文件存在时才会执行一个块? 我有问题,我有一个通用的ant脚本,应该做一些特殊的处理,但只有当一个特定的configuration文件存在。

可用和条件

<target name="check-abc"> <available file="abc.txt" property="abc.present"/> </target> <target name="do-if-abc" depends="check-abc" if="abc.present"> ... </target> 

从编码的angular度来看,这可能会更有意义(可以用ant-contrib: http : //ant-contrib.sourceforge.net/ ):

 <target name="someTarget"> <if> <available file="abc.txt"/> <then> ... </then> <else> ... </else> </if> </target> 

自1.8.0以来,显然还有资源存在

http://ant.apache.org/manual/Tasks/conditions.html

testing资源是否存在。 自Ant 1.8.0以来

要testing的实际资源被指定为嵌套元素。

一个例子:

 <resourceexists> <file file="${file}"/> </resourceexists> 

我正要从上面这个问题的好答案中重新做一个例子,然后我发现了这个

从Ant 1.8.0开始,您可以使用属性扩展; true(或on或yes)将启用该项目,而false(或closures或否)将禁用它。 其他值仍被认为是属性名称,因此只有在定义了命名属性的情况下才能启用该项目。

与较旧的样式相比,这样可以提供更多的灵活性,因为您可以从命令行或父脚本中覆盖该条件:

 <target name="-check-use-file" unless="file.exists"> <available property="file.exists" file="some-file"/> </target> <target name="use-file" depends="-check-use-file" if="${file.exists}"> <!-- do something requiring that file... --> </target> <target name="lots-of-stuff" depends="use-file,other-unconditional-stuff"/> 

来自http://ant.apache.org/manual/properties.html#if+unless的ant手册;

希望这个例子对某些人有用。 他们没有使用资源存在,但大概你可以?

我认为它的价值引用这个相似的答案: https : //stackoverflow.com/a/5288804/64313

这是另一个快速解决scheme。 还有其他的变化可能使用<available>标签:

 # exit with failure if no files are found <property name="file" value="${some.path}/some.txt" /> <fail message="FILE NOT FOUND: ${file}"> <condition><not> <available file="${file}" /> </not></condition> </fail> 

检查使用文件名filter,如“DB _ * / ** / *。sql”

如果存在与通配符filter相​​对应的一个或多个文件,则执行操作的方法如下。 也就是说,你不知道文件的确切名称。

在这里,我们正在recursion地查找名为“ DB_ * ”的任何子目录中的“ * .sql ”文件。 您可以根据需要调整filter。

注意:Apache Ant 1.7及更高版本!

如果存在匹配的文件,则这是设置属性的目标:

 <target name="check_for_sql_files"> <condition property="sql_to_deploy"> <resourcecount when="greater" count="0"> <fileset dir="." includes="DB_*/**/*.sql"/> </resourcecount> </condition> </target> 

这是一个“条件”目标,只有在文件存在的情况下才会运行:

 <target name="do_stuff" depends="check_for_sql_files" if="sql_to_deploy"> <!-- Do stuff here --> </target> 

您可以通过命令使用名称与您需要的名称相同的文件列表进行操作。 比创build一个特殊的目标要容易和直接得多。 而且你不需要任何额外的工具,只需要纯粹的Ant。

  <delete> <fileset includes="name or names of file or files you need to delete"/> </delete> 

http://ant.apache.org/manual/Types/fileset.html