Morgan Kenyon
Morgan Kenyon

Reputation: 3172

Apache Ant: adding and using a .txt file

I'm using Apache Ant to compile, jar and run a java project for school. I've run into a problem that I don't know how to include a single .txt file into my .jar, and then reference it as well in my java code.

Here is my compile and jar command in my build.xml.

    <target name="compile">
        <mkdir dir="build/classes"/>
        <javac srcdir="src" destdir="build/classes"/>
    </target>
    <target name="jar">
        <mkdir dir="build/jar"/>
        <jar destfile="build/jar/BinaryTree.jar" basedir="build/classes">
            <manifest>
                <attribute name="Main-Class" value="cs3345.MainLauncher"/>
            </manifest>
        </jar>
    </target>

What command do I use to include a .txt file into my jar. I've seen a couple Stack Overflow questions about it already, notably

adding non-code resources to jar file using Ant

How can I include data text files in a jar using Ant?

But none of them really explained how to include a file. They just had commands that didn't really make sense to me. I've tried,

        <jar destfile="build/jar/BinaryTree.jar" basedir="build/classes">
            <manifest>
                <attribute name="Main-Class" value="cs3345.MainLauncher"/>
            </manifest>
            <fileset dir="src/resources" />
        </jar>

But that didn't seem to help because I just don't understand Ant, I couldn't find documentation for it that actually explained why you do different things in ant.

My .txt file is in, src/resources.

Upvotes: 0

Views: 2487

Answers (3)

David W.
David W.

Reputation: 107030

The <jar> task tasks multiple filesets:

<jar destfile="build/jar/BinaryTree.jar" basedir="build/classes">
    <manifest>
        <attribute name="Main-Class" value="cs3345.MainLauncher"/>
    </manifest>
    <fileset dir="src/resources" />
</jar>

The <manifest> task is a sub entity. It's very much like the <manifest> task. In your jar is a file called META-INF/MANIFEST.MF and this is adding another attribute to that jar. This particular attribute tells the Jar what is the main class which should launch when the jar is exeuted.

The <fileset dir="src/resources"/> is adding all the files that are found in theresources` directory. What if you only want to add a particular file? You can use selectors. For example:

<jar destfile="build/jar/BinaryTree.jar" basedir="build/classes">
    <manifest>
        <attribute name="Main-Class" value="cs3345.MainLauncher"/>
    </manifest>
    <fileset dir="src/resources">
        <include name="foo.txt"/>
    </fileset>
</jar>

This time I'm not adding the entire resources directory tree. I'm just adding a single file called foo.txt into my jar file.

Upvotes: 2

Daniel Kaplan
Daniel Kaplan

Reputation: 67300

There is nothing in the documentation that says, "you can only add .class files". The fileset tag should work. Perhaps it's putting the file in a place you're not looking. Run in verbose mode to figure out what it's doing with your .txt file. To control the destination, you should use the zipfileset tag.


But that didn't seem to help. My .txt file is in, src/resources.

You're going to have to elaborate on that. How didn't it help? Is the problem that the file was nested under the zip or didn't go in there or you got an error?

Upvotes: 1

Maxim Shoustin
Maxim Shoustin

Reputation: 77904

Use <copy>:

<target name="copy" depends="blablabla" description="Copies additional files">
    <echo>Start Copying Files</echo>
    <copy file="./src/resources/a.txt" overwrite="true" todir="build/classes/"/>

    <echo>End Copying Files</echo>
</target>

As a side note

If we want to include some files under src folder, we can write under target something like:


Upvotes: 1

Related Questions