Difference between Maven and Apache Ant

Build tools are programs to manage and automate build process of your application. By build process means compiling source code, linking the packages, bundle them into an executable binary.

Maven and Apache Ant are popular build tools from Apache.

Here are key some key differences in both tools:

Apache Ant             Maven
Ant is flexible and does not impose coding conventions. You need to specify the source of code and the destination.

Maven uses conventions. It knows where your source code is automatically, as long as you follow these conventions. You don’t need to tell Maven where it is.

Ant is procedural. You have to tell Ant exactly what to do; tell it to compile, copy, then compress, etc.

Maven is declarative; All you have to do is create a pom.xml file and put your source in the default directory. Maven will take care of the rest.

Ant doesn’t have a lifecycle.


Maven has a lifecycle. You simply call mvn install and a series of sequential steps are executed.

In Ant, you would first have to JUnit JAR file is, then create a classpath that includes the JUnit JAR, then tell Ant where it should look for test source code, write a goal that compiles the test source and then finally execute the unit tests with JUnit.

Maven has intelligence about common project tasks. To run tests, simply execute mvn test, as long as the files are in the default location.

Ant is simply a build tool. 

Maven is more of project management tool.

Apache Ant


Apache Ant basic tasks

In the previous post, we have learned what is Apache Ant, its features and an example to create simple build file. This post will guide you other basic ant tasks that are frequently used to create and deploy packages.


 Ant allows us to declare properties and assign values that can be used as variables

<property name="property_name" value="property_value"/>

And that can be used using


Ant predefined properties




Version of apache ant


Java version that apache ant is using


Home directory where apache is installed


Path of ant library files


Location of build file


Base directory of build

Echoing message on screen

<echo message="Version of Apache Ant ${ant.project.name}: ${ant.file}"/>

Define classpath

<path id="classpath">
    <fileset dir="directory_path" includes="*.jar" />
    <fileset dir="directory_path" includes="*.jar" />

Compiling Java code

<javac srcdir="source_path_of_java_files" classpathref="classpath" destdir="destination_of_compiled_files" />

Creating Jar

<jar jarfile="jar_name.jar" basedir="compiled_files_path" includes="**/*.class" update="true">

Create directory

<mkdir dir="directory_name" />

Delete directory

<delete dir="directory_name" />

Copy directory

<copy todir="directory_path_where_to_copy" overwrite="true">
    <fileset dir="directory_path_to_copy_files">
         <include name="**/*.*" />

Copy files

<copy file="file_to_copy" todir="directory_path_where_to_copy" overwrite="true" />

Delete files

     <fileset dir="directory_path_to_copy_files">
         <include name="file_name_or_wild_card"/>
         <include name="file_name_or_wild_card"/>

File loading 

<loadfile property="defined_property_name" srcFile="file_to_load_as_string" />

Replace string in file

<replace file="file_path" token="string_to_replace" value="string_value_to_replace_with"></replace>

Set property based on condition

Based on condition property value is set to true or false.

<condition property="property_name_to_set">
     <matches string="string_data" pattern="pattern_to_match_in_string" />

Above code will set property if particular pattern is matched in specified string. Pattern can hold regex expression. You can specified string inline or property that holds the string file data. Detailed explanation is available on official site.


   <equals arg1="value" arg2="value"/>
   <then><echo message="if block executed"/></then>
         <echo message="else block executed"/>

Like if-else in any other language, based on condition task is performed. Detailed explanation is available on official site.

Call target inside a target

<antcall target="valid_target_name"/>

Add external tasks

<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask" classpath="./lib/xmltask.jar" />

Xmltask : Insert data using XPath

<xmltask source="source_file_to_edit" dest="destination_to_save_file">
     <insert path="//*[local-name()='beans'][1]/*[local-name()='bean'][last()]" position="after">
              <import xmlns="http://www.springframework.org/schema/beans" resource="MyData.xml"/>
 Above command will insert xml-import after the path defined using XPath.

Xmltask : Insert data based on condition using XPath

<xmltask source="source_file_to_edit" dest="destination_to_save_file">
     <copy path="//*[local-name()='beans'][1]/*[local-name()='bean'][@resource='MyData.xml']/@resource" property="ITEM_EXISTS_ALREADY" />
     <insert path="//*[local-name()='beans'][1]/*[local-name()='bean'][last()]" position="after"> position="after" unless="ITEM_EXISTS_ALREADY">
            <import xmlns="http://www.springframework.org/schema/beans" resource="MyData.xml"/>

The above code will set property ITEM_EXISTS_ALREADY if the attribute resource with value MyData.xml  exists under given XPath. Then insertion will be performed based on the ITEM_EXISTS_PROPERTY.

Xmltask : Remove xml content

<xmltask source="source_file_to_edit" dest="destination_to_save_file">
    <remove path="/beans/bean/import[@resource='MyData.xml']"/>

The above code will remove the import with resource value MyData.xml.




Apache Ant : Java Build Tool

Apache Ant is a Java library and command-line tool to automate the repetitive tasks and helps to build software. Ant is flexible and does not impose coding conventions. It supplies a number of built-in tasks allowing to compile, assemble, test and run Java applications.

Other than Java applications it can also be used to build non-Java applications like C or C++ applications.

The Apache Ant project is part of the Apache Software Foundation.

Ant Features

  • Command-line tool to automate the repetitive tasks
  • A flexible tool that provides built-in tasks allowing to compile, assemble, test and run Java applications
  • Can develop custom tasks
  • Declaring properties using property element
  • Copying files or directories from one location to other
  • Delete files or directories
  • Edit files and replace/delete some content
  • Conditional tasks

Ant Setup

  • To work with apache ant JDK (Java Development Kit) must have installed on your system.
  • Set JAVA_HOME environment variable.
  • Download Apache Ant from the link.
  • Unzip the downloaded Apache zip and set the ANT_HOME environment variable.
  • To verify the installation use following command
C:\>ant -version
Apache Ant(TM) version 1.9.7 compiled on April 9 2016

Creating simple build file

Apache Ant build file is written in XML. To create a simple build file you need to create a project and at least one target using ‘project’ and ‘target’ element.


<project basedir="." default="build" name="project_name">

name is the name of the project.

default attribute specifies the default target that will be executed when no target is specified with ant command.

basedir attribute defines the path of the directory from where another path will be resolved.


<target depends="init" name="build-project">
   <echo message="any message here to print"/>

name is the name of target that can be used to refer/run a target.

depends attribute defines the target on which current target depends on. Dependent target will be executed first.


 <project basedir="." default="init" name="project_name">
      <target name="init">
          <echo message="Init target"/>

Save above snippet as build.xmlTo run this use ant command.

Buildfile: D:\build.xml
     [echo] Init target
Total time: 1 second

When you simply run ant command it searches for the build.xml file and run the default target that is defined in project element.

If your build file is saved with some other name then you can use buildfile option.

 ant -buildfile file_name.xml

 If you want to run specific target then you can use

 ant -buildfiile file_name.xml target_name

Next Tutorial >> Apache Ant Basic Tasks

Apache Ant : Install Apache Ant on Linux Machine

Apache Ant is a Java library and command-line tool to automate the repetitive tasks and helps to build software. Ant is flexible and does not impose coding conventions. It supplies a number of built-in tasks allowing to compile, assemble, test and run Java applications.

Install Apache Ant 


1) Download Ant
$ wget http://www.us.apache.org/dist/ant/binaries/apache-ant-1.9.2-bin.tar.gz
2) Extract using tar command
$ sudo tar xvfvz apache-ant-1.9.2-bin.tar.gz -C /opt
3) Check files if installed
$ sudo ln -s /opt/apache-ant-1.9.2 /opt/ant
4) Setup ANT environment variable by creating file called ant.sh under /etc/profile.d/ directory
$ vi /etc/profile.d/ant.sh
5) Add the following contents
export CLASSPATH=.
6) Save and close the file. Make it executable using the following command
$ chmod +x /etc/profile.d/ant.sh
7) Set the environment variables permanently by running the following command
$ source /etc/profile.d/ant.sh
8) Reboot your system.
9) Check the ant version using command
$ ant -version
10) Done