How to get all the filenames from a directory using ant - ant

Using ant I want to get all the filenames from a directory and create a property with value as comma separated file names.
Example: If we have 3 files in a directory (i.e. 1.txt, 2.txt, 3.txt) then we have to create a property & its value should be 1.txt,2.txt,3.txt.
Thanks,
Mansoor MD.

First, you need to create a path with the files:
<path>
<fileset dir="${dir.name}">
<include name="*"/>
</fileset>
</path>
This can be combined with a <pathconvert> task:
<pathconvert pathsep=","
property="my.files">
<path>
<fileset dir="${dir.name}">
<include name="*"/>
</fileset>
</path>
</pathconvert>
The property ${my.files} will contain a comma separated list of files.
If you prefer, you can do this in two steps:
<path id="mypath">
<fileset dir="${dir.name}">
<include name="*"/>
</fileset>
</path>
<pathconvert pathsep=","
property="my.files"
refid="mypath"/>
Word 'o Warning: It will also contain the full path to these files.

Related

I wan to unzip multiple files in a folder with the same name as the file name using Ant?

I have a folder with multiple .zip files. I want to unzip them in the same folder, with the name same as that of the file.
eg.
temp/product_file1.zip
temp/product_file2.zip
temp/product_file3.zip
Output expected:
temp/product_file1
temp/product_file2
temp/product_file2
How can I make a generic code using Ant?
The code which I am using now is able to only unzip the last file.
<path id="warFilePath">
<fileset dir="/temp">
<include name="product_*.zip"/>
</fileset>
</path>
<property name="warFile" refid="warFilePath" />
<basename property="warFilename" file="${warFile}" suffix=".zip" />
<unzip dest="temp/${warFilename}">
<fileset dir="/temp">
<include name="${warFilename}.zip"/>
</fileset>
</unzip>

Convert from full path to relative path

In ant, how can I convert the file path of a file to a relative path? Here is what I have so far that still keeps the full path for the file
<path id="build.classpath">
<fileset dir="../../lib">
<include name="*.jar"/>
</fileset>
</path>
<pathconvert property="mf.classpath" pathsep=" " refid="build.classpath">
<map from="${build.classpath}" to="lib/"/>
</pathconvert>
The solution to the problem is as follows
<path id="build.classpath">
<fileset dir="../../lib">
<include name="*.jar"/>
</fileset>
</path>
<pathconvert property="mf.classpath" refid="build.classpath" pathsep=" ">
<chainedmapper>
<flattenmapper/>
<globmapper from="*" to="../lib/*"/>
</chainedmapper>
</pathconvert>
Use the chain mapper to chain different mappings together, and flatten the string to just the file name. Then glob all the files and append the relative path to the front.

Delete fileset filenames, but in different directory

So I have a fileset containing files in one directory:
<fileset id="modules" dir="${modules.dir}">
<include name="core*.jar"/>
<include name="fileset*.jar"/>
<include name="upgrader*.jar"/>
<include name="hello*.jar"/>
</fileset>
However, these files are copied into the ${lib.dir}, i.e, the ${lib.dir} contains copies of core*.jar, fileset*.jar, etc.
How do I delete these copied files?
Also, please note I can't use external libraries like ant-contrib.
Use a PatternSet to define the set of names. Then reference that PatternSet in any number of FileSets.
<patternset id="module.patterns">
<include name="core*.jar"/>
<include name="fileset*.jar"/>
<include name="upgrader*.jar"/>
<include name="hello*.jar"/>
</patternset>
<fileset id="modules" dir="${modules.dir}" >
<patternset refid="module.patterns"/>
</fileset>
UPDATE:
Given your comment that you want only the original files, try this:
<pathconvert pathsep="," property="flattened.modules" refid="modules">
<mapper type="flatten" />
</pathconvert>
<filelist id="libmodules" dir="${lib.dir}" files="${flattened.modules}"/>

What's the difference between a nested path and fileset?

I have been googling for the "Differences between fileset and path" article for some time, but have found nothing useful.
For example, what is the difference between the following (say, there is a someDir directory, which contains .jar files and has no subdirectories):
<path id="somePathId">
<pathelement path="someDir"/>
</path>
<path id="someId">
<path refid="somePathId" />
</path>
and
<path id="someId">
<fileset dir="someDir">
<include name="*.*">
</fileset>
</path>
?
They are used in different situations.
fileset is used to specify a group of files. You can use selectors and patternsets to get only the files you want.
classpath is used to specify classpath references. classpath can be specified with a single jar (location="..."), a ; or : separated list of jars (path="...") or with nested resource collections (like fileset).
Also if you want to debug them, it is different:
<echo message="Build-path: ${toString:build-path}" />
vs
<property name="debug.classpath" refid="classpath"/>
<echo message="Classpath = ${debug.classpath}"/>
As for your scripts,
<path id="somePathId">
<pathelement location="someDir"/>
</path>
I did not test it but according to the documentation path= expects a ; or : separated list of jars. This is not the same as your second example.
The major difference between a <path> and a <fileset> is that in <fileset> you can specify if you want to include or exclude certain type of files (Basically, its a group of files within a path... not necessary all the files), for eg:
<path id="someId">
<fileset dir="someDir">
<include name="*.java">
<include name="*.properties">
</fileset>
</path>

How can I exclude files from a reference path in Ant?

In a project we have several source paths, so we defined a reference path for them:
<path id="de.his.path.srcpath">
<pathelement path="${de.his.dir.src.qis.java}"/>
<pathelement path="${de.his.dir.src.h1.java}"/>
...
</path>
Using the reference works fine in the <javac> tag:
<src refid="de.his.path.srcpath" />
In the next step, we have to copy non-java files to the classpath folder:
<copy todir="${de.his.dir.bin.classes}" overwrite="true">
<fileset refid="de.his.path.srcpath">
<exclude name="**/*.java" />
</fileset>
</copy>
Unfortunately, this does not work because "refid" and nested elements may not be mixed.
Is there a way I can get a set of all non-java files in my source path without copying the list of source paths into individual filesets?
Here's an option. First, use the pathconvert task to make a pattern suitable for generating a fileset:
<pathconvert pathsep="/**/*,"
refid="de.his.path.srcpath"
property="my_fileset_pattern">
<filtermapper>
<replacestring from="${basedir}/" to="" />
</filtermapper>
</pathconvert>
Next make the fileset from all the files in the paths, except the java sources. Note the trailing wildcard /**/* needed as pathconvert only does the wildcards within the list, not the one needed at the end:
<fileset dir="." id="my_fileset" includes="${my_fileset_pattern}/**/*" >
<exclude name="**/*.java" />
</fileset>
Then your copy task would be:
<copy todir="${de.his.dir.bin.classes}" overwrite="true" >
<fileset refid="my_fileset" />
</copy>
For portability, instead of hard-coding the unix wildcard /**/* you might consider using something like:
<property name="wildcard" value="${file.separator}**${file.separator}*" />

Resources