Pregunta

I'm trying to create 2 zip files as part of an ant task. Both are similar, but one includes some extra data. So to reduce code I'd like to define a fileset and reference it. The only problem is that produces an error.

Task:

<target name="create-zips" depends="cleanup, compile, package-jar">

    <zipfileset id="zipfiles">
        <zipfileset dir="${ant.project.name}-dist" includes="${jarname}.jar" prefix="${title}"/>
        <zipfileset dir="${scripts.dir}" includes="myprogram" filemode="755" prefix="${title}"/>
        <zipfileset dir="${scripts.dir}" includes="myprogram_gui" filemode="755" prefix="${title}"/>
        <zipfileset dir="${scripts.dir}" includes="myprogram_gui.command" filemode="755" prefix="${title}"/>
        <zipfileset dir="${scripts.dir}" includes="myprogram.bat" filemode="755" prefix="${title}"/>
        <zipfileset dir="${scripts.dir}" includes="myprogram_gui.bat" filemode="755" prefix="${title}"/>
        <zipfileset dir="${docs.dir}" includes="myprogram_readme.txt" prefix="${title}"/>
    </zipfileset>

    <zip destfile="${deploy.dir}/myprogram_${version}.zip" whenempty="fail" defaultexcludes="true">
         <zipfileset refid="zipfiles"/>
        <zipfileset dir="${otherdata.dir}" includes="other/*.*" prefix="${title}"/>
    </zip>
    <zip destfile="${deploy.dir}/myprogram_noother_${version}.zip" whenempty="fail" defaultexcludes="true">
        <zipfileset refid="zipfiles"/>
    </zip>
</target>

When task is run:

    create-zips:

BUILD FAILED
/path/to/my/buildfile/build-tools.xml:119: Problem opening /path/to/my/data/docs/myprogram_readme.txt

I've verified that the file exists. Removing the readme include just makes the error happen on a different file. Also, if I just copy and paste the zipfileset into 2 separate tags everything works fine. But I'd rather not do that.

edit: Apache Ant(TM) version 1.8.2 compiled on October 14 2011, Mac OS X 10.7. Haven't tried on any other platform.

¿Fue útil?

Solución

I was trying once to do the same, to no avail. A fileset does not accept nested fileset. When you try to cheat and use zipfileset as the outer set, ant considers its content as zip files. When ant tries to unzip your files, you get opening error.

A fileset must have dir attribute. This dir must be parent to all fileset components. Sad but true.

I ended up in workaround. Create X filesets. Those filesets may be specified as nested elements of a zip task. If in a given case you need less than X, then make a default for all X filesets, and zip always all X.

<!-- define X empty filesets -->
<fileset dir="c:\temp\1" includes="asdfasdf.neverhappens" id="nullfileset" />
<fileset id="zipfileset2" refid="nullfileset" />
<fileset id="zipfileset3" refid="nullfileset" />

<!-- here go real sets -->
<zipfileset dir="${scripts.dir}" includes="myprogram" />

Be careful about ant filesets. They are mutable. I wrote once a "question" about it, but someone deleted it. Well done quiet job, stack folks, "thanks" for that.

EDIT1: On Linux one could try dir="/" and use selectors nested inside a fileset. I was on Windows with my problem, so I couldn't do that. There's no "/" on Windows and I was gathering files from different drives.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top