Create a new file called build.xml in /confucius - i.e., in the same directory where you have the HelloWorld.java
Type in (or copy-paste) the following in build.xml - these are the instructions for building HelloWorld.jar
<project name="HelloWorld">
<target name="init">
<mkdir dir="classes"/>
<mkdir dir="target"/>
</target>
<target name="compile" depends="init">
<javac srcdir="." destdir="classes">
<classpath>
<pathelement location="lib/log4j-1.2.16.jar"/>
</classpath>
</javac>
</target>
<target name="dist" depends="compile">
<jar jarfile="target/HelloWorld.jar" basedir="classes">
<manifest>
<attribute name="Main-Class" value="HelloWorld"/>
<attribute name="Class-Path" value="HelloWorld.jar ../lib/log4j-1.2.16.jar"/>
</manifest>
</jar>
</target>
</project>
Let us try and understand what we have written.
We created an Ant project called "HelloWorld".
We created three Ant 'targets' - init, compile and dist (short for distribution).
'dist' depends on 'compile' which depends on 'init' - this means that if you run 'dist', it will first run 'compile' which itself will first run 'init'.
In the 'init' we created two directories.
Note that if these directories already exist, Ant will not create them.
In the 'compile', we told Ant to compile everything in the current direcrtory and place the .class files in the 'classes' directory.
We also specified a classpath so it can find the log4j.jar
In the 'dist' we told Ant to jar all the .class files in /classes directory and place the jar in /target.
We also told it to add some things to the MANIFEST.MF
No comments:
Post a Comment