I have a build file that creates tar and zip containing the same resources.
<tar destfile="my.tar.gz" compression="gzip"> <tarfileset dir="somedir" prefix="some_prefix" filemode="xxx"> <include name="some_pattern"/> </tarfileset> </tar> <zip destfile="my.zip"> <zipfileset dir="somedir" prefix="some_prefix" filemode="xxx"> <include name="some_pattern"/> </zipfileset> </tar>
There are many included file sets in archives.
I would prefer to split duplication - to somehow define a set of files once and reuse them to build tar and zip.
TarFileSet and ZipFileSet are very similar (both extend ArchiveFileSet). I experimented with creating tar and zip from zipfilesets. It works. At least for now, you don't need functions specific to TarFileSet. (I also looked in the TarFileSet source and see that it has code for converting ArchiveFileset to TarFileSet, which explains why this works).
There is no way to define a set of archive file sets and reuse them.
You cannot successfully combine archive files with path or union . In this case, additional ArchiveFileSet attributes (such as a prefix) are discarded. (Using PatternSet does not work at all.)
So, the best I came up with is the following:
<project default="test"> <target name="test"> <zipfileset dir="test/1" filemode="755" prefix="test1" id="test1"/> <zipfileset dir="test/2" filemode="755" prefix="test2" id="test2"/> <do_tar destfile="test.tar.gz"> <filesets> <zipfileset refid="test1"/> <zipfileset refid="test2"/> </filesets> </do_tar> <do_zip destfile="test.zip"> <filesets> <zipfileset refid="test1"/> <zipfileset refid="test2"/> </filesets> </do_zip> </target> <macrodef name="do_tar"> <attribute name="destfile"/> <attribute name="compression" default="gzip"/> <element name="filesets"/> <sequential> <tar destfile="@{destfile}" compression="@{compression}"> <filesets/> </tar> </sequential> </macrodef> <macrodef name="do_zip"> <attribute name="destfile"/> <element name="filesets"/> <sequential> <zip destfile="@{destfile}"> <filesets/> </zip> </sequential> </macrodef> </project>
And really, I donβt think this is an improvement at all, because it is not reuse of sets of files.
The only thing I can think of is to create a custom typedef (writing Java code) for the "set of archive file sets" that the id in marcos will reference. But if it comes to this, I will probably just live with duplication in the build file.
Is there anything much cleaner than I miss?
My requirements are to use only Ant (not ant -contrib or other third-party extensions), but the answers that use them may be useful to others.