Ant How to create a Java Project
By:Roy.LiuLast updated:2019-08-17
In this tutorial, we will show you how to use Ant build tool to manage a Java project, compile, and package it into a Jar file.
Technologies used :
- Eclipse 4.2
- Ant 1.9.4
- JDK 1.7
1. Create a Java Project
In Eclipse IDE, create a new Java project named “AntDateUtils”.
2. Java Source Code
Create a new Java class to print out the current date :
src/com/mkyong/core/utils/DateUtils.java
package com.mkyong.core.utils; import java.util.Date; public class DateUtils { public static void main(String[] args) { System.out.println(getLocalCurrentDate()); private static Date getLocalCurrentDate() { return new Date();
3. build.xml
Create a new build.xml in the project root folder, read comment for self-explanatory.
build.xml
<project name="AntJavaProject" default="main" basedir="."> <description> Create a Java Project (JAR) with Ant build script </description> <property name="projectName" value="DateUtils" /> <!-- Java sources --> <property name="src.dir" location="src" /> <!-- Java classes --> <property name="build.dir" location="bin" /> <!-- Output, Jar --> <property name="dist.dir" location="dist" /> <target name="init"> <!-- Create the time stamp --> <tstamp /> <!-- Create the build directory structure used by compile --> <mkdir dir="${build.dir}" /> </target> <target name="compile" depends="init" description="compile the source "> <!-- Compile the java code from ${src.dir} into ${build.dir} --> <javac includeantruntime="false" srcdir="${src.dir}" destdir="${build.dir}" /> </target> <target name="dist" depends="compile" description="package, output to JAR"> <!-- Create the distribution directory --> <mkdir dir="${dist.dir}" /> <!-- Put everything in ${build} into the {$projectName}-${DSTAMP}.jar file --> <jar jarfile="${dist.dir}/${projectName}-${DSTAMP}.jar" basedir="${build.dir}" > <manifest> <!-- create an executable Jar --> <attribute name="Main-Class" value="com.mkyong.core.utils.DateUtils" /> </manifest> </jar> </target> <target name="clean" description="clean up"> <delete dir="${build.dir}" /> <delete dir="${dist.dir}" /> </target> <!-- Default, run this --> <target name="main" depends="clean, compile, dist" /> </project>
4. Ant Build Scripts
Done, try few Ant’s commands
4.1 Compile the source code
$ ant compile
build.xml
<target name="compile" depends="init" description="compile the source "> <javac includeantruntime="false" srcdir="${src.dir}" destdir="${build.dir}" /> </target>
4.2 Package the project into an executable Jar file
$ ant dist
build.xml
<target name="dist" depends="compile" description="package, output to JAR"> <mkdir dir="${dist.dir}" /> <jar jarfile="${dist.dir}/${projectName}-${DSTAMP}.jar" basedir="${build.dir}"> <manifest> <attribute name="Main-Class" value="com.mkyong.core.utils.DateUtils" /> </manifest> </jar> </target>
4.3 Delete folders
$ ant clean
build.xml
<target name="clean" description="clean up"> <delete dir="${build.dir}" /> <delete dir="${dist.dir}" /> </target>
4.4 If no options, the default target will be executed, in this example, the default target is main
build.xml
<project name="AntJavaProject" default="main" basedir="."> ... <target name="main" depends="clean, compile, dist" />
$ ant
Output
Buildfile: /Users/mkyong/Documents/workspace/AntDateUtils/build.xml clean: [delete] Deleting directory /Users/mkyong/Documents/workspace/AntDateUtils/bin [delete] Deleting directory /Users/mkyong/Documents/workspace/AntDateUtils/dist init: [mkdir] Created dir: /Users/mkyong/Documents/workspace/AntDateUtils/bin compile: [javac] Compiling 1 source file to /Users/mkyong/Documents/workspace/AntDateUtils/bin dist: [mkdir] Created dir: /Users/mkyong/Documents/workspace/AntDateUtils/dist [jar] Building jar: /Users/mkyong/Documents/workspace/AntDateUtils/dist/DateUtils-20141030.jar main: BUILD SUCCESSFUL Total time: 1 second
Final Directory Structure
5. Test
5.1 Run a class inside a Jar file.
$ pwd /Users/mkyong/Documents/workspace/AntDateUtils $ java -cp dist/DateUtils-20141030.jar com.mkyong.core.utils.DateUtils Thu Oct 30 17:39:21 MYT 2014
5.2 Run the executable Jar file
$ pwd /Users/mkyong/Documents/workspace/AntDateUtils $ java -jar dist/DateUtils-20141030.jar Thu Oct 30 17:40:21 MYT 2014
References
From:一号门
COMMENTS