Discussion:
Order of attributes signifificant in zipfileset?
Dave Glasser
2016-05-01 04:06:56 UTC
Permalink
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>
Stefan Bodewig
2016-05-01 04:30:35 UTC
Permalink
Post by Dave Glasser
I discovered this in ant 1.6.5, and found that it still behaves this way in 1.9.7.
<zipfileset file="file1.txt" dir="dir1"/>
<zipfileset dir="dir1" file="file1.txt"/>
It looks for file1.txt inside the current working directory.
I can confirm this to be true for any kind of fileset - the reason is
the way the file attribute is implemented in AbstractFileSet.
Internally file is translated to setting dir and includes, so whicherver
attribute is reached later it wins when setting the dir attribute.
Post by Dave Glasser
Is this a known, and expected behavior?  Is it documented anywhere?
At least I wasn't aware of it so far. I would have expected Ant to throw
a BuildException if you wanted to set both attributes at the same time
and would like to make it do just that for 1.9.8/1.10.0. Do you want to
create a bug report for this?

What you want to do for your build files is to set dir and includes
rather than dir and file.

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

Cheers

Stefan

---------------------------------------------------------------------
To unsubscribe, e-mail: user-***@ant.apache.org
For additional commands, e-mail: user-***@ant.apache.org
Dave Glasser
2016-05-01 13:55:18 UTC
Permalink
From: Stefan Bodewig <***@apache.org>
To: ***@ant.apache.org
Sent: Sunday, May 1, 2016 12:30 AM
Subject: Re: Order of attributes signifificant in zipfileset?
Post by Stefan Bodewig
At least I wasn't aware of it so far. I would have expected Ant to throw
a BuildException if you wanted to set both attributes at the same time
and would like to make it do just that for 1.9.8/1.10.0. Do you want to
create a bug report for this?
Stefan,
Thanks for the response. I just filed a bug report:
https://bz.apache.org/bugzilla/show_bug.cgi?id=59402
Dave


| |

Loading...