简体   繁体   中英

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.

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 the resources` 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.

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:


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?

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