java 如何在 Ant 的可用命令中使用通配符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1073077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:03:37  来源:igfitidea点击:

How to use wildcard in Ant's Available command

javaant

提问by ianmayo

I'm using an Ant build script to collate my Eclipse-based application for distribution.

我正在使用 Ant 构建脚本来整理我的基于 Eclipse 的应用程序以进行分发。

One step of the build is to check that the correct libraries are present in the build folders. I currently use the Ant command for this. Unfortunately, I have to amend the script each time I switch to a new Eclipse build (since the version numbers will have updated).

构建的一个步骤是检查构建文件夹中是否存在正确的库。我目前为此使用 Ant 命令。不幸的是,每次切换到新的 Eclipse 版本时我都必须修改脚本(因为版本号会更新)。

I don't need to check the version numbers, I just need to check that the file's there.

我不需要检查版本号,我只需要检查文件是否在那里。

So, how do I check for:

那么,我如何检查:

org.eclipse.rcp_3.5.0.*

instead of:

代替:

org.eclipse.rcp_3.5.0.v20090519-9SA0FwxFv6x089WEf-TWh11

using Ant?

使用蚂蚁?

cheers, Ian

干杯,伊恩

回答by VonC

You mean, something like (based on the pathconvert task, after this idea):

你的意思是,类似于(基于pathconvert 任务,在这个想法之后):

<target name="checkEclipseRcp">
  <pathconvert property="foundRcp" setonempty="false" pathsep=" ">
    <path>
      <fileset dir="/folder/folder/eclipse"
               includes="org.eclipse.rcp_3.5.0.*" />
    </path>
  </pathconvert>
</target>

<target name="process" depends="checkEclipseRcp" if="foundRcp">
  <!-- do something -->
</target>

回答by Vadzim

A slightly shorter and more straightforward approach with resourcecountcondition:

使用资源计数条件的一种更短更直接的方法:

<target name="checkEclipseRcp">
    <condition property="foundRcp">
        <resourcecount when="greater" count="0">
            <fileset file="/folder/folder/eclipse/org.eclipse.rcp_3.5.0.*"/>
        </resourcecount>
    </condition>
</target>

<target name="process" depends="checkEclipseRcp" if="foundRcp">
  <!-- do something -->
</target>

回答by Generic Ratzlaugh

The pathconvert task is probably the preferred way to go in most cases. But it creates a little problem when the directory tree is very large and one uses the echoproperties task. With a very large directory tree, the string generated by pathconvert can be huge. Then echoproperties sprays the huge string, making the output more difficult to work with. I use a macrodef on Linux that creates a property set to "1" if there are files in the directory:

在大多数情况下,pathconvert 任务可能是首选方法。但是当目录树非常大并且使用 echoproperties 任务时,它会产生一个小问题。对于非常大的目录树,pathconvert 生成的字符串可能很大。然后 echoproperties 喷射巨大的字符串,使输出更难以处理。我在 Linux 上使用一个 macrodef,如果目录中有文件,它会创建一个设置为“1”的属性:

<macrodef name="chkDirContents" >
    <attribute name="propertyName" />
    <attribute name="dirPath" />
    <attribute name="propertyFile" />
    <sequential>
        <exec executable="sh" dir="." failonerror="false" >
            <arg value="-c" />
            <arg value='fyles=`ls -1 @{dirPath} | head -1` ; if [ "$fyles" != "" ] ; then echo @{propertyName}=1 > @{propertyFile} ; fi' />
        </exec>
    </sequential>
</macrodef>

<target name="test" >
    <tempfile destdir="." property="temp.file" deleteonexit="true" />
    <chkDirContents propertyName="files.exist" dirPath="./target_dir" propertyFile="${temp.file}" />
    <property file="${temp.file}" />

    <echoproperties/>
</target>

Executing the "test" target will generate the following echoproperties line if there are files in the ./target_dir/ directory:

如果 ./target_dir/ 目录中有文件,则执行“test”目标将生成以下 echoproperties 行:

[echoproperties] files.exist=1

What "test" does: It generates a temporary filename, ${temp.file}, that can later be used as a property file. It then executes the macrodef, which calls the shell to check the contents of the dirPath directory. If there are any files or directories in dirPath, it assigns the propertyName property a value of 1 in the temporary file. It then reads the file and sets the property given in the file. If the file is empty, no property is defined.

“测试”的作用:它生成一个临时文件名 ${temp.file},以后可以用作属性文件。然后它执行macrodef,它调用shell 来检查dirPath 目录的内容。如果 dirPath 中有任何文件或目录,它会在临时文件中为 propertyName 属性分配值 1。然后它读取文件并设置文件中给定的属性。如果文件为空,则不定义任何属性。

Note that the temporary file could be reused for subsequent calls of the macrodef if desired. On the other hand, of course, once a property is set, it is immutable.

请注意,如果需要,可以将临时文件重用于随后的宏定义调用。另一方面,当然,一旦设置了属性,它就是不可变的。