Pages

Monday, June 7, 2010

Introduction To Maven

Share it Please

Apache Maven is not just a build tool. It is a build tool, deployment tool and also a project management and a comprehension tool. It manages project build, documentation, reporting and also project dependencies.

A project management tool is nothing but a tool which takes care of initiating, planning, executing, monitoring and controlling projects and a comprehension tool makes the developer understand the project structure with in a less time.

So what makes Maven different from Ant [ Apache Ant is a build tool used heavily in projects for building , compiling and deploying ].Ant mainly focus on pre-processing , building , compiling , packaging and testing , where as Maven provides the same features with some additional capabilities like running reports , generating sites , providing communication among the development team.
Convention over Configuration
One of the important features of Maven over Ant is Convention over configuration also known as coding by convention. This allows the developer to perform various operations on a project with making less decisions .This helps the developer to gain simplicity without losing flexibility. This is achieved by imposing certain standards while building a project. According to Maven, it provides a default structure for the project source code, test cases, resources and various dependencies.

Maven Directory Structure
The power of maven comes from the standards imposed while building projects. Maven imposes a default directory structure which will help developers to understand the structure of the project with in very less time [Comprehension]. The standard Maven 2 directory structure is illustrated in Figure. In the project home directory goes the POM (pom.xml) and two subdirectories: src for all source code and target for generated artifacts. 


The src directory has a number of subdirectories, each of which has a clearly defined purpose:
  • src/main/java: Your Java source code goes here (strangely enough!)
  • src/main/resources: Other resources your application needs
  • src/main/filters: Resource filters, in the form of properties files, which may be used to define variables only known at runtime
  • src/main/config: Configuration files
  • src/main/webapp: The Web application directory for a WAR project
  • src/test/java: Unit tests
  • src/test/resources: Resources to be used for unit tests, but will not be deployed
  • src/test/filters: Resources filters to be used for unit tests, but will not be deployed
  • src/site: Files used to generate the Maven project Website

Installing Maven on Microsoft Windows

®  Install Maven from http://maven.apache.org/download.html
®  Set the Path for Maven
                        C :> set MAVEN_HOME=c:\Program Files\apache-maven-2.2.1
                        C :> set PATH=%PATH%;%MAVEN_HOME%\bin
®  Test the Maven Installation By  “mvn  –v”

Maven Installation Details
The following are the directories in maven

®  Bin  : contains maven scripts that executes maven
®  Boot : contains a jar file classworlds-1.1.jar which is used in creating a class loader in which maven executes
®  Conf : contains global settings.xml file which is used to customize the maven installation
®  Lib : contains a single maven core jar file

User Specific Configuration
Once we start using maven extensively we will see maven has created some directories for us in the local system. They are,

C:\Document & settings\User ID\: A folder by the name .m2 is created in which contains settings.xml file which contains settings for customizing the behavior of maven.
C:\User ID: in this we will see a directory by the name MAVEN. In it we have a directory by the name repository. When we download dependencies from a remote repository a local copy is saved here.
Creating a Sample project
We will see the core concepts of maven by creating a sample project and moving through the project step by step. In order to create a sample project we use the command

mvn archetype:generate -DgroupId=com.myCompany.SecondExample -DartifactId=SecondExample -Dpackage=com.myCompany.SecondExample -Dversi
on=1.0-SNAPSHOT

Let’s see the command

®  mvn is a maven command

®  archetype: generate : a maven goal similar to ANT target which defines a unit of work
®  The plug-in is the prefix archetype, and the goal is generate

Maven Archetype
Archetypes are nothing but a model from which all other things can be created which is of same type. Consider that we want to standardize our development within our organization with certain standards, so we create the archetypes with the standards and made them available in our repository so that developers will start using them to create new projects according to the organization standards. We can create archetypes for different sort of application ranging from j2ee, war to ear e.t.c.

To create a project based on archetype, we need to call mvn archetype: generate goal like this
mvn archetype: generate

The archetypes are nothing but a jar which contains archetype metadata which describes the contents of the archetype and velocity templates which help in constructing the project.
Maven provides several archetypes, some of them are
maven-archetype-quickstart           An archetype which contains a sample Maven project.
maven-archetype-simple               An archetype which contains a simple Maven project.
maven-archetype-site-simple          An archetype which contains a sample Maven site.
maven-archetype-webapp             An archetype which contains a sample Maven Webapp  project.


Building a Project
Once the project is created from the archetype, go to the main directory and execute the command
“mvn install” . For the above example go to SecondExample directory that is created in your C drive and execute the command


Core Concepts
So we have now created a simple maven project, now we will go thorough core concepts of Maven

POM [Project Object Model] – A POM is said to be a fundamental unit of work .it is an xml file which contain configuration details used by maven in building the project. When maven executes it looks for the POM file in order to build the project. It also contains project configuration details also.
®the project configuration details include Project details include description , version , url and information about developers
®Configuration details include project dependencies, build files, plug-ins and goals that can be executed.
So what does a POM file contains, here is an excerpt from the PM file that is created in SecondExample directory

 <modelVersion>4.0.0</modelVersion>

  <groupId>com.myCompany.SecondExample</groupId>
  <artifactId>SecondExample</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

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

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>


We will go through the important ones,
  project root – Root element is project
modelVersion - Version of the Object model this POM is using, set to 4.0.0
groupId-unique identifier of the organization that created the project, as   com.myCompany.app
articraftId -unique base name of the primary articraft being generated by the object , ex:javaero-1.0.jar
packaging -type of packaging to be used by this articraft ex: war, jar, ear
version- version of the generated articraft
name- display name used by the project. Often used in generated documentation, during build process.
url-this indicated where the project can be found
description-Basic description of the project

The above elements are minimum configuration that Maven requires in order to build a project. The first few elements—groupId, artifactId,packaging, version—are what is known as the Maven coordinates which uniquely identify a project. name and url are descriptive elements of the POM providing a human readable name and associating the project with a web site.
The POM names the project, provides a set of unique identifiers (coordinates) for a project, and defines the relationships between this project and others through dependencies, parents, and prerequisites.
Super POM
All POM files extend from their parent POM or super POM. All POMs extend the Super POM unless explicitly set, meaning the configuration specified in the Super POM is inherited by the POMs you created for your projects. The default settings for Maven are defined in the Super POM.
mvn help:effective-pom
Execute the above command from the SecondExample Directory and we can see much larger POM which has the defaults.

Maven Plug-in and Goal
When we created the sample example above, we executed 2 commands “mvn archetype:generate “and “mvn install”.

The first one is a maven plug-in goal, archetype is a plug-in and generate is goal which belongs to archetype plug-in. when we run a goal from a plug-in, maven displays the info regarding the plugin and the goal.
Maven Plug-in - Maven plug –in is nothing but a collection of one or more goals. Some examples of plug-ins include jar plug-in which helps in creating the jars, compiler plug-in which helps in compiling the source code . Maven gives us the ability to write our own custom plug-in in java ,jruby and some other languages.
Maven Goal – a maven goal is nothing but a unit of work or a task. a plug-in may contain  any number of goals. Some examples of goals include jar which helps in creating the jar, compile which helps in compiling the files. So how do we use them, in order to use a maven goal we need to call it from plug-in like?
Compiler: compile – the compiler is a plug-in and compile is a goal related to compiler plug-in.
Clean Plug-in is one of the simplest maven 2.0 plugging. When we run "mvn clean", the "clean" goal is executed and the target directory is removed.
Some more plugins,
clean
Clean up after the build.
compiler
Compiles Java sources.
deploy
Deploy the built artifact to the remote repository.
install
Install the built artifact into the local repository.
resources
Copy the resources to the output directory for including in the JAR.
site
Generate a site for the current project.
surefire
Run the Junit tests in an isolated classloader.
verifier
Verifies the existence of certain conditions.
Packaging types / tools
Related to packaging respective artifact types.
ear
Generate an EAR from the current project.
ejb
Build an EJB from the current project.
jar
Build a JAR from the current project.
rar
Build a RAR from the current project.
Tools
Tools available through Maven by default
ant
Generate an Ant build file for the project.
antrun
Run a set of ant tasks from a phase of the build.
archetype
Generate a skeleton project structure from an archetype.
assembly
Build an assembly of sources and/or binaries.
dependency
Dependency manipulation and analysis.
enforcer
Environmental constraint checking, User Custom Rule Execution.
gpg
Create signatures for the artifacts and poms.
help
Get information about the working environment for the project.
invoker
Run a set of Maven projects and verify the output
one
A plugin for interacting with legacy Maven 1.x repositories and builds.
patch
Use the gnu patch tool to apply patch files to source code.
plugin
Create a Maven plugin descriptor for any Mojo's found in the source tree, to include in the JAR.
release
Release the current project
remote-resources
Copy remote resources to the output directory for inclusion in the artifact.
repository
Plugin to help with repository-based tasks.

Maven Lifecycle
The second command we ran is “mvn install”, which is a maven life cycle command. It is a phase in a project build cycle. Project Build life cycle is nothing but a sequence of steps involved in building a project. The default life cycle includes testing the validity of the project to constructing the site for the project. Build life cycles differ from project to project
, a project may generate war and some other may generate jar.

A build phase may contain plug-in goals attached to them, so when ever we execute a maven life cycle phase, maven also executes the plug-in goal attached to it. For example, if we execute the package phase, the plug-in goal “jar: jar” which is used to generate the jar is also executed in order to generate the jar file for the project.

The important thing is that whenever we execute a maven phase, the phase preceding the current one also gets executes. So if we execute the package phase, the phase preceding the package phase like compiles, test also gets executed and the jar file is prepared.

®  The default life cycle phase is(for a complete list of the build phases, refer
to the Lifecycle Reference):
®  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.

So how can we execute a maven life cycle, so in order to execute the clean life cycle, execute “mvn clean” from the SecondExample directory.

Maven Coordinates
When we created a project, a POM file was created .the POM file was actually a description of the project. When Maven executes a goal, it looks for the POM file .when it founds it gathers all the information about the project. So consider when a jar goal is getting executed, the jar plug-in checks the name of the jar file to be used from the POM file.

So a Maven coordinated is nothing but properties that are defined uniquely for a project.
groupId,artifactId,packaging and version are the coordinates that are unique for every project . Maven coordinates are often written using a colon as a delimiter in the following format: groupId:artifactId:packaging:version. These four elements become the key to locating and using one particular project in the vast space of other “Mavenized” projects .so if a project is mavenized and if another project wants to use this project , then it just needs to specify the artifactId in the dependencies element.

Maven Coordinates are already explained earlier.

Maven Repository
When we are working with a large project, we need a lot number of dependent jars for compiling, executing the project. We will keep a copy of all jars in out project lib directory. But when some other started working with same project t, they may use a different version of same jar file. Maven helps in this situation by using Repositories

A Maven Repository is nothing but a store where we can store all the dependent jars required for a project. This helps in solving the duplication of jars.

The main purpose of Maven Repository is to server as an internal private organization repository for all dependent jars for the project.

There are two kinds of repository: local and remote. Both of them can be declaratively set. Unless specified otherwise, the local repository is created in a special directory called ".m2/repository". In Windows, this directory is created in C:\Documents and Settings\Administrator. For example, if your project depends on commons-logging version 1.0.4, you can specify the dependency in pom.xml and when maven is executed, it will copy the appropriate commons-logging jar file from the remote repository to the local repository.

So when we start maven for the first time, maven connects to the remote repository and downloads the required jars. This is the reason why maven download is very small. It comes with only necessary plugins.Once we need some library maven connects to the remote maven repository and downloads them. They are stored in the local repository and from then the project will be able to access the jars from the local repository. The remote repository that maven connects is

So what is a Maven repository? , a maven repository is nothing collection of project artifacts. So when you go the maven remote repository and access Ant libraries, they will be in the following structure


These are saved in following format
/<groupId>/<artifactId>/<version>/<artifactId>-<version>.<packaging>.

Maven downloads these artifacts (dependent software) and stores them in the local repository.

Instead of each project having its own copies of third party libraries, the repository helps developers across projects to share the libraries. Each project can also in turn generate its artifacts and publish it into the remote repository. The process of publishing a jar into the repository is called "install" in Maven lingo. This install process helps organizations to share internal artifacts across projects in a standard manner.

Maven Dependency Management
When we created the SecondExample project, we got the POM file which contains the snippet

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
 </dependencies>

In the above snippet we are defining a maven dependency. Maven resolved the coordinates as junit: junit: 3.8.1.Now what maven does is it finds the junit library by using the artifact and downloads the dependent library into the local repository. This gives maven the ability for dependency management. This was a simple dependency. A more complex project may contain multiple dependencies .

We need to define the necessary jar that is required in our project; maven just downloads the necessary jar into our local repository. Once we run any life cycle phase, maven downloads the jar necessary; we can go to the local repository and see the junit available in repository. In the junit directory, we can see a junit-3.8.1.jar and also a POM file and some other sha files. Maven uses these sha files for verifying the authenticity of the downloaded artifact. The POM file is used to provide supports Maven transitive dependencies.

What is a Transitive Dependency?
In order to download the necessary jar, we define them in the POM file .consider if we we are using
Hibernate 3.0 in our project. So we define the jar in dependencies like

<dependencies>
    <dependency>
      <groupId>hiberante</groupId>
      <artifactId> hiberante </artifactId>
      <version>3.0.0</version>
      <scope>test</scope>
    </dependency>
 </dependencies>

We just defined that we need hibernate, we have not defined any other libraries that hibernate 3.0 need. Maven will take care by downloading the jar that hibernate 3.0 needs to run. So we will get the hibernate 3.0 as well as all the libraries that are needed in order for the hibernate 3.0 application to run.

Dependency Scopes
We will include the entire libraries that are needed to run our application. But there are some libraries that are needed only at compile time and not necessary at runtime. In such case we need to make sure that the libraries are not included at run time. Dependency scopes helps in this place. Dependency scopes allow some libraries to be included at places. There are 4 types of dependency scopes,

®  compile: A compile-scope dependency is available in all phases. This is the default value.
®  provided: A provided dependency is used to compile the application, but will not be deployed. You would use this scope when you expect the JDK or application server to provide the JAR.
®  runtime: Runtime-scope dependencies are not needed for compilation, only for execution, such as JDBC (Java Database Connectivity) drivers.
®  test: Test-scope dependencies are needed only to compile and run tests (JUnit, for example).

Site Generation and Reporting
One of the important feature of maven is site generation and reporting. Execute the life cycle phase
“mvn site”
This generates the new directory calls site in the target. In the site directory , open the index.html page to see all the basic information regarding the project

We will see some more details on maven in my  next articles . So Happy Coding.....

No comments :

Post a Comment