Tag Archives: Ant

Maven – Project Management and Builds Automation

Maven is a software tool for project management and builds automation. While primarily used for Java programming, it can also be used to build and manage projects written in C#, Ruby, Scala, and other languages.

Maven is the standard way to build projects and it also provides various other characters like clearing the definition of the project, ways to share jars across projects. It also provides the easy way to publish project information (OOS).

Maven serves a similar purpose to the Apache Ant tool, but it is based on different concepts and works in a profoundly different manner. Maven is hosted by the Apache Software Foundation, where it was formerly part of the Jakarta Project.

Maven’s Objective
The primary goal of maven is to allow the developers to comprehend the complete state of a project in the shortest time by using easy build process, uniform building system, quality project management information (such as change Log, cross-reference, mailing lists, dependencies, unit test reports, test coverage reports and many more), guidelines for best practices and transparent migration to new features.

To achieve to this goal Maven attempts to deal with several areas like:

  • It makes the build process easy
  • Provides a uniform building system
  • Provides quality related project information
  • Provides guidelines related to development to meet the best goal.
  • Allows transparent migration to new features.

Java™ Application Development on Linux® – Free 599 Page eBook

Enterprise Java Virtualization:

Understanding the TCO Implications

InfoWorld’s Java IDE Comparison Strategy Guide:

Java Essential Training

Apache Jakarta Commons: Reusable Java™ Components

Enabling Rapid ROI: With Java™ – Based Business Intelligence Applications:

Maven’s Features

  1. Portable: Maven is portable in nature because it includes:
  • Building configuration using maven are portable to another machine, developer and architecture without any effort
  • Non trivial: Maven is non trivial because all file references need to be relative, environment must be completely controlled and independent from any specific file system.
  1. Technology: Maven is a simple core concept that is activated through IoC container (Plexus). Everything is done in maven through plugins and  every plugin works in isolation (ClassLoader). Plugings are downloaded from a plugin-repository on demand.

Faceoff ~ Maven vs Ant

When & Why Ant? When & Why Maven
“Situational Software” which has to be developed quickly (in a few weeks / months). You are working with many “creative” developers. It will be hard to establish a defined project structure etc. without tool support
Projects with external dependencies which are working with “cutting edge” libraries. There is no need for finer grained dependency management. The developers are religious about their IDE and are using IntelliJ, NetBeans and Eclipse at the same time.
NetBeans projects :-). They come with predefined Ant scripts which even work perfectly with Hudson. Just check everything into svn and let hudson check it out. You are building a product-like application. You will have to manage different versions and branches with their dependencies.
Legacy projects which do not fit the Maven conventions very well. Violating Maven conventions may become a hassle. You project consists of several teams which work on dependent modules. These modules have to be composed to the application regularly.
Projects without explicit requirements for modularization. The deployable output will consist of only a few archives. You plan to use checkstyle, pmd, generate a project website etc. It’s easy with Maven.
Ant gives the ultimate power and flexibility in build and deployment to the developer. Maven adds a layer of abstraction above Ant (and uses Jelly). Maven can be used to build any Java application.
Dozens of lines in an Ant build script. Today JEE build and deployment has become much standardized. Every enterprise has some variations, but in general it is all the same: deploying EARs, WARs, and EJB-JARs. Maven captures this intelligence and lets you achieve the build and deployment in about 5-6 lines of Maven script
Ant lets you do any variations you want, but requires a lot of scripting. Maven on the other hand mandates certain directories and file names, but it provides plugins to make life easier.

Maven is essentially a project management and comprehension tool and as such provides a way to help with managing:

Builds

  • Documentation
  • Reporting
  • Dependencies
  • SCMs
  • Releases
  • Distribution

Maven uses a construct known as a Project Object Model (POM) to describe the software project being built, its dependencies on other external modules and components, and the build order. It comes with pre-defined targets for performing certain well-defined tasks such as compilation of code and its packaging.

Maven dynamically downloads Java libraries and Maven plug-ins from one or more repositories. Maven provides built-in support for retrieving files from the Maven Central Repository and other Maven repositories, and can upload artifacts to specific repositories after a successful build. A local cache of downloaded artifacts acts as the primary means of synchronizing the output of projects on a local system.
Maven Tools
Maven Phases

Project lifecycles are central to Maven. Most developers are familiar with the notion of build phases such as compile, test, and deploy. Ant has targets with names like those. In Maven 1, corresponding plug-ins are called directly. To compile Java source code, for instance, the java plug-in is used:

In Maven, this notion is standardized into a set of well-known and well-defined lifecycle phases. Instead of invoking plug-ins, the Maven  developer invokes a lifecycle phase: $mvn compile.

Maven Phases

Maven Phases

Although hardly a comprehensive list, these are the most common default lifecycle phases executed.

  • validate: validate the project is correct and all necessary information is available
  • compile: compile the source code of the project
  • test: test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
  • package: take the compiled code and package it in its distributable format, such as a JAR.
  • integration-test: process and deploy the package if necessary into an environment where integration tests can be run
  • verify: run any checks to verify the package is valid and meets quality criteria
  • install: install the package into the local repository, for use as a dependency in other projects locally
  • deploy: done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

There are two other Maven lifecycles of note beyond the default list above. They are

* clean: cleans up artifacts created by prior builds

* site: generates site documentation for this project

Phases are actually mapped to underlying goals. The specific goals executed per phase is dependant upon the packaging type of the project. For example, package executes jar:jar if the project type is a JAR, and war:war is the project type is – you guessed it – a WAR.

An interesting thing to note is that phases and goals may be executed in sequence.

mvn clean dependency:copy-dependencies package

This command will clean the project, copy dependencies, and package the project (executing all phases up to package, of course).

Generating the Site

mvn site

This phase generates a site based upon information on the project’s pom. You can look at the documentation generated under target/site.
The POM
The pom.xml file is the core of a project’s configuration in Maven. It is a single configuration file that contains the majority of information required to build a project in just the way you want. The POM is huge and can be daunting in its complexity, but it is not necessary to understand all of the intricacies just yet to use it effectively. This project’s POM is:

xmlns=”http://maven.apache.org/POM/4.0.0″ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>4.0.0<groupId>com.mycompany.app</groupId><artifactId>my-app</artifactId>

<packaging>jar</packaging>

<version>1.0-SNAPSHOT</version>

<name>Maven Quick Start Archetype</name>

<url>http://maven.apache.org</url>

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.8.2</version>

<scope>test</scope>

</dependency>

</dependencies>

</project>

What Maven can Do?
A typical Maven site can publish, on a daily basis:

  • General project information such as source repositories, defect tracking, team members, etc.
  • Unit test and test coverage reports
  • Automatic code reviews and with Checkstyle and PMD
  • Configuration and versioning information
  • Dependencies
  • Javadoc
  • Source code in indexed and cross-referenced HTML format
  • Team member list
  • And much more

Maven – The Show Stopper
Maven simplifies build enormously by imposing certain fixed file names and acceptable restrictions like one artifact per project. Artifacts are treated as files on your computer by the build script. Maven hides the fact that everything is a file and forces you to think and script to create a deployable artifact such as an EAR. Artifact has a dependency on a particular version of a third party library residing in a shared remote (or local) enterprise repository, and then publishes your library into the repository as well for others to use. Hence there are no more classpath issues. No more mismatch in libraries. It also gives the power to embed even the Ant scripts within Maven scripts if absolutely essential.

Maven is a powerful tool that greatly simplifies and standardizes the build process. By promoting a standard project organization and recommended best practices, Maven handles much of the grunt work. And standard plug-ins such as the site generator provide valuable team-oriented project tools with little extra effort. Check it out!

Single Sign-On for Java and Web Applications

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Transforming a Generic Java IDE to Your Application Specific IDE:

The Java Virtual Appliance—No OS Required

BEA WebLogic® Operations Control: Application Virtualization for Enterprise Java

Enabling Rapid ROI: With Java™ – Based Business Intelligence Applications:

References:
http://maven.apache.org/

http://en.wikipedia.org/wiki/Apache_Maven

http://maven.apache.org/download.html#Installation

http://maven.apache.org/guides/getting-started/index.html

http://java.dzone.com/news/maven-or-ant

http://www.roseindia.net/maven2/introduction-Maven.shtml

http://www.javaworld.com/javaworld/jw-12-2005/jw-1205-maven.html

Sample Ant Build

Your first Sample Ant Build

 <!–?xml version=”1.0″?>

<project name=”Ant test project” default=”build” basedir=”.”>

<target name=”build” >

<javac srcdir=”src” destdir=”build/src” debug=”true” includes=”**/*.java“/>

</target>

</project>

First line of the build.xml file represents the document type declaration

Second line is comment entry.

Third line is the project tag. Each buildfile contains one project tag and all the instruction are written in the project tag.

The project tag:

<project name=”Ant test project” default=”build” basedir=”.”>  requires three attributes namely name, default and basedir.

Here is the description of the attributes:

Attribute             Description

name                    Represents the name of the project.

default                  Name of the default target to use when no target is supplied.

basedir                  Name of the base directory from which all path calculations are done.

All the attributes are required.

One project may contain one or more targets. In this example there is only one target.

<target name=”build” >

<javac srcdir=”src” destdir=”build/src” debug=”true” includes=”**/*.java”/>

</target>

Which uses task javac to compile the java files.

Here is the code of our AntTestExample.java file which is to be compiled by the Ant utility.

class AntTestExample{

public static void main (String args[]){

System.out.println(“This is Ant Test Example “);

}

}

Run the ANT file with ant command.

Above mentioned process compiles the file and places in the buildsrc directory.

Refer to examples

Java™ Application Development on Linux® – Free 599 Page eBook

Enterprise Java Virtualization:

Understanding the TCO Implications

InfoWorld’s Java IDE Comparison Strategy Guide:

Java Essential Training

Apache Jakarta Commons: Reusable Java™ Components

Enabling Rapid ROI: With Java™ – Based Business Intelligence Applications:

Single Sign-On for Java and Web Applications

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Transforming a Generic Java IDE to Your Application Specific IDE:

The Java Virtual Appliance—No OS Required

BEA WebLogic® Operations Control: Application Virtualization for Enterprise Java

Enabling Rapid ROI: With Java™ – Based Business Intelligence Applications:

Introduction to ANT

Introduction to ANT

Apache Ant is a powerful way to convert your developmental structures to deployment structures. It is declarative and all the command line tasks used for deploying an application are represented by simple XML elements.

A Java-based build tool.

Ant is a free tool under GNU License and is freely available at ANT Site, current version of ant is 1.7.1.

Ant allows the developer to automate the repeated process involved in the development of J2EE/J2SE application.

Developers can easily write the script to automate the build process like compilation, archiving and deployment.

Ant (originally an acronym for Another Neat Tool), is a build tool with special support for the Java programming language but can be used for just about everything. Ant is platform-independent; it is written purely in Java. Ant is particularly good at automating complicated repetitive tasks and thus is well suited for automating standardized build processes. Ant accepts instructions in the form of XML documents thus are extensible and easy to maintain.

The Official Definition:
“Apache Ant is a Java-based build tool. In theory, it is kind of like make, without make’s wrinkles.”
-  ant.apache.org

It can take care of archiving, compilation, execution, documentation, deployment, and much more.

Ant is cross platform and portable.

Ant is well suited to a large number of automation and scripting tasks.

Ant is not a coding language.

Ant uses XML to describe the build process and its dependencies. By default the XML file is named build.xml.

Java™ Application Development on Linux® – Free 599 Page eBook

Enterprise Java Virtualization:

Understanding the TCO Implications

InfoWorld’s Java IDE Comparison Strategy Guide:

Java Essential Training

Apache Jakarta Commons: Reusable Java™ Components

Enabling Rapid ROI: With Java™ – Based Business Intelligence Applications:

ANT Benefits

    Platform independent
Requires only a JDK 1.1 or later JVM.
Easy to use
Built-in tasks accomplish all typical build functions.
User contributed tasks cover most other needs.
Easy to extend
Create a new task by writing Java code.

ANT Capabilities

  • Compile java applications
  • Create javadoc documentation
  • Create jar, zip, tar, war files
  • Delete and copy files
  • Send mail
  • Validate XML files
  • Ant can get source code from version control
    • CVS, Subversion, Synergy, Perforce, ClearCase and many more
    • Ant can run unit tests
  • JUnit3, JUnit4, TestNG, or any arbitrary test application

Single Sign-On for Java and Web Applications

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Transforming a Generic Java IDE to Your Application Specific IDE:

The Java Virtual Appliance—No OS Required

BEA WebLogic® Operations Control: Application Virtualization for Enterprise Java

Enabling Rapid ROI: With Java™ – Based Business Intelligence Applications:

installing Ant

Installing ANT

•             Download Ant binary distribution from:

                Download

•             Set ANT_HOME to where you installed Ant

•             Include $ANT_HOME/bin in PATH

•             Make sure JAVA_HOME is set to point to JDK

After successful installation of Ant, run ant –version command to verify it.

Java™ Application Development on Linux® – Free 599 Page eBook

Enterprise Java Virtualization:

Understanding the TCO Implications

InfoWorld’s Java IDE Comparison Strategy Guide:

Java Essential Training

Apache Jakarta Commons: Reusable Java™ Components

Enabling Rapid ROI: With Java™ – Based Business Intelligence Applications:

Single Sign-On for Java and Web Applications

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Transforming a Generic Java IDE to Your Application Specific IDE:

The Java Virtual Appliance—No OS Required

BEA WebLogic® Operations Control: Application Virtualization for Enterprise Java

Enabling Rapid ROI: With Java™ – Based Business Intelligence Applications:

How ANT works?

How ANT works?

Each Project will have a build file (build.xml)

Each build file will contain one or more Targets

The Target to be executed:

                Is either explicitly selected on the command line

                Or a project default Target is executed

Java™ Application Development on Linux® – Free 599 Page eBook

Enterprise Java Virtualization:

Understanding the TCO Implications

InfoWorld’s Java IDE Comparison Strategy Guide:

Java Essential Training

Apache Jakarta Commons: Reusable Java™ Components

Enabling Rapid ROI: With Java™ – Based Business Intelligence Applications:

Each Target is executed only once.

Project

is the top level element in an Ant script

<project> has three optional attributes:

                name: the name of the project

                default: the default target to use when no target is supplied

                basedir: the base directory from which all path calculations are done

<project name=“MyProject” default=“compile”>

                <!–- properties and targets will come here…–>

</project>

Targets

             Each project defines zero or more targets

             A target is a set of tasks you want to be executed

             When starting Ant, you can select which target(s) you want to have executed

             When no target is given, the project’s default is used

             Targets can be conditionally executed (using if/unless)

             A target can depend on other targets

             Target dependencies are transitive

             Each Target will contain one or more Tasks

             Some Tasks are executed conditionally

             Tasks are implemented as Java classes

<project name=“MyProject” default=“compile”>

                <property name=”buildDir” value=”build”/>

                <property name=“srcDir” value=“.”/> 

                <target name=”compile”>

                                <!–Tasks will come here…–>

                </target>

</project>

Tasks

             A task is a piece of code that can be executed

             A task can have multiple attributes (a.k.a arguments)

             The value of an attribute might use the value of a property.

             Ant comes with over 80 core tasks, and 60 optional tasks

             Ant task extensions can be easily written for any unique problem

<project name=“MyProject” default=“compile”>

                <property name=”buildDir” value=”build”/>

                <property name=“srcDir” value=“.”/> 

                <target name=”compile”>

                                <javac srcdir=”${srcDir}”               destdir=”${build}”/>

                </target>

</project>

Single Sign-On for Java and Web Applications

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Transforming a Generic Java IDE to Your Application Specific IDE:

The Java Virtual Appliance—No OS Required

BEA WebLogic® Operations Control: Application Virtualization for Enterprise Java

Enabling Rapid ROI: With Java™ – Based Business Intelligence Applications:

Built in Tasks in Ant

 

Java™ Application Development on Linux® – Free 599 Page eBook

Enterprise Java Virtualization:

Understanding the TCO Implications

InfoWorld’s Java IDE Comparison Strategy Guide:

Java Essential Training

Apache Jakarta Commons: Reusable Java™ Components

Enabling Rapid ROI: With Java™ – Based Business Intelligence Applications:

Built in Tasks in ANT

File Tasks

<copy>

<concat>

<delete>  

<get>

<mkdir>

<move>

<replace>

Compile Tasks

<javac>

Compiles the specified source file(s) within the running (Ant) VM, or in another VM if the fork attribute is specified.

<jspc>

                Runs the JSP compiler. The task can be used to compile the generated Java source.

<rmic>

                Runs the rmic compiler

Archive Tasks

<zip>, <unzip>

                Creates a zipfile.

<jar>, <unjar>

                Jars a set of files.

<war>, <unwar>

                An extension of the Jar task with special treatment web archive dirs

<ear>

                An extension of the Jar task with special treatment enterprise archive dirs.

Testing Tasks

<junit>

                Runs tests from the JUnit testing framework.

<junitreport>

Merges the individual XML files generated by the Junit task and applies a stylesheet on the resulting merged document to provide a browsable report of the testcases results.

Property Tasks

<dirname>

                Sets a property to the value excluding the last path element.

<loadfile>

                Loads a file into a property.

<propertyfile>

                Creates or modifies property files.

Misc. Tasks

<echo>

                Echoes text to System.out or to a file.

<javadoc>

                Generates code documentation using the javadoc tool.

<sql>

Executes a series of SQL statements via JDBC to a database. Statements can either be read in from a text file using the src attribute, or from between the enclosing SQL tags.

Refer to Core Tasks for more details.

Single Sign-On for Java and Web Applications

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Transforming a Generic Java IDE to Your Application Specific IDE:

The Java Virtual Appliance—No OS Required

BEA WebLogic® Operations Control: Application Virtualization for Enterprise Java

Enabling Rapid ROI: With Java™ – Based Business Intelligence Applications:

ANT Overview, Structure and Basics

ANT Overview, Structure and Basics (Build File)

ANT Overview, Structure and Basics (Build File)

ANT Overview, Structure & Basics (Build File)

Each Project will have a build file (build.xml)

Default name for a build file is build.xml

Root element must be the ‘project’ element

The ‘default’ attribute is required

Specifies the default target to use

Other attributes available (e.g. name, basedir)

Comments are allowed

Java™ Application Development on Linux® – Free 599 Page eBook

Enterprise Java Virtualization:

Understanding the TCO Implications

InfoWorld’s Java IDE Comparison Strategy Guide:

Java Essential Training

Apache Jakarta Commons: Reusable Java™ Components

Enabling Rapid ROI: With Java™ – Based Business Intelligence Applications:

Project

There is only one ‘project’ per build file.

A top level collection of targets.

Property

An Ant variable .

A ‘name’ and ‘value’ pair

Immutable once set.

Example

<project name=“MyProject” default=“compile”>

                <property name=“buildDir” value=“build”/>

                <property name=“srcDir” value=“.”/> 

                <!–- targets will come here…–>

</project>

Target

A collection of tasks executed to achieve a particular purpose (a goal).

As many different ‘target’ elements as needed ( at least one ).

Each target can contain as many tasks as are needed.

Each build file will contain one or more Targets

The Target to be executed:

Is either explicitly selected on the command line

Or a project default Target is executed

Each Target is executed only once.

Task

A unit of Ant execution (a step).

Large set of built-in tasks and optional tasks.

A simple Ant build file

 

Create build.xml with following content.

<project default=”hello”>

                <target name=”hello”>

                                <echo message=”Hello, World”/>

</target>

</project>

Single Sign-On for Java and Web Applications

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Transforming a Generic Java IDE to Your Application Specific IDE:

The Java Virtual Appliance—No OS Required

BEA WebLogic® Operations Control: Application Virtualization for Enterprise Java

Enabling Rapid ROI: With Java™ – Based Business Intelligence Applications: