Create a jar file from source folder with build.xml

38,423

If the build.xml file doesn't already have a target that creates a jar file, you can read about the ant jar command here:

However, there's probably a good chance that the build file already does this for you.

You can run the build script by typing ant when you're in the directory that contains the build.xml file (after unpacking the jar).

Just for fun, here is an example of a simple ant target that compiles some code and creates a jar.

This target will compile every .java file in any folder named reports.

As you can see, most of the values are using variables defined elsewhere in the script, but hopefully you get the idea...

<target name="create-funky-jar" depends="some-other-funky-targets">
    <javac
      srcdir="${src.dir}"
      includes="**/reports/*.java"
      destdir="${build.classes.dir}"
      deprecation="${javac.deprecation}"
      source="${javac.source}"
      target="${javac.target}"
      includeantruntime="false">
      <classpath>
        <path path="${javac.classpath}:${j2ee.platform.classpath}"/>
      </classpath>
    </javac>

    <jar destfile="${dist.dir}/SomeFunkyJar.jar"
         basedir="${build.classes.dir}"
         includes="**/reports/*.class"/>
  </target>

The above was just created by modifying a build script generated by NetBeans.

You can run the above target by adding it to a build.xml file and typing the following from the command line:

ant create-funky-jar

Note: You'll have to define all the variables for it to actually work.

Share:
38,423
Arya
Author by

Arya

Updated on July 25, 2022

Comments

  • Arya
    Arya almost 2 years

    I have download an API which has the following structure:

    In the folder, there is a source folder and a build.xml file. How would I go about creating a jar out of this?