使用Ant生成逗号分隔的文件列表

时间:2022-02-28 16:58:20

I'm trying to create a comma-delimited list of files or directories under the current directory. For instance, suppose I have the following folder structure:

我正在尝试在当前目录下创建以逗号分隔的文件或目录列表。例如,假设我有以下文件夹结构:

Root
-- Directory1
-- Directory2
...

I want to generate a variable or property that contain "Directory1,Directory2." I've tried iterating (using ant-contrib "for" task) over a <dirset dir="." includes="*">, but this generates absolute paths; I've then extracted the file names using the "basename" task, however that in turn generates an output property. Since properties are immutable, what I get in practice is "Directory1,Directory1,..."

我想生成一个包含“Directory1,Directory2”的变量或属性。我已经尝试通过 ,但这会生成绝对路径;然后我使用“basename”任务提取文件名,但这又生成了一个输出属性。由于属性是不可变的,我在实践中得到的是“Directory1,Directory1,......”

Is there a saner way of doing this, or will I have to write a Java extension to do this for me?

有没有更好的方法来做到这一点,还是我必须编写Java扩展来为我做这个?

2 个解决方案

#1


The pathconvert task can be used to format a dirset with arbitrary separators:

pathconvert任务可用于使用任意分隔符格式化dirset:

<dirset id="dirs" dir="." includes="*"/>
<pathconvert dirsep="/" pathsep="," property="dirs" refid="dirs"/>
<echo message="${dirs}"/>

#2


Just confirming Jörn's answer was exactly what I needed (as a starting point) as well.

只是确认Jörn的答案正是我所需要的(作为起点)。

<dirset id="dirset.sandbox" dir="${sandbox.dir}" includes="*">
  <exclude name="output"/>
</dirset>
<pathconvert pathsep=" " property="dirs.sandbox" refid="dirset.sandbox">
  <mapper type="flatten"/>
</pathconvert>
<echo message="[*** the sandbox dir list is ${dirs.sandbox} ***]"/>

sandbox.dir is an absolute path similar to /root/build/workspace and contains several subdirectories. The output is a space-separated list of those directories.

sandbox.dir是一个类似于/ root / build / workspace的绝对路径,包含几个子目录。输出是这些目录的以空格分隔的列表。

#1


The pathconvert task can be used to format a dirset with arbitrary separators:

pathconvert任务可用于使用任意分隔符格式化dirset:

<dirset id="dirs" dir="." includes="*"/>
<pathconvert dirsep="/" pathsep="," property="dirs" refid="dirs"/>
<echo message="${dirs}"/>

#2


Just confirming Jörn's answer was exactly what I needed (as a starting point) as well.

只是确认Jörn的答案正是我所需要的(作为起点)。

<dirset id="dirset.sandbox" dir="${sandbox.dir}" includes="*">
  <exclude name="output"/>
</dirset>
<pathconvert pathsep=" " property="dirs.sandbox" refid="dirset.sandbox">
  <mapper type="flatten"/>
</pathconvert>
<echo message="[*** the sandbox dir list is ${dirs.sandbox} ***]"/>

sandbox.dir is an absolute path similar to /root/build/workspace and contains several subdirectories. The output is a space-separated list of those directories.

sandbox.dir是一个类似于/ root / build / workspace的绝对路径,包含几个子目录。输出是这些目录的以空格分隔的列表。