Java Ant tutorialsFree and quality Java programming tutorials, articles, guides, code examples, sample projects and best practiceshttps://www.codejava.net/tools/ant2024-05-20T07:25:42-05:00Joomla! - Open Source Content ManagementJava Ant Tutorial for beginner2020-02-17T16:03:49-06:002020-02-17T16:03:49-06:00https://www.codejava.net/tools/ant/java-ant-tutorial-for-beginnerNam Ha Minhhainatu@gmail.com<div class="feed-description"><p>Throughout this Ant tutorial, I will walk you through the process of developing a Java project using Ant build, step by step. After finishing this lesson, you will be able to wrote Ant script for a standard build of a Java project. And based on that, you will be able to modify Ant build script of an existing project and customize the build process when needed.</p> <p>To follow this tutorial, please use a text editor (<a href="coding/how-to-compile-and-run-a-java-program-with-textpad" target="_blank">Textpad</a> or <a href="coding/how-to-compile-and-run-a-java-program-with-sublime-text-3" target="_blank">Sublime</a>) with Command line prompt (on Windows) or Terminal (on Linux).</p> <p>&nbsp;</p> <h2>1. Download and setup Apache Ant</h2> <p>Go to <a href="https://ant.apache.org/bindownload.cgi" rel="nofollow" target="_blank">https://ant.apache.org/bindownload.cgi</a> to download the latest binary distribution of Apache Ant.</p> <p>When I’m writing this, the latest release is Ant 1.10.7 which requires minimum of Java 8 at runtime. Download the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">apache-ant-1.10.7-bin.zip</span> file and extract it on your hard drive. Then update the system environment variable PATH to include a path to <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">apache-ant-1.10.7\bin</span> directory. On Windows, you can type the following command in a Command Prompt window launched with administrator privilege:</p> <pre class="brush:text">setx -m PATH "%PATH%;apache-ant-1.10.8\bin"</pre> <p>Then open another command prompt, type <span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">ant -version</span></strong></span> and hit Enter. You should see the following output:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/tools/ant/beginner/ant-version.png" alt="ant-version" /></p> <p>That means Apache Ant was installed successfully and ready to be used.</p> <p>&nbsp;</p> <h2>2. Code Java Project</h2> <p>We will code a Java program that inserts some contact information to a MySQL database. So create a new database schema named <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">contactsdb</span> with one table <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">contact</span> by executing the following MySQL statements (using either MySQL Workbench or MySQL Command Line Client):</p> <pre class="brush:text">CREATE DATABASE `contactsdb`; CREATE TABLE `contacts` ( `id` int(11) NOT NULL AUTO_INCREMENT, `firstname` varchar(20) NOT NULL, `lastname` varchar(20) NOT NULL, `city` varchar(30) NOT NULL, `country` varchar(30) NOT NULL, PRIMARY KEY (`id`) );</pre> <p>As you can see, the contacts table has 5 columns: id, firstname, lastname, city, and country.</p> <p>Next, make a new directory for the project, called <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">ContactManager</span>. And under this project create <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">src</span> folder to store Java source files. And create the directory <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">com\mycompany</span> under <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">src</span>, for the package named <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">com.mycompany</span>. Then we have the following directory structure:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/tools/ant/beginner/project-structure.png" alt="project-structure" /></p> <p>Now under <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">mycompany</span>, create a new Java source file named <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">ContactInserter.java</span> with the following code:</p> <pre class="brush:java">package com.mycompany; import java.sql.*; public class ContactInserter { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/contactsdb?useSSL=false"; String username = "root"; String password = "password"; String sql = "INSERT INTO contacts (firstname, lastname, city, country) VALUES (?, ?, ?, ?)"; try { Connection connection = DriverManager.getConnection(url, username, password); PreparedStatement statement = connection.prepareStatement(sql); statement.setString(1, "Tom"); statement.setString(2, "Eagar"); statement.setString(3, "Chicago"); statement.setString(4, "U.S.A"); int rows = statement.executeUpdate(); if (rows &gt; 0) { System.out.println("A row was inserted."); } else { System.out.println("No row was inserted."); } connection.close(); } catch (SQLException e) { e.printStackTrace(); } } }</pre> <p>As you can see, this program simply connects to the database, inserts a row to the <span style="font-family: 'Courier New';">contacts</span> table and then exits.</p> <p>Next, you will see how to use Ant to compile, package and run this program.</p> <p>&nbsp;</p> <h2>3. Write Ant build script</h2> <p>Create the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">build.xml</span> file under project’s root directory with the following XML code:</p> <pre class="brush:xml">&lt;project name="ContactProject" default="dist" basedir="."&gt; &lt;property name="src" location="src"/&gt; &lt;property name="build" location="build"/&gt; &lt;property name="dist" location="dist"/&gt; &lt;target name="init"&gt; &lt;tstamp/&gt; &lt;mkdir dir="${build}"/&gt; &lt;/target&gt; &lt;target name="compile" depends="init" description="compile the source"&gt; &lt;javac srcdir="${src}" destdir="${build}"/&gt; &lt;/target&gt; &lt;target name="dist" depends="compile" description="generate the distribution"&gt; &lt;mkdir dir="${dist}"/&gt; &lt;jar jarfile="${dist}/ContactManager-${DSTAMP}.jar" basedir="${build}"&gt; &lt;manifest&gt; &lt;attribute name="Main-Class" value="com.mycompany.ContactInserter"/&gt; &lt;/manifest&gt; &lt;/jar&gt; &lt;/target&gt; &lt;target name="clean" description="clean up"&gt; &lt;delete dir="${build}"/&gt; &lt;delete dir="${dist}"/&gt; &lt;/target&gt; &lt;/project&gt;</pre> <p>This Ant build file contains 4 targets init, compile, dist and clean – similar to the one described in the previous lesson.</p> <p>In the command line prompt, change the working directory to the project’s directory, and type<strong> ant </strong>command. You should see the following output:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/tools/ant/beginner/run-ant-build.png" alt="run-ant-build" width="600" height="259" /></p> <p>Typing <strong>ant</strong> command without any arguments will execute the default target. In our build file, the default target is <strong>dist</strong> which depends on <strong>compile</strong> target which depends on <strong>init</strong> target. Hence these 3 targets are executed in the following order: init, compile and dist. The <strong>clean</strong> target was not executed because it is standalone.</p> <p>Check the project’s directory and you see the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">build</span> directory was created and the compiled <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">.class</span> files are put in, as a result of the compile target.</p> <p>And the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">dist</span> target created the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">dist</span> directory and generate an executable JAR file in this directory. Now from the command line you can type the following command to run the program from JAR file:</p> <pre class="brush:text">java -jar dist\ContactManager-20191101.jar</pre> <p>And you will see this output:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/tools/ant/beginner/error-run-first-time.png" alt="error-run-first-time" width="640" height="96" /></p> <p>It throws <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';"><span style="color: #800000;">SQLException</span>&nbsp;</span>because no JDBC driver for MySQL found in the classpath.</p> <p><a href="https://dev.mysql.com/downloads/connector/j/" rel="nofollow" target="_self">Click here to</a> download JDBC driver for MySQL. Download and extract the ZIP archive, e.g. <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">mysql-connector-java-8.0.18.zip</span> and you will see the JAR file named <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">mysql-connector-java-8.0.18.jar</span>.</p> <p>Next, create a directory named <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">lib</span> under the project’s directory and copy the MySQL JDBC driver JAR file to it.</p> <p>In the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">build.xml</span> file, add two new properties:</p> <pre class="brush:xml">&lt;property name="lib" location="lib"/&gt; &lt;property name="JdbcDriver" value="mysql-connector-java-8.0.18.jar"/&gt;</pre> <p>In the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">&lt;manifest&gt;</span> section, add a new attribute:</p> <pre class="brush:xml">&lt;attribute name="Class-Path" value="${JdbcDriver}"/&gt;</pre> <p>This specifies the MySQL JDBC driver JAR file referenced by the executable JAR file. And add the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">&lt;copy&gt;</span> task right after the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">&lt;jar&gt;</span> task:</p> <pre class="brush:xml">&lt;copy file="${lib}\${JdbcDriver}" todir="${dist}" overwrite="true" /&gt;</pre> <p>This tells Ant to copy the MySQL JDBC driver JAR file to the distribution directory when the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">dist</span> target is executed.</p> <p>The complete <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">build.xml</span> file now looks like this:</p> <pre class="brush:xml">&lt;project name="ContactProject" default="dist" basedir="."&gt; &lt;property name="src" location="src"/&gt; &lt;property name="build" location="build"/&gt; &lt;property name="dist" location="dist"/&gt; &lt;property name="lib" location="lib"/&gt; &lt;property name="JdbcDriver" value="mysql-connector-java-8.0.18.jar"/&gt; &lt;target name="init"&gt; &lt;tstamp/&gt; &lt;mkdir dir="${build}"/&gt; &lt;/target&gt; &lt;target name="compile" depends="init" description="compile the source"&gt; &lt;javac srcdir="${src}" destdir="${build}"/&gt; &lt;/target&gt; &lt;target name="dist" depends="compile" description="generate the distribution"&gt; &lt;mkdir dir="${dist}"/&gt; &lt;jar jarfile="${dist}/ContactManager-${DSTAMP}.jar" basedir="${build}"&gt; &lt;manifest&gt; &lt;attribute name="Main-Class" value="com.mycompany.ContactInserter"/&gt; &lt;attribute name="Class-Path" value="${JdbcDriver}"/&gt; &lt;/manifest&gt; &lt;/jar&gt; &lt;copy file="${lib}\${JdbcDriver}" todir="${dist}" overwrite="true" /&gt; &lt;/target&gt; &lt;target name="clean" description="clean up"&gt; &lt;delete dir="${build}"/&gt; &lt;delete dir="${dist}"/&gt; &lt;/target&gt; &lt;/project&gt;</pre> <p>Now, type <span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">ant clean</span></strong></span> to delete everything the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';"><span style="color: #800000;">build</span>&nbsp;</span>and <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">dist</span> directories. You will see the following output:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/tools/ant/beginner/run-clean-target.png" alt="run-clean-target" width="460" height="139" /></p> <p>Type <strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">ant</span></strong> again to rebuild the project. You will see the MySQL JDBC driver JAR file is copied to the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">dist</span> directory, along with the generated executable JAR file of the program.</p> <p>Now you can type the following command to run the program from the JAR file:</p> <pre class="brush:text">java -jar dist\ContactManager-20191101.jar</pre> <p>You will see the following output:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/tools/ant/beginner/run-jar-file.png" alt="run-jar-file" /></p> <p>The program runs successfully and prints the message “A row was inserted”. You can check the database to confirm.</p> <p>So each time you make changes to the code, you can run <strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">ant</span></strong> command to rebuild the project, conveniently.</p> <p>That’s how to get started with Apache Ant – a popular build tool for Java projects. This lesson just scratches the surface, and Ant has more useful features which you can explore on Ant <a href="https://ant.apache.org/" rel="nofollow" target="_blank">official homepage</a>.</p></div><div class="feed-description"><p>Throughout this Ant tutorial, I will walk you through the process of developing a Java project using Ant build, step by step. After finishing this lesson, you will be able to wrote Ant script for a standard build of a Java project. And based on that, you will be able to modify Ant build script of an existing project and customize the build process when needed.</p> <p>To follow this tutorial, please use a text editor (<a href="coding/how-to-compile-and-run-a-java-program-with-textpad" target="_blank">Textpad</a> or <a href="coding/how-to-compile-and-run-a-java-program-with-sublime-text-3" target="_blank">Sublime</a>) with Command line prompt (on Windows) or Terminal (on Linux).</p> <p>&nbsp;</p> <h2>1. Download and setup Apache Ant</h2> <p>Go to <a href="https://ant.apache.org/bindownload.cgi" rel="nofollow" target="_blank">https://ant.apache.org/bindownload.cgi</a> to download the latest binary distribution of Apache Ant.</p> <p>When I’m writing this, the latest release is Ant 1.10.7 which requires minimum of Java 8 at runtime. Download the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">apache-ant-1.10.7-bin.zip</span> file and extract it on your hard drive. Then update the system environment variable PATH to include a path to <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">apache-ant-1.10.7\bin</span> directory. On Windows, you can type the following command in a Command Prompt window launched with administrator privilege:</p> <pre class="brush:text">setx -m PATH "%PATH%;apache-ant-1.10.8\bin"</pre> <p>Then open another command prompt, type <span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">ant -version</span></strong></span> and hit Enter. You should see the following output:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/tools/ant/beginner/ant-version.png" alt="ant-version" /></p> <p>That means Apache Ant was installed successfully and ready to be used.</p> <p>&nbsp;</p> <h2>2. Code Java Project</h2> <p>We will code a Java program that inserts some contact information to a MySQL database. So create a new database schema named <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">contactsdb</span> with one table <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">contact</span> by executing the following MySQL statements (using either MySQL Workbench or MySQL Command Line Client):</p> <pre class="brush:text">CREATE DATABASE `contactsdb`; CREATE TABLE `contacts` ( `id` int(11) NOT NULL AUTO_INCREMENT, `firstname` varchar(20) NOT NULL, `lastname` varchar(20) NOT NULL, `city` varchar(30) NOT NULL, `country` varchar(30) NOT NULL, PRIMARY KEY (`id`) );</pre> <p>As you can see, the contacts table has 5 columns: id, firstname, lastname, city, and country.</p> <p>Next, make a new directory for the project, called <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">ContactManager</span>. And under this project create <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">src</span> folder to store Java source files. And create the directory <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">com\mycompany</span> under <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">src</span>, for the package named <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">com.mycompany</span>. Then we have the following directory structure:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/tools/ant/beginner/project-structure.png" alt="project-structure" /></p> <p>Now under <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">mycompany</span>, create a new Java source file named <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">ContactInserter.java</span> with the following code:</p> <pre class="brush:java">package com.mycompany; import java.sql.*; public class ContactInserter { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/contactsdb?useSSL=false"; String username = "root"; String password = "password"; String sql = "INSERT INTO contacts (firstname, lastname, city, country) VALUES (?, ?, ?, ?)"; try { Connection connection = DriverManager.getConnection(url, username, password); PreparedStatement statement = connection.prepareStatement(sql); statement.setString(1, "Tom"); statement.setString(2, "Eagar"); statement.setString(3, "Chicago"); statement.setString(4, "U.S.A"); int rows = statement.executeUpdate(); if (rows &gt; 0) { System.out.println("A row was inserted."); } else { System.out.println("No row was inserted."); } connection.close(); } catch (SQLException e) { e.printStackTrace(); } } }</pre> <p>As you can see, this program simply connects to the database, inserts a row to the <span style="font-family: 'Courier New';">contacts</span> table and then exits.</p> <p>Next, you will see how to use Ant to compile, package and run this program.</p> <p>&nbsp;</p> <h2>3. Write Ant build script</h2> <p>Create the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">build.xml</span> file under project’s root directory with the following XML code:</p> <pre class="brush:xml">&lt;project name="ContactProject" default="dist" basedir="."&gt; &lt;property name="src" location="src"/&gt; &lt;property name="build" location="build"/&gt; &lt;property name="dist" location="dist"/&gt; &lt;target name="init"&gt; &lt;tstamp/&gt; &lt;mkdir dir="${build}"/&gt; &lt;/target&gt; &lt;target name="compile" depends="init" description="compile the source"&gt; &lt;javac srcdir="${src}" destdir="${build}"/&gt; &lt;/target&gt; &lt;target name="dist" depends="compile" description="generate the distribution"&gt; &lt;mkdir dir="${dist}"/&gt; &lt;jar jarfile="${dist}/ContactManager-${DSTAMP}.jar" basedir="${build}"&gt; &lt;manifest&gt; &lt;attribute name="Main-Class" value="com.mycompany.ContactInserter"/&gt; &lt;/manifest&gt; &lt;/jar&gt; &lt;/target&gt; &lt;target name="clean" description="clean up"&gt; &lt;delete dir="${build}"/&gt; &lt;delete dir="${dist}"/&gt; &lt;/target&gt; &lt;/project&gt;</pre> <p>This Ant build file contains 4 targets init, compile, dist and clean – similar to the one described in the previous lesson.</p> <p>In the command line prompt, change the working directory to the project’s directory, and type<strong> ant </strong>command. You should see the following output:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/tools/ant/beginner/run-ant-build.png" alt="run-ant-build" width="600" height="259" /></p> <p>Typing <strong>ant</strong> command without any arguments will execute the default target. In our build file, the default target is <strong>dist</strong> which depends on <strong>compile</strong> target which depends on <strong>init</strong> target. Hence these 3 targets are executed in the following order: init, compile and dist. The <strong>clean</strong> target was not executed because it is standalone.</p> <p>Check the project’s directory and you see the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">build</span> directory was created and the compiled <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">.class</span> files are put in, as a result of the compile target.</p> <p>And the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">dist</span> target created the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">dist</span> directory and generate an executable JAR file in this directory. Now from the command line you can type the following command to run the program from JAR file:</p> <pre class="brush:text">java -jar dist\ContactManager-20191101.jar</pre> <p>And you will see this output:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/tools/ant/beginner/error-run-first-time.png" alt="error-run-first-time" width="640" height="96" /></p> <p>It throws <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';"><span style="color: #800000;">SQLException</span>&nbsp;</span>because no JDBC driver for MySQL found in the classpath.</p> <p><a href="https://dev.mysql.com/downloads/connector/j/" rel="nofollow" target="_self">Click here to</a> download JDBC driver for MySQL. Download and extract the ZIP archive, e.g. <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">mysql-connector-java-8.0.18.zip</span> and you will see the JAR file named <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">mysql-connector-java-8.0.18.jar</span>.</p> <p>Next, create a directory named <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">lib</span> under the project’s directory and copy the MySQL JDBC driver JAR file to it.</p> <p>In the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">build.xml</span> file, add two new properties:</p> <pre class="brush:xml">&lt;property name="lib" location="lib"/&gt; &lt;property name="JdbcDriver" value="mysql-connector-java-8.0.18.jar"/&gt;</pre> <p>In the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">&lt;manifest&gt;</span> section, add a new attribute:</p> <pre class="brush:xml">&lt;attribute name="Class-Path" value="${JdbcDriver}"/&gt;</pre> <p>This specifies the MySQL JDBC driver JAR file referenced by the executable JAR file. And add the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">&lt;copy&gt;</span> task right after the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">&lt;jar&gt;</span> task:</p> <pre class="brush:xml">&lt;copy file="${lib}\${JdbcDriver}" todir="${dist}" overwrite="true" /&gt;</pre> <p>This tells Ant to copy the MySQL JDBC driver JAR file to the distribution directory when the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">dist</span> target is executed.</p> <p>The complete <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">build.xml</span> file now looks like this:</p> <pre class="brush:xml">&lt;project name="ContactProject" default="dist" basedir="."&gt; &lt;property name="src" location="src"/&gt; &lt;property name="build" location="build"/&gt; &lt;property name="dist" location="dist"/&gt; &lt;property name="lib" location="lib"/&gt; &lt;property name="JdbcDriver" value="mysql-connector-java-8.0.18.jar"/&gt; &lt;target name="init"&gt; &lt;tstamp/&gt; &lt;mkdir dir="${build}"/&gt; &lt;/target&gt; &lt;target name="compile" depends="init" description="compile the source"&gt; &lt;javac srcdir="${src}" destdir="${build}"/&gt; &lt;/target&gt; &lt;target name="dist" depends="compile" description="generate the distribution"&gt; &lt;mkdir dir="${dist}"/&gt; &lt;jar jarfile="${dist}/ContactManager-${DSTAMP}.jar" basedir="${build}"&gt; &lt;manifest&gt; &lt;attribute name="Main-Class" value="com.mycompany.ContactInserter"/&gt; &lt;attribute name="Class-Path" value="${JdbcDriver}"/&gt; &lt;/manifest&gt; &lt;/jar&gt; &lt;copy file="${lib}\${JdbcDriver}" todir="${dist}" overwrite="true" /&gt; &lt;/target&gt; &lt;target name="clean" description="clean up"&gt; &lt;delete dir="${build}"/&gt; &lt;delete dir="${dist}"/&gt; &lt;/target&gt; &lt;/project&gt;</pre> <p>Now, type <span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">ant clean</span></strong></span> to delete everything the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';"><span style="color: #800000;">build</span>&nbsp;</span>and <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">dist</span> directories. You will see the following output:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/tools/ant/beginner/run-clean-target.png" alt="run-clean-target" width="460" height="139" /></p> <p>Type <strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">ant</span></strong> again to rebuild the project. You will see the MySQL JDBC driver JAR file is copied to the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">dist</span> directory, along with the generated executable JAR file of the program.</p> <p>Now you can type the following command to run the program from the JAR file:</p> <pre class="brush:text">java -jar dist\ContactManager-20191101.jar</pre> <p>You will see the following output:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/tools/ant/beginner/run-jar-file.png" alt="run-jar-file" /></p> <p>The program runs successfully and prints the message “A row was inserted”. You can check the database to confirm.</p> <p>So each time you make changes to the code, you can run <strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">ant</span></strong> command to rebuild the project, conveniently.</p> <p>That’s how to get started with Apache Ant – a popular build tool for Java projects. This lesson just scratches the surface, and Ant has more useful features which you can explore on Ant <a href="https://ant.apache.org/" rel="nofollow" target="_blank">official homepage</a>.</p></div>Introduction to Ant for beginner2020-02-17T15:38:42-06:002020-02-17T15:38:42-06:00https://www.codejava.net/tools/ant/introduction-to-ant-for-beginnerNam Ha Minhhainatu@gmail.com<div class="feed-description"><p>In this tutorial, you will learn about Apache Ant – one of most popular build tools for software development with Java. By the end of the tutorial, you will understand the role of Ant in developing Java projects and able to develop a simple Java project using Ant build. You will also know how IDEs like Eclipse, NetBeans and IntelliJ IDEA support Ant.</p> <p>&nbsp;</p> <h2>1. What is Ant?</h2> <p>Apache Ant is a Java library and command-line tool for automating software build processes. Imagine you are working on a Java project that contains many classes and the build process involves in compiling <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';"><span style="color: #800000;">.java</span>&nbsp;</span>files to <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">.class</span> files, then packaging the compiled files to an executable JAR file. In each build, the name of the JAR file is suffixed by the current date time to track versions and releases. If you do this build process manually, you have to use <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">javac</span> command to <a href="java-core/tools/using-javac-command" target="_blank">compile</a>, <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">jar</span> command to <a href="java-core/tools/using-jar-command-examples" target="_blank">package</a> and maybe an OS-dependent command to change the JAR file name.</p> <p>That build process is repetitive and time-consuming if done manually. So you can automate the build process by creating an Ant build script that describes all the tasks need to be executed. Then you can use only a single Ant command to build the project, comfortably.</p> <p>That’s basically how Ant works and the reason why we need to use it. But Ant is not limited to that, it is a powerful tool that can be used to automate any type of process which can be described in terms of targets and tasks (a target contains a set of tasks).</p> <p>&nbsp;</p> <h2>2. A Brief History of Ant</h2> <p>Ant was created by James Ducan Davidson in early 2000 when he was working on the <a href="servers/tomcat" target="_blank">Apache Tomcat</a> project. James was creating Tomcat’s builds for different operating systems and he found that there’s no cross-platform build tools he can use. He used the Make build tool on Unix, but on Windows he had to write different build script. So James wrote Ant in pure Java code as a replacement to Make.</p> <p>Soon thereafter, Ant was used by several open source Java projects and spread like a virus. It became an independent project managed by the Apache Software Foundation – hence the official name Apache Ant. The name Ant is an acronym for “Another Neat Tool” – according to its original author James Duncan Davidson.</p> <p>The first official release of Ant was Ant 1.1, released on 19 July 2000, and the latest version (at the time of this writing) is Ant 1.10.7 released on 5 Sep 2019.</p> <p>Today Ant is the build tool of choice for a lot of projects, and it is the default build tool in NetBeans IDE (version 8.2 and older).</p> <p>&nbsp;</p> <h2>3. A Sample Ant Build script</h2> <p>Let’s look at a typical Ant build script to learn more. The default Ant build file name is <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">build.xml</span> which is usually placed in the project’s root directory. Following is the code of a sample Ant build script for a Java project:</p> <pre class="brush:xml">&lt;project name="MyProject" default="dist" basedir="."&gt; &lt;!-- set global properties for this build --&gt; &lt;property name="src" location="src"/&gt; &lt;property name="build" location="build"/&gt; &lt;property name="dist" location="dist"/&gt; &lt;target name="init"&gt; &lt;!-- Create the time stamp --&gt; &lt;tstamp/&gt; &lt;!-- Create the build directory structure used by compile --&gt; &lt;mkdir dir="${build}"/&gt; &lt;/target&gt; &lt;target name="compile" depends="init" description="compile the source"&gt; &lt;!-- Compile the java code from ${src} into ${build} --&gt; &lt;javac srcdir="${src}" destdir="${build}"/&gt; &lt;/target&gt; &lt;target name="dist" depends="compile" description="generate the distribution"&gt; &lt;!-- Create the distribution directory --&gt; &lt;mkdir dir="${dist}/lib"/&gt; &lt;!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file --&gt; &lt;jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"&gt; &lt;manifest&gt; &lt;attribute name="Main-Class" value="net.codejava.HelloAnt"/&gt; &lt;/manifest&gt; &lt;/jar&gt; &lt;/target&gt; &lt;target name="clean" description="clean up"&gt; &lt;!-- Delete the ${build} and ${dist} directory trees --&gt; &lt;delete dir="${build}"/&gt; &lt;delete dir="${dist}"/&gt; &lt;/target&gt; &lt;/project&gt;</pre> <p>As you can see, we use XML to describe the build process in terms of targets and tasks. There are 4 targets defined in this build file: <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">init</span>, <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">compile</span>, <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">dist </span>and <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">clean</span>. When you run an Ant build, the default target will be executed.</p> <p>In this project, the default target is <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">dist</span>:</p> <pre class="brush:xml">&lt;project name="MyProject" default="dist" basedir="."&gt;</pre> <p>Look at the description of the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">dist</span> target, you will see it depends on the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">compile</span> target:</p> <pre class="brush:xml"> &lt;target name="dist" depends="compile”…&gt;</pre> <p>In turn, the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">compile</span> target depends on the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">init</span> target:</p> <pre class="brush:xml">&lt;target name="compile" depends="init"…&gt;</pre> <p>So the sequence of execution is <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">init</span>, <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">compile </span>and <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">dist</span>. The <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">init</span> target initializes resources for the build, e.g. create a timestamp and make the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">build</span> directory. It is done using the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">tstamp</span> and <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">mkdir</span> tasks:</p> <pre class="brush:xml">&lt;target name="init"&gt; &lt;tstamp/&gt; &lt;mkdir dir="${build}"/&gt; &lt;/target&gt;</pre> <p>Then the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">compile</span> target compiles all Java source files and put the compile <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">.class</span> files to the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">build</span> directory, using the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">javac</span> task:</p> <pre class="brush:xml">&lt;target name="compile" depends="init"&gt; &lt;javac srcdir="${src}" destdir="${build}"/&gt; &lt;/target&gt;</pre> <p>And finally the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">dist</span> target generates an executable JAR file and put it into the distribution directory <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">dist/lib</span>. It is done using the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">mkdir</span> and <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">jar</span> tasks:</p> <pre class="brush:xml">&lt;target name="dist" depends="compile"&gt; &lt;mkdir dir="${dist}/lib"/&gt; &lt;jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"&gt; &lt;manifest&gt; &lt;attribute name="Main-Class" value="net.codejava.HelloAnt"/&gt; &lt;/manifest&gt; &lt;/jar&gt; &lt;/target&gt;</pre> <p>That’s the execution flow of this build file. And you can notice that the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">clean</span> target doesn’t depend on any targets, which means it will run standalone if requested.</p> <p>From the command line prompt, just type <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">ant</span> run the build starting from the default target. And you would see the following output:</p> <p style="text-align: center;"><img loading="lazy" src="images/articles/tools/ant/intro/run-ant-build.png" alt="run-ant-build" />&nbsp;</p> <p>You can also type <span style="color: #800000;"><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">ant</span> <i><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">&lt;target&gt;</span></i></span> to run a specific target, for example:</p> <p style="text-align: center;"><img loading="lazy" src="images/articles/tools/ant/intro/run-target-clean.png" alt="run-target-clean" />&nbsp;</p> <p>This executes the clean target that deletes everything in the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">build</span> and <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">dist</span> directories.</p> <p>You can reuse this sample <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">build.xml</span> file for any Java project.</p> <p>&nbsp;</p> <h2>4. Benefits of using Ant</h2> <p>Ant helps automating the build process so you can save time and effort when developing Java projects using Ant build. You can avoid doing repetitive tasks manually, which is boring and time-consuming.</p> <p>In addition, Ant is written in pure Java code so you can reuse Ant build files across platforms.</p> <h2><br />5. Drawbacks of using Ant</h2> <p>Ant build files, which are written in XML, can be complex and verbose when the project becomes complex and large. Ant doesn’t directly support dependency management (automatic download and management of library JAR files), though you can do it using Apache Ivy, which requires longer learning curve.</p> <p>&nbsp;</p> <h2>6. Ant supported by IDEs</h2> <p>NetBeans IDE versions 8.2 and older uses Ant as the default build for its projects. From newer versions (Apache NetBeans), it allows programmers to explicitly choose a build tool for new projects (Ant, Maven or Gradle). Eclipse and IntelliJ IDEA allow you to generate Ant build files for projects.</p> <p>And all these IDEs support code completion and suggestion for Ant build files, which allows you to write Ant script easily.</p> <p>To learn more, visit <a href="https://ant.apache.org/" rel="nofollow" target="_self">Apache Ant homepage</a>.</p> <p>&nbsp;</p></div><div class="feed-description"><p>In this tutorial, you will learn about Apache Ant – one of most popular build tools for software development with Java. By the end of the tutorial, you will understand the role of Ant in developing Java projects and able to develop a simple Java project using Ant build. You will also know how IDEs like Eclipse, NetBeans and IntelliJ IDEA support Ant.</p> <p>&nbsp;</p> <h2>1. What is Ant?</h2> <p>Apache Ant is a Java library and command-line tool for automating software build processes. Imagine you are working on a Java project that contains many classes and the build process involves in compiling <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';"><span style="color: #800000;">.java</span>&nbsp;</span>files to <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">.class</span> files, then packaging the compiled files to an executable JAR file. In each build, the name of the JAR file is suffixed by the current date time to track versions and releases. If you do this build process manually, you have to use <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">javac</span> command to <a href="java-core/tools/using-javac-command" target="_blank">compile</a>, <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">jar</span> command to <a href="java-core/tools/using-jar-command-examples" target="_blank">package</a> and maybe an OS-dependent command to change the JAR file name.</p> <p>That build process is repetitive and time-consuming if done manually. So you can automate the build process by creating an Ant build script that describes all the tasks need to be executed. Then you can use only a single Ant command to build the project, comfortably.</p> <p>That’s basically how Ant works and the reason why we need to use it. But Ant is not limited to that, it is a powerful tool that can be used to automate any type of process which can be described in terms of targets and tasks (a target contains a set of tasks).</p> <p>&nbsp;</p> <h2>2. A Brief History of Ant</h2> <p>Ant was created by James Ducan Davidson in early 2000 when he was working on the <a href="servers/tomcat" target="_blank">Apache Tomcat</a> project. James was creating Tomcat’s builds for different operating systems and he found that there’s no cross-platform build tools he can use. He used the Make build tool on Unix, but on Windows he had to write different build script. So James wrote Ant in pure Java code as a replacement to Make.</p> <p>Soon thereafter, Ant was used by several open source Java projects and spread like a virus. It became an independent project managed by the Apache Software Foundation – hence the official name Apache Ant. The name Ant is an acronym for “Another Neat Tool” – according to its original author James Duncan Davidson.</p> <p>The first official release of Ant was Ant 1.1, released on 19 July 2000, and the latest version (at the time of this writing) is Ant 1.10.7 released on 5 Sep 2019.</p> <p>Today Ant is the build tool of choice for a lot of projects, and it is the default build tool in NetBeans IDE (version 8.2 and older).</p> <p>&nbsp;</p> <h2>3. A Sample Ant Build script</h2> <p>Let’s look at a typical Ant build script to learn more. The default Ant build file name is <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">build.xml</span> which is usually placed in the project’s root directory. Following is the code of a sample Ant build script for a Java project:</p> <pre class="brush:xml">&lt;project name="MyProject" default="dist" basedir="."&gt; &lt;!-- set global properties for this build --&gt; &lt;property name="src" location="src"/&gt; &lt;property name="build" location="build"/&gt; &lt;property name="dist" location="dist"/&gt; &lt;target name="init"&gt; &lt;!-- Create the time stamp --&gt; &lt;tstamp/&gt; &lt;!-- Create the build directory structure used by compile --&gt; &lt;mkdir dir="${build}"/&gt; &lt;/target&gt; &lt;target name="compile" depends="init" description="compile the source"&gt; &lt;!-- Compile the java code from ${src} into ${build} --&gt; &lt;javac srcdir="${src}" destdir="${build}"/&gt; &lt;/target&gt; &lt;target name="dist" depends="compile" description="generate the distribution"&gt; &lt;!-- Create the distribution directory --&gt; &lt;mkdir dir="${dist}/lib"/&gt; &lt;!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file --&gt; &lt;jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"&gt; &lt;manifest&gt; &lt;attribute name="Main-Class" value="net.codejava.HelloAnt"/&gt; &lt;/manifest&gt; &lt;/jar&gt; &lt;/target&gt; &lt;target name="clean" description="clean up"&gt; &lt;!-- Delete the ${build} and ${dist} directory trees --&gt; &lt;delete dir="${build}"/&gt; &lt;delete dir="${dist}"/&gt; &lt;/target&gt; &lt;/project&gt;</pre> <p>As you can see, we use XML to describe the build process in terms of targets and tasks. There are 4 targets defined in this build file: <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">init</span>, <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">compile</span>, <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">dist </span>and <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">clean</span>. When you run an Ant build, the default target will be executed.</p> <p>In this project, the default target is <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">dist</span>:</p> <pre class="brush:xml">&lt;project name="MyProject" default="dist" basedir="."&gt;</pre> <p>Look at the description of the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">dist</span> target, you will see it depends on the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">compile</span> target:</p> <pre class="brush:xml"> &lt;target name="dist" depends="compile”…&gt;</pre> <p>In turn, the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">compile</span> target depends on the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">init</span> target:</p> <pre class="brush:xml">&lt;target name="compile" depends="init"…&gt;</pre> <p>So the sequence of execution is <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">init</span>, <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">compile </span>and <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">dist</span>. The <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">init</span> target initializes resources for the build, e.g. create a timestamp and make the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">build</span> directory. It is done using the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">tstamp</span> and <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">mkdir</span> tasks:</p> <pre class="brush:xml">&lt;target name="init"&gt; &lt;tstamp/&gt; &lt;mkdir dir="${build}"/&gt; &lt;/target&gt;</pre> <p>Then the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">compile</span> target compiles all Java source files and put the compile <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">.class</span> files to the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">build</span> directory, using the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">javac</span> task:</p> <pre class="brush:xml">&lt;target name="compile" depends="init"&gt; &lt;javac srcdir="${src}" destdir="${build}"/&gt; &lt;/target&gt;</pre> <p>And finally the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">dist</span> target generates an executable JAR file and put it into the distribution directory <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">dist/lib</span>. It is done using the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">mkdir</span> and <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">jar</span> tasks:</p> <pre class="brush:xml">&lt;target name="dist" depends="compile"&gt; &lt;mkdir dir="${dist}/lib"/&gt; &lt;jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"&gt; &lt;manifest&gt; &lt;attribute name="Main-Class" value="net.codejava.HelloAnt"/&gt; &lt;/manifest&gt; &lt;/jar&gt; &lt;/target&gt;</pre> <p>That’s the execution flow of this build file. And you can notice that the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">clean</span> target doesn’t depend on any targets, which means it will run standalone if requested.</p> <p>From the command line prompt, just type <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">ant</span> run the build starting from the default target. And you would see the following output:</p> <p style="text-align: center;"><img loading="lazy" src="images/articles/tools/ant/intro/run-ant-build.png" alt="run-ant-build" />&nbsp;</p> <p>You can also type <span style="color: #800000;"><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">ant</span> <i><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">&lt;target&gt;</span></i></span> to run a specific target, for example:</p> <p style="text-align: center;"><img loading="lazy" src="images/articles/tools/ant/intro/run-target-clean.png" alt="run-target-clean" />&nbsp;</p> <p>This executes the clean target that deletes everything in the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">build</span> and <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">dist</span> directories.</p> <p>You can reuse this sample <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">build.xml</span> file for any Java project.</p> <p>&nbsp;</p> <h2>4. Benefits of using Ant</h2> <p>Ant helps automating the build process so you can save time and effort when developing Java projects using Ant build. You can avoid doing repetitive tasks manually, which is boring and time-consuming.</p> <p>In addition, Ant is written in pure Java code so you can reuse Ant build files across platforms.</p> <h2><br />5. Drawbacks of using Ant</h2> <p>Ant build files, which are written in XML, can be complex and verbose when the project becomes complex and large. Ant doesn’t directly support dependency management (automatic download and management of library JAR files), though you can do it using Apache Ivy, which requires longer learning curve.</p> <p>&nbsp;</p> <h2>6. Ant supported by IDEs</h2> <p>NetBeans IDE versions 8.2 and older uses Ant as the default build for its projects. From newer versions (Apache NetBeans), it allows programmers to explicitly choose a build tool for new projects (Ant, Maven or Gradle). Eclipse and IntelliJ IDEA allow you to generate Ant build files for projects.</p> <p>And all these IDEs support code completion and suggestion for Ant build files, which allows you to write Ant script easily.</p> <p>To learn more, visit <a href="https://ant.apache.org/" rel="nofollow" target="_self">Apache Ant homepage</a>.</p> <p>&nbsp;</p></div>