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.
Related
I have the below ant code which gives basename (for ex: abc_01142018.txt) of the file from location input.dir. When the file is available, it works as expected.But when there are no files in input.dir it is picking up a value which I am not passing. I am expecting it to be empty value.
I read about basename and its not mentioned anywhere about how basename works when the input parameter is empty.
<path id="ref-id">
<fileset dir="${input.dir}">
<include name="abc*.txt"/>
</fileset>
</path>
<property name="input.files" refid="ref-id"/>
<basename property="input.file" file="${input.files}" />
There's no need to use Ant's path type or basename task if all you want is a list of file names without parent directories. The fileset type already does this, so just reference its ID instead.
Example:
<target name="test">
<touch file="exists1.txt" />
<touch file="exists2.txt" />
<fileset dir="." id="exists">
<include name="exists*.txt" />
</fileset>
<fileset dir="." id="doesnotexist">
<include name="doesnotexist*.txt" />
</fileset>
<property name="exists" refid="exists" />
<property name="doesnotexist" refid="doesnotexist" />
<echo message="${exists}" />
<echo message="${doesnotexist}" />
</target>
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.
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}"/>
I have the following :
<dirset dir="../release">
<include name="*/src/**"/>
</dirset>
But I want the "*/src" lobbed out. How is this done? I haven't been able to figure out how to do this with <mapper/> or <pathconvert/>.
If I understand your question correctly, you just need to add an exclude, i.e.:
<dirset dir="../release">
<include name="*/src/**"/>
<exclude name="*/src"/>
</dirset>
... or perhaps:
<dirset dir="../release" id="ds">
<include name="*/src/**"/>
</dirset>
<pathconvert refid="ds" property="ds.prop">
<mapper type="regexp" from=".*/src/(.*)" to="\1" />
</pathconvert>
I have a populated fileset and I need to print the matching filenames into a text file.
I tried this:
<fileset id="myfileset" dir="../sounds">
<include name="*.wav" />
<include name="*.ogg" />
</fileset>
<property name="sounds" refid="myfileset" />
<echo file="sounds.txt">${sounds}</echo>
which prints all the files on a single line, separated by semicolons. I need to have one file per line. How can I do this without resorting to calling OS commands or writing Java code?
UPDATE:
Ah, should have been more specific - the list must not contain directories. I'm marking ChssPly76's as the accepted answer anyway, since the pathconvert command was exactly what I was missing. To strip the directories and list only the filenames, I used the "flatten" mapper.
Here is the script that I ended up with:
<fileset id="sounds_fileset" dir="../sound">
<include name="*.wav" />
<include name="*.ogg" />
</fileset>
<pathconvert pathsep="
" property="sounds" refid="sounds_fileset">
<mapper type="flatten" />
</pathconvert>
<echo file="sounds.txt">${sounds}</echo>
Use the PathConvert task:
<fileset id="myfileset" dir="../sounds">
<include name="*.wav" />
<include name="*.ogg" />
</fileset>
<pathconvert pathsep="${line.separator}" property="sounds" refid="myfileset">
<!-- Add this if you want the path stripped -->
<mapper>
<flattenmapper />
</mapper>
</pathconvert>
<echo file="sounds.txt">${sounds}</echo>
Since Ant 1.6 you can use toString:
<echo file="sounds.txt">${toString:myfileset}</echo>