Ant and Maven both are build tools provided by Apache.
The main purpose of these technologies is to ease the build process of a project.
There are many clear differences between Maven and ANT:
1) Ant doesn't has formal conventions, so we need to provide information of the project structure in build.xml file.
A) Maven has a convention to place source code, compiled code etc. So we don't need to provide information about the project structure in pom.xml file.(Project Object Model)
2) Ant is procedural, you need to provide information about what to do and when to do through code. You need to provide order.
B) Maven is declarative, everything you define in the pom.xml file. (Project Object Model)
3) There is no life cycle in Ant.
C) There is life cycle in Maven.
4) ANT is a tool box.
D) Maven is a framework.
5) ANT is mainly a build tool.
E) Maven is mainly a project management tool.
6) The ANT scripts are not reusable.
F) The maven plugins are reusable.
7) G) ANT is less preferred than Maven.
What is Build Tool ?
A build tool takes care of everything during building a process. It does following:
a) Generates source code (if auto-generated code is used)
b) Generates documentation from source code
c) Compiles source code
d) Packages compiled code into JAR of ZIP file
e) Installs the packaged code in local repository, server repository, or central repository
Understanding the problem without Maven
There are many problems that we face during the project development. They are discussed below:
1) Adding set of Jars in each project: In case of struts, spring, hibernate frameworks, we need to add set of jar files in each project. It must include all the dependencies of jars also.
2) Creating the right project structure: We must create the right project structure in servlet, struts etc, otherwise it will not be executed.
3) Building and Deploying the project: We must have to build and deploy the project so that it may work.
4) It provides us local, central and remote repository.
What it does?
Maven simplifies the above mentioned problems. It does mainly following tasks.
a) It makes a project easy to build
b) It provides uniform build process (maven project can be shared by all the maven projects)
c) It provides project information (log document, cross referenced sources, mailing list, dependency list, unit test reports etc.)
d) It is easy to migrate for new features of Maven
Apache Maven helps to manage :
Builds, Documentation, Reporting, SCMs, Releases, Distribution
BUT WE ARE GOING TO LEARN ANT ?
How to write, build, and debug code in Eclipse through the Ant editor ?
Apache Ant is considered the Holy Grail of build tools in the Java™ development world.
Working with Ant : Create a new Ant build file
Begin by adding a new Ant file to your project
1. Open the Package Explorer
2. Right-click any Java Project and click New > File
3. In the New File window, type build.xml as the file name
The file is now created, and the Ant editor opens. Now, add some content to the file. Click anywhere in the editor and press Ctrl+Space. A completion proposal containing an option called Buildfile template
<project> -- attributes used is name, depends, description etc...
This is the root tag within buid.xml
within this project tag, there are different tags used like <description> for describing the project, <property> tag for describing the different properties name and value, <target> tag with name, depends and description attributes.
within this <target> tag there is different inner tags are available like <tstamp> tag for time stamp, <mkdir> tag for create a build directory structure used by compile <javac> tag for includeruntime attribute, srcdir and destdir attributes, <jar> tag for different attributes like jarfile and basedir, which internally contains the <manifest> tag supported by <attribute> tag internally.
==============================================================
build.xml
==============================================================
<?xml version="1.0" encoding="UTF-8"?>
<!-- ======================================================================
Jan 24, 2017 2:23:34 PM
project
description
Nishant_Kumar1
====================================================================== -->
<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>
<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>