Show List
Ant Build To Create a Jar File
Here is a sample Ant build to create Jar file.
Java class: HelloWorld.java
public class HelloWorld{public static void main(String args[]){System.out.println("Hello World");}}
build.xml
<?xml version="1.0" ?><project name="FirstProject" basedir="." default="run"><property file="build.properties"/><!-- description type --><description>Project to create jar file</description><target name="clean"><!-- Task to delete directory --><delete dir="${build.dir}"/></target><target name="compile" depends="clean"><!-- Task to create directory --><mkdir dir="${classes.dir}"/><!-- Task to compile java classes --><javac srcdir="${src.dir}" destdir="${classes.dir}"/></target><target name="jar" depends="compile"><!-- Task to create directory --><mkdir dir="${jar.dir}"/><!-- Task to create jar file with Main-Class assignment --><jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}" excludes="**/Test.class"><manifest><attribute name="Main-Class" value="${main-class}"/></manifest></jar></target><target name="run" depends="jar"><!-- Task to run the jar. Fork value true enables class execution in another JVM--><java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/></target></project>
build.properties
src.dir=srcbuild.dir=buildclasses.dir=${build.dir}/classesjar.dir=${build.dir}/jarmain-class=HelloWorldarchive.dir=archivebackup.dir=backuputil.dir=util
Output:
PS C:\Users\mail2\Downloads\Ant> ant Buildfile: C:\Users\mail2\Downloads\Ant\build.xml clean: [delete] Deleting directory C:\Users\mail2\Downloads\Ant\build compile: [mkdir] Created dir: C:\Users\mail2\Downloads\Ant\build\classes [javac] C:\Users\mail2\Downloads\Ant\build.xml:19: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds [javac] Compiling 2 source files to C:\Users\mail2\Downloads\Ant\build\classes jar: [mkdir] Created dir: C:\Users\mail2\Downloads\Ant\build\jar [jar] Building jar: C:\Users\mail2\Downloads\Ant\build\jar\FirstProject.jar run: [java] Hello World BUILD SUCCESSFUL
Source code:
https://github.com/it-code-lab/ant-build-to-create-a-jar-file
Leave a Comment