简体   繁体   中英

Create directory structures during Ant jar task

I want to create a jar with the following directory structure:

thejar.jar/
    classes/ --> where all classes go
    lib/ --> where all dependencies go
    res/ --> where all non-classpath resources go (scripts, etc.)
    META-INF/

Here's my ant task:

<jar destfile="dist/main/thejar.jar">
    <!-- Create the manifest -->
    <manifest>
        <!-- JAR should be sealed. -->
        <attribute name="Sealed" value="true" />
    </manifest>

    <!-- Copy main build directory to classes/ directory in JAR. -->
    <fileset dir="dist/main/classes" includes="build/main"/>

    <!-- Copy main library directory to lib/ directory in JAR. -->
    <fileset dir="dist/main/lib" includes="lib/main"/>

    <!-- Copy main resources directory to res/ in JAR. -->
    <fileset dir="dist/main/res" includes="res/main"/>
</jar>

If I am understanding this correctly, it should be:

  • Copying all the built (.class) files in build/main to dist/main/classes
  • Copying all lib/main dependencies to dist/main/lib
  • Copying all res/main files to dist/main/res
  • JARring up dist/main/* into thejar.jar

The JAR task executes without errors, but when I go to view the contents of thejar.jar I just see META-INF/ (none of the subdirectories I mentioned above).

What's going on here? Thanks in advance!

You want jar task to copy files in build/main into jar's dist/main/lib , but

<fileset dir="dist/main/classes" includes="build/main"/>

means to pack files in dist/main/classes/build/main into the jar file.

Take a look at the example from the Ant-Jar task doc:

<jar destfile="${dist}/lib/app.jar">
    <fileset dir="${build}/classes"
             excludes="**/Test.class"
    />
    <fileset dir="${src}/resources"/>
</jar>

(The code above) jars all files in the ${build}/classes directory and also in the ${src}/resources directory together into a file called app.jar in the ${dist}/lib directory.

To achieve your request, I think you can copy the classes, resources and dependencies with <copy> task into the directory structure that you want and then jar the directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM