Generating a comma-delimited file list with Ant - java

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,..."
Is there a saner way of doing this, or will I have to write a Java extension to do this for me?

The pathconvert task can be used to format a dirset with arbitrary separators:
<dirset id="dirs" dir="." includes="*"/>
<pathconvert dirsep="/" pathsep="," property="dirs" refid="dirs"/>
<echo message="${dirs}"/>

Just confirming Jörn's answer was exactly what I needed (as a starting point) as well.
<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.

Related

ant fixcrlf include/exclude does not work

I am having little problem with fixcrlf from ant. When I try to compile for unix target with fixcrlf filter, it modifies all files in the target folder, no matter of includes and excludes. I expect it should not convert any files that are not in the includes patterns, or at least I could force some files to not be converted via excludes attribute. None of that I tried been able to exclude anything. Even some binary files are modified and as result screwed up.
According to fixcrlf documentation from apache site, these parameters should work like this:
includes: comma- or space-separated list of patterns of files that must be included.
excludes: comma- or space-separated list of patterns of files that must be excluded.
When I run ant with parameters -verbose and -debug, there is no information on that fixcrlf did.
<copy todir="target">
<fileset dir="source1"><exclude name="**/*.bak"/></fileset>
<fileset dir="source2"><exclude name="**/*.bak"/></fileset>
<filterchain>
<fixcrlf srcdir="${src}" includes="**/*.sh,**/*.properties" excludes="**/*.xml" eol="lf" />
</filterchain>
</copy>

ant uppercase vs lowercase characters in path

I have an ant target that copies a file from one location to another, say it is defined in the build.xml as:
${project}/some-component-ABC/lib/whatever.jar
But on the file system the actual path is
${project}/some-component-abc/lib/whatever.jar
No uppercase ABC in some-component-abc on the fliesystem...
This path will resolve in Windows (7) but not in Linux?
Why?
I'll figure out what to do with it, just wanted some background why the functional difference.
Per the following article: https://ubuntuforums.org/showthread.php?t=1227827
Linux is written in C, which is case sensitive. It makes for faster sorting.
Ant doesn't do anything fancy with paths - it just delegates down to the underlying filesystem. Windows' filesystem is not case-sensitive, while any linux filesystem I can think of is. So, in windows, some-component-abc and some-component-ABC are the same directory, while in linux they would not be.
As others have mentioned, for the most part Ant just stores strings for everything and relies on the OS's filesystem when it actually comes time to perform filesystem operations, so you will run into differences like this when running scripts in Linux vs Windows.
However, Ant does have tasks that manipulate path-like strings, and these can be used to groom references to files prior to using them. Here's an example:
~/test $ ls -1
build.xml
FILE
build.xml:
<project>
<pathconvert property="file">
<fileset dir="${basedir}" includes="file" casesensitive="false" />
</pathconvert>
<echo message="${file}" />
</project>
Output:
[echo] /home/me/test/FILE
Alternative approach (will return a path-like string relative to the fileset's dir attribute):
<project>
<fileset dir="${basedir}" includes="file" casesensitive="false" id="filepath" />
<property name="file" refid="filepath" />
<echo message="${file}" />
</project>
Output:
[echo] FILE

Using ant, find files matching a regular expression

I have a directory containing 2 sub directories one of this directory has a file called
dictionary_FR.properties
dictionary_EN.properties
I want to search for these files with ant
Thanks
<fileset dir="loc_of_dir_containing_the_sub_dirs" id="xyz">
<include name="**/dictionary_*.properties"/>
</fileset>
<pathconvert pathsep="," property="my_files" refid="xyz"/>
${my_files} will contain the address of all files named in the pattern dictionary_XX.properties.

ANT- Loading Multiple Java files from Several packages

I have several files scattered across several packages. I need to load the file one by one and perform operations using ANT but whenever I try, only one file gets loaded. For ex. I have 2 classes - com.abc.one.One and com.bcd.two.Two . The following script print both file name but only the first file as loaded file in both loop iterations
<target name="build" description="My Task">
<for param="file">
<path>
<fileset dir="C:\workspace\AntTest1" includes="**\*.java" />
</path>
<sequential>
<echo message="#{file}" />
<loadfile property="loadedFile" srcfile="#{file}" />
<echo message="${loadedFile}" />
</sequential>
</for>
</target>
I have tried searching the documentation but could not find the concise explanation on how to use loadfile task. I suspected that this might be because ant uses immutable string but could not get workaround. I tried to split the job by creating new target but that does not help me either. Any help is highly appreciated
Ant property can only be set once, and after it is set, it is immutable.
It has been some time since my Ant days, but perhaps the following solution can work: For each file, make an antcall call, with the file name as parameter. then, in the new target, load the file and perform your task. Notice that antcall can impact severely the runtime performance.

How to specify xml documents to transform with resource collection in ant?

I'm trying to use ant to use XSLT to preprocess three specific stylesheets in my project. The Ant documentation for the xslt task says that it should be able to accept any resource collection. Specifically, it says:
Use resource collections to specify resources that the stylesheet should be applied to. Use a nested mapper and the task's destdir attribute to specify the output files.
I've therefore tried to specify these stylesheets using a fileset, and use the fileset as a nested element in the xslt task, but so far this hasn't been working. Instead, what it will do is seemingly ignore the specified fileset, and scan the entire project for files ending in .xsl, apply the stylesheet to those, and name the output according to the logic specified in the mapper.
<fileset id="stylesheets-to-preprocess" dir="${basedir}">
<filename name="src/xslt/backends/js/StatePatternStatechartGenerator.xsl"/>
<filename name="src/xslt/backends/js/StateTableStatechartGenerator.xsl"/>
<filename name="src/xslt/backends/js/SwitchyardStatechartGenerator.xsl"/>
</fileset>
<!-- ... -->
<target name="preprocess-stylesheets" depends="init">
<xslt
classpathref="xslt-processor-classpath"
style="src/xslt/util/preprocess_import.xsl"
destdir="build"
scanincludeddirectories="false">
<fileset refid="stylesheets-to-preprocess"/>
<mapper>
<chainedmapper>
<flattenmapper/>
<globmapper from="*.xsl" to="*_combined.xsl"/>
</chainedmapper>
</mapper>
</xslt>
</target>
What I'd like is to restrict it so that only those files specified in the fileset are processed.
Removing the mapper, so that the fileset is the only nested element, will result in ant attempting to apply the transformation to every file, even those without xsl extensions, which inevitably fails when it tries to transform a non-xml document.
I'm using ant 1.7.1. Any guidance would be appreciated.
Your problem is being caused by the implicit fileset functionality. In order to use a nested fileset parameter you need to switch this feature off.
I'd also recommend using an "include" parameter within the fileset, much simpler, and avoids the need for a complicated mapper element (You must specify the extension of the generated files, otherwise it'll default to .html)
<target name="preprocess-stylesheets" depends="init">
<xslt
classpathref="xslt-processor-classpath"
style="src/xslt/util/preprocess_import.xsl"
destdir="build"
extension=".xsl"
useImplicitFileset="false"
>
<fileset dir="src/xslt/backends">
<include name="StatePatternStatechartGenerator.xsl"/>
<include name="StateTableStatechartGenerator.xsl"/>
<include name="SwitchyardStatechartGenerator.xsl"/>
</fileset>
</xslt>
</target>

Categories

Resources