Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a jar file from source folder with build.xml

Tags:

java

jar

ant

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?

like image 629
Arya Avatar asked May 11 '12 00:05

Arya


People also ask

What is build xml in Java?

The build. xml file contains information that the wsgen Java Ant task uses to assemble Web services into Enterprise Application archive (*. ear) files. The following sections provide an example build.

How do I build an Ant build xml?

Create Ant build fileIn the Project tool window, select the directory, where the build file should be created. Right-click the directory and from the context menu, select New | File ( Alt+Insert ). In the New File dialog, specify the name of the new file with the xml extension, for example, build. xml.


1 Answers

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:

  • http://ant.apache.org/manual/Tasks/jar.html

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.

like image 163
jahroy Avatar answered Oct 15 '22 16:10

jahroy