Apr 29
Ant filterset breaks binary files
Posted by James Netherton | Thursday 29 April 2010 20:21 PM | In Ant
I encountered a problem with an Ant build that was some how corrupting a bunch of war files that were being copied into a working directory in order to build an RPM package. I couldn’t work out how this was happening until I noticed the <filterset> ant tag was being used.
Filtersets allow special tokens in text files to be replaced with a specified value. The Ant documentation on filtersets contains the following note:
When a filterset is used in an operation, the files are processed in text mode and the filters applied line by line. This means that the copy operations will typically corrupt binary files.
Here’s an example which demonstrates the problem……
A basic directory structure with a mixture of text and binary files:

The myText.txt file contains some tokens that we’ll get Ant to replace at build time using a filterset:
@FOO@ @BAR@
Now for the Ant script:
<?xml version="1.0"?>
<project default="init">
<property name="sourcedir" value="source"/>
<property name="targetdir" value="target"/>
<target name="init" depends="clean">
<mkdir dir="${targetdir}"/>
<copy todir="${targetdir}">
<fileset dir="${sourcedir}"/>
<filterset>
<filter token="FOO" value="Foo"/>
<filter token="BAR" value="Bar"/>
</filterset>
</copy>
</target>
<target name="clean">
<delete dir="${targetdir}"/>
</target>
</project>
Simple stuff here. I copy the contents of one directory (source) to another (target) whilst using a filterset to replace tokens FOO and BAR.
Run the script and as expected the tokens in myText.txt have been replaced with the text Foo and Bar. Now I try to extract the contents of any of my war files and I get a message like the one described below, proving that it has been corrupted:
missing 3232546307 bytes in zipfile
The simple fix for this is to apply the filterset only to the specific files that require it.