Maven versus Ant

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>

Tip

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:

  • Convention over configuration: Ant requires a developer to configure everything right from the source code's location to the storage of a JAR file. Maven, on the other hand, follows conventions, has a well-defined project structure, and knows where to reference source, resource files, and place the output.
  • Lifecycle: Ant does not have a lifecycle and requires defining goals and their dependencies. Also, in Ant, the sequence of tasks needs to be specified. Maven has defined a lifecycle that consists of build phases and goals; hence, no configuration is required.

Apart from the preceding differences that can be cited from the preceding simple example, Maven is superior to Ant in the following aspects:

  • Higher level of reusability: The build logic can be reused with Maven across different projects in Maven.
  • Less maintenance: With a standardized structure and the reusability option, it requires less effort towards maintenance.
  • Dependency management: One of the most superior aspects of Maven over Ant is its ability to manage the corresponding dependencies. Though, lately, Ant in combination with Apache Ivy does ease dependency management; however, Maven has other aspects that outdo this combo offering.
  • Automatic downloads: Maven downloads the dependencies automatically; however, Ant lacks this. While Ant can use Ivy to replicate this behavior, it requires additional behavior.
  • Repository management: Maven repositories are arbitrary and accessible locations that are designed to store the artifacts that Maven builds. They manage repositories as local versus remote (will be discussed in detail in the Repository section of Chapter 3, Creating and Importing Projects). Ant does not have this aspect built.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.245.1