I discovered this in ant 1.6.5, and found that it still behaves this way in 
1.9.7.
If you have a <zipfileset> element with both a dir and a file attribute, it 
will produce different results depending on the order in which those attributes 
appear. If the file attribute appears first, it behaves as you would expect. 
For example, with this:
<zipfileset file="file1.txt" dir="dir1"/>
It looks for a file named "file1.txt" inside the directory "dir1". However, 
with this:
<zipfileset dir="dir1" file="file1.txt"/>
It looks for file1.txt inside the current working directory. This can be 
verified by running the demonstration target I provide below with the -debug 
switch, and reading the debug output.
I want to make clear that I'm aware that the docs for fileset say:
"Either dir or file must be specified" and that I might be doing it wrong. You 
could argue otherwise, but perhaps that does in fact unambiguously imply that 
having both is incorrect and hence the behavior is undefined. Be that as it 
may, what I would like to know is this:
Is this a known, and expected behavior?  Is it documented anywhere?
Should I assume that the order of attributes is significant in other places 
throughout my build.xml file?
Here's a self-contained target that demonstrates this behavior:
  <target name="oa">
    <property name="JARCMD" value="jar"/>

    <!-- Delete both zip files if they exist. -->
    <property name="ZIP1" value="zip1.zip"/>
    <property name="ZIP2" value="zip2.zip"/>
    <delete file="${ZIP1}"/>
    <delete file="${ZIP2}"/>

    <property name="DIR1" value="dir1"/>
    <mkdir dir="${DIR1}"/>

    <!-- make sure there are no files named file1.txt or file2.txt in the
         current directory, so we know they're not being read from there. -->
    <fail 
      message="A file named file1.txt exists in the current directory.">
      <condition><available file="file1.txt"/></condition>
    </fail>

    <fail 
      message="A file named file2.txt exists in the current directory.">
      <condition><available file="file2.txt"/></condition>
    </fail>

    <touch file="${DIR1}/file1.txt"/>
    <touch file="${DIR1}/file2.txt"/>

    <zip destfile="${ZIP1}">
      <zipfileset file="file1.txt" dir="${DIR1}"/>
      <zipfileset file="file2.txt" dir="${DIR1}"/>
    </zip>

    <zip destfile="${ZIP2}">
      <zipfileset file="file1.txt" dir="${DIR1}"/>

      <!-- This file will not be included, because the dir attribute 
           comes before the file attribute. -->
      <zipfileset dir="${DIR1}" file="file2.txt"/>
    </zip>

    <echo>Contents of ${ZIP1}:</echo>
    <exec dir="." executable="${JARCMD}">
      <arg value="-tvf"/>
      <arg value="${ZIP1}"/>
    </exec>

    <echo>Contents of ${ZIP2}:</echo>
    <exec dir="." executable="${JARCMD}">
      <arg value="-tvf"/>
      <arg value="${ZIP2}"/>
    </exec>
  </target>

Reply via email to