Bug 59402

Summary: Order of attributes is significant in fileset
Product: Ant Reporter: Dave Glasser <dglasser>
Component: CoreAssignee: Ant Notifications List <notifications>
Status: RESOLVED FIXED    
Severity: major    
Priority: P2    
Version: 1.9.7   
Target Milestone: 1.9.8   
Hardware: PC   
OS: Windows NT   

Description Dave Glasser 2016-05-01 13:49:31 UTC
I discovered this in ant 1.6.5, and confirmed that it exists in 1.9.7.

If you have a <zipfileset> element with both a dir and a file attribute, a zip task 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 following  demonstration target:

  <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>
Comment 1 Stefan Bodewig 2016-05-22 15:17:58 UTC
With commit de5b405 you are no longer able to set file and dir at the same time, taking this undefined behavior away. In your case it depended on the order of attributes, but it may as well depend on the XML parser being used (as there is no defined order of XML attributes once the document has been read).

If you want to look for file1.txt in dir1 you need to use the includes attribute like in

<zipfileset includes="file1.txt" dir="dir1"/>

Fixed for 1.9.8 and 1.10.0