We have a whole bunch of stylesheets in our project. Unfortunately, our release build had the filenames hard coded in, i.e. it was only building specific stylesheets. So when somebody added new css files, they didn’t get included in the build.
There must be a way to automatically pick up all the css files and build them, we thought. And here it is….
This tip relies on the foreach and propertyregexp Ant tasks from the ant-contrib library. You can download the ant-contrib distro from here. I found the easiest way to make use of this library is to just copy ant-contrib.jar to your Ant lib directory.
In your build file, simply add the following line, which will enable Ant to access the new tasks:
<taskdef resource="net/sf/antcontrib/antlib.xml"/>
and here’s the interesting bit:
<target name="build-debug"> <foreach param="filename.css" target="css-build-debug"> <path> <fileset dir="${basedir}"> <include name="*.css"/> </fileset> </path> </foreach> </target> <target name="css-build-debug"> <echo>Building stylesheet ${filename.css} (DEBUG)</echo> <!-- Extract the base of the filename, i.e. for c:\Development\tls\...\v1.0\kn.css" we want "kn" --> <propertyregex property="basename" input="${filename.css}" regexp=".*\\(.*)\.css" select="\1"/> <java jar="${mxmlc.jar}" dir="${basedir}" failonerror="true" fork="true" maxmemory="256m"> <arg line="+flexlib='${flexsdk.frameworks.dir}'"/> <arg line="${filename.css}"/> <arg line="-output 'bin/${basename}.swf'"/> </java> </target>
The <foreach> task is provided by the Ant-contrib library – in this case it iterates through all the files picked out by the <fileset> task and passes them to the target in the property filename.css.
The <propertyregex> task is another one from the Ant-contrib library. This takes an input property, in this case filename.css, performs a regex search for a substring, and puts the result back into another property, basename.
The clever bit is the actual regular expression – all it really does is extract the filename without the extension from filename.css. We need this to be able to pass in the name of the output swf.
For those of you who love your regular expressions, here’s a synopsis of how it works:
…and that’s it….