Before the emergence of Maven, Ant was the most widely used build tool across Java projects. Ant emerged from the concept of creating files in C/C++ programming to a platform-independent build tool. Ant used XML files to define the build process and its corresponding dependencies.
Another Neat Tool (Ant) was conceived by James Duncan Davidson while preparing Sun's reference JSP/Servlet engine, Apache Tomcat. The following is a simple sample of an Ant build file (http://ant.apache.org/manual/using.html):
<project name="MyProject" default="dist" basedir="."> <description> simple example build file </description> <!-- set global properties for this build --> <property name="src" location="src"/> <property name="build" location="build"/> <property name="dist" location="dist"/> <target name="init"> <!-- Create the time stamp --> <tstamp/> <!-- Create the build directory structure used by compile --> <mkdir dir="${build}"/> </target> <target name="compile" depends="init" description="compile the source " > <!-- Compile the java code from ${src} into ${build} --> <javac srcdir="${src}" destdir="${build}"/> </target> <target name="dist" depends="compile" description="generate the distribution" > <!-- Create the distribution directory --> <mkdir dir="${dist}/lib"/> <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file --> <jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/> </target> <target name="clean" description="clean up" > <!-- Delete the ${build} and ${dist} directory trees --> <delete dir="${build}"/> <delete dir="${dist}"/> </target> </project>
Downloading the sample code
You can download the sample code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
This example shows how to build a simple JAR file. Note how all the details corresponding to source files, class files, and JAR files have to be specified. Even the sequence of steps must be specified. This results in a complex build file and often a lot of duplicated XML.
Let's look at the simplest Maven build file, the pom
file, which will be discussed in more detail in Chapter 3, Creating and Importing Projects.
A simple pom
file will look as shown in the following code snippet:
<project> <modelVersion>4.0.0</modelVersion> <groupId>com.packt.mvneclipse</groupId> <artifactId>mvneclipse</artifactId> <version>1.2</version> </project>
This is all we need to build and package as a JAR from a Java project. Some of the differences between Ant and Maven in the preceding examples are as follows:
Apart from the preceding differences that can be cited from the preceding simple example, Maven is superior to Ant in the following aspects:
52.15.245.1