tags:

views:

36

answers:

3

Update: I am using Ant 1.8.1 on Windows XP.

I am trying to write an Ant master build file for multiple projects. I can successfully create a jar for each project, and I want to package all of these jars into a single tar.gz file. Each jar file is located within the bin subdirectory of its respective project, but this path could be changed in the future. I've tried something like this:

<tar destfile="foo.tar.gz" compression="gzip" >
   <tarfileset dir=".">
      <include name="**/*.jar" />
   </tarfileset>
</tar>

...which kind of works. The only problem is that it maintains the directory structure within the jar. I want a flat file. So instead of:

foo.tar.gz
   project1
      bin
         project1.jar
   project2
      bin
         project2.jar
etc...

I need:

foo.tar.gz
   project1.jar
   project2.jar

I attempted to use Ant's copy task to copy these jar files to a temporary directory and then tar them from there. However, the copy operation replicates paths within the target directory. So, same problem.

A: 

I think you're almost there, having assembled a flattened directory of your files. What remains is to get rid of the tarfileset element, which is there specifically for preserving/inserting structure. I think tar and zip tasks work fine with embedded fileset elements.

The documentation bears me out on this - up until 1.7 fileset was the embedded resource element that could be used in place of tarfileset.

Come to think of it, I think that once you switch to fileset, you may no longer need to temp-copy your files for flattening.


EDIT:

I just built and ran this buildfile:

<project name="tartest" default="teer">
   <target name="teer">
      <tar destfile="tartest.tar">
     <fileset dir="../Downloads" includes="*.ico *.gif"/>
      </tar>
   </target>
</project>

and there was no trace of the original directory in the archive:

-rw-r--r-- 0/0            3487 2009-12-28 20:04 actor075_thumb.gif
-rw-r--r-- 0/0            1456 2010-06-16 10:57 bea-logo.gif
-rw-r--r-- 0/0           23800 2010-06-16 09:22 ejb1.gif
-rw-r--r-- 0/0            1406 2010-05-15 10:40 favicon-beta.ico
-rw-r--r-- 0/0            1406 2010-05-15 10:25 favicon-clown.ico
-rw-r--r-- 0/0            1406 2008-12-06 01:08 favicon.ico

Also, tar -xvf tartest.tar dumped the tarred files in my current directory.

So... what are you doing differently that is causing different results for you?


EDIT 2:

More insight gained from re-reading your question. I'd completely sidestepped the problem of source paths generated by **/* wildcards.

Since you're already considering copying those jar files to a temporary directory, all that's missing is to use the flatten attribute on the copy task. That should fix you up.

Carl Smotricz
I tried switching to fileset instead of tarfileset, but I had the same results.
RobotNerd
I apologize for all the irrelevant advice and comments so far. I believe my 2nd update now has the info you need.
Carl Smotricz
Thanks! Flatten worked. I copied all jar files to a temporary directory using <copy todir="temp" flatten="true"> and tarred them from there.
RobotNerd
A: 

You might be able to work with zipFileSet to accomplish this. It Allows you to control the overall structure providing your own prefix (or no prefix).

<zip destfile="${dist}/product.zip">
    <zipfileset dir="bin/project1.jar" prefix="any/fake/path/you/want"/>
    <zipfileset dir="bin/project2.jar" prefix="any/fake/path/you/want"/>
    <zipfileset dir="some/other/path/project3.jar" prefix="any/fake/path/you/want"/>
</zip>

Will result in:

any/fake/path/you/want/project1.jar
any/fake/path/you/want/project2.jar
any/fake/path/you/want/project3.jar
Brian
I have to use a wildcard like "**/*.jar" because I have multiple subprojects (and more to be added in the future). I need to make it generic rather than manually adding every single one to the build file. I tried to create a zip file, which is almost identical to the original tar version I put above. It had the same result.I tried using the prefix attribute, but it merely appends the relative path of the file to the prefix I provide. E.g. prefix="test/out/" would create files within the archive with paths like "test/out/project1/bin/project1.tar" and so on.
RobotNerd
A: 

You may want to look at Ant's flattenmapper.

Alan Krueger
I looked at that too, but in some places ant's documentation is too cryptic to help in using the features. I wasn't able to fathom how to use a `flattenmapper` within a `tar` task so I opted for the simpler solution of using the `flatten` attribute of `copy`. Are you aware of more helpful documentation on ant tasks?
Carl Smotricz
I forgot, this is how it SHOULD work, but for some reason Ant doesn't allow you to use mappers with the `tar` task. You end up having to construct the files in roughly the same layout you need beforehand.
Alan Krueger