Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the destination directory of Ant's fileset command?

I have an Ant buildfile for a Java library. It looks something like this:

<project ... ><target ... >
<jar destfile="C:\path\to\export.jar">
    <manifest> ... </manifest>
    <fileset dir="C:\path\to\bin" />
    <fileset dir="C:\path\to\src" />
    <fileset dir="C:\path\to\doc" />
    <zipfileset src="C:\path\to\included\library.jar" />
</jar>
</target></project>

The only problem is that my JavaDoc is being exported directly into the root directory of the resulting jar file. Essentialy, I'd like some equivalent of the <copydir> command that can be used inside the <jar> command.

My desired structure is this:

export.jar
  META-INF
    Manifest.MF
  com
    example
      whatever
        Blah.class
        Blah.java
  org
    external
      somelibrary
        Magic.class     // contents of the included library jar file
  doc
    // javadoc files here

The current structure is:

export.jar
  META-INF
    Manifest.MF
  com
    example
      whatever
        Blah.class
        Blah.java
        // some javadoc files here
  org
    external
      somelibrary
        Magic.class     // contents of the included library jar file
  // more javadoc files here

My current "solution" is to omit the documentation <fileset> command, then, once the jar has exported, go into Windows Explorer and right click7-ZipOpen Archive; I can then drop the doc directory in there just fine. However, this pretty completely defeats the purpose of Ant as a completely automated build system.

If it matters, this file was originally generated by Eclipse with the Runnable JAR exporter. However, I obviously need to modify it to add source files, etc. because it's a library and not actually a runnable jar. I exported it as a runnable jar to get Eclipse to package in the required libraries; apparently libraries on the build path aren't available for export via the standard FileExportJAR file.

like image 283
wchargin Avatar asked May 28 '13 04:05

wchargin


People also ask

What is FileSet in Ant?

A FileSet is a group of files. These files can be found in a directory tree starting in a base directory and are matched by patterns taken from a number of PatternSets and Selectors. PatternSets can be specified as nested <patternset> elements.

What does * stands for in * Test Java in ant?

**\*.sql means "in the given directory and inside all of its subdirectories, all the files that end with .sql"

How do I make an ant war?

Wrap the war task inside an Ant target (usually package) and run it. This will create the WAR file in the specified location. It is entirely possible to nest the classes, lib, metainf and webinf directors so that they live in scattered folders anywhere in the project structure.


1 Answers

A jar is actually like a zip file. Hence you can use a zipfileset. Its attribute prefix is what you are looking for.

The zipfileset command can accept either a zip file via src or a filesystem directory via dir. Using the latter, you can add the following command:

<zipfileset dir="C:\path\to\doc" prefix="doc" />

Also worth to note is that zipfileset supports all attributes of fileset. Thus if you want to include just a single file in a specific location you can use:

<zipfileset file="C:\path\to\doc\file.txt" prefix="doc" />

Further reading: http://ant.apache.org/manual/Types/zipfileset.html

like image 54
Nicolas Lalevée Avatar answered Oct 31 '22 00:10

Nicolas Lalevée