Skip to content

Maven

December 2, 2007

The main feature of Maven is that it knows how to access remote repositories with Java libraries. You do not need to download all necessary .jar files yourself, you just define what libraries your project needs. Maven does the rest. Which means that the 3rd-party libraries do not fill anymore your (usually) lib directory.

The places where Maven finds are

  • regular directories served by a normal Web HTTP server
  • in a local file-system.

There is not much difficulty in managing dependencies for a single a project, but when you start getting into dealing with multi-module projects and applications that consist of tens or hundreds of modules this is where Maven can help you a great deal in maintaining a high degree of control and stability.

Eg:

<project>
<modelVersion>4.0.0</modelVersion>
<groupId>maven</groupId>
<artifactId>A</artifactId>
<packaging>pom</packaging>
<name>A</name>
<version>1.0</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>test</groupId>
<artifactId>a</artifactId>
<version>1.2</version>
</dependency>


One can have parent and child project mgmt settings. pom.xml is the project’s Project Object Model, or POM.

When a phase is given, Maven will execute every phase in the sequence up to and including the one defined. For example, if we execute the compile phase, the phases that actually get executed are:

  1. validate
  2. generate-sources
  3. process-sources
  4. generate-resources
  5. process-resources
  6. compile

List of phases is as follows:

  • 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.

* make is a procedural language that let’s you program build scripts
* ant is a programming language that let’s you program build scripts
o using coarse-grained components
+ wrapping around existing and new tools
o that have a more or less uniform API
o and has very good support for transitive target dependencies
o ~ Prolog, really heavy on the side effects
+ declarative, logic programming / procedural
* maven is a declarative description of you project’s metadata
o with standardized build script for all sorts of targets
o extensible and customizable
* (automake ~ make build script generator, based on standardized build protocols)
* The difference between make and ant, and maven is like the difference between a library and a framework
o The algorithm is defined (template)
o What it does specifically is left open (hooks)

Maven is a simple core
* Everything is done through plugins
* Activated through IoC container (Plexus)
* Every plugin works in isolation (ClassLoader)
* Plugins are downloaded from a plugin-repository on demand

An apache group’s tool that can build the projects, publish project information, defines what the project consists of and that can share JARs across several projects. Maven is non trivial because all file references need to be relative. The restriction imposed by Maven is that only one artifact is generated per project (A project in Maven terminology is a folder with a project.xml file in it). A Maven project can have sub projects. Each sub project can build its own artifact. The topmost project can aggregate the artifacts into a larger one. This is synonymous to jars and wars put together to form an EAR. Maven also provides inheritance in projects.

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: