Maven

What is MAVEN?
Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information.

Feature Summary
The following are the key features of Maven in a nutshell:


 * Simple project setup that follows best practices - get a new project or module started in seconds
 * Consistent usage across all projects means no ramp up time for new developers coming onto a project
 * Superior dependency management including automatic updating, dependency closures (also known as transitive dependencies)
 * Able to easily work with multiple projects at the same time
 * A large and growing repository of libraries and metadata to use out of the box, and arrangements in place with the largest Open Source projects for real-time availability of their latest releases
 * Extensible, with the ability to easily write plugins in Java or scripting languages
 * Instant access to new features with little or no extra configuration
 * Ant tasks for dependency management and deployment outside of Maven
 * Model based builds: Maven is able to build any number of projects into predefined output types such as a JAR, WAR, or distribution based on metadata about the project, without the need to do any scripting in most cases.
 * Coherent site of project information: Using the same metadata as for the build process, Maven is able to generate a web site or PDF including any documentation you care to add, and adds to that standard reports about the state of development of the project. Examples of this information can be seen at the bottom of the left-hand navigation of this site under the "Project Information" and "Project Reports" submenus.
 * Release management and distribution publication: Without much additional configuration, Maven will integrate with your source control system such as CVS and manage the release of a project based on a certain tag. It can also publish this to a distribution location for use by other projects. Maven is able to publish individual outputs such as a JAR, an archive including other dependencies and documentation, or as a source distribution.
 * Dependency management: Maven encourages the use of a central repository of JARs and other dependencies. Maven comes with a mechanism that your project's clients can use to download any JARs required for building your project from a central JAR repository much like Perl's CPAN. This allows users of Maven to reuse JARs across projects and encourages communication between projects to ensure that backward compatibility issues are dealt with. We are collaborating with the folks at Ibiblio who have graciously allowed the central repository to live on their servers.

Installation

 * 1) Download Maven
 * 2) Unzip maven-2.0.x-bin.zip to the directory you wish to install Maven 2.0.x. These instructions assume you chose C:\Program Files\Apache Software Foundation\maven-2.0.x
 * 3) Add the bin directory to your path, by opening up the system properties (WinKey + Pause), selecting the "Advanced" tab, and the "Environment Variables" button, then editing the PATH variable in the user variables. eg. "C:\Program Files\Apache Software Foundation\maven-2.0.3\bin";%PATH%
 * 4) In the same dialog, make sure that JAVA_HOME is set to the location of your JDK, eg. C:\Program Files\Java\jdk1.5.0_02
 * 5) Run mvn --version to verify that it is correctly installed.

Maven 2.x Plug-in for Eclipse
Take a look at the website http://m2eclipse.codehaus.org/ to learn how to install this plugin.

Documentation

 * The marven documentation can be found at http://maven.apache.org/guides/index.html


 * Getting Started

Preparation of the Development Environment
1) Download MAVEN from http://maven.apache.org/download.html

2) Unpack the archive where you would like to store the binaries, eg:

tar zxvf maven-2.0.tar.gz or unzip maven-2.0.zip 3) Rename the output directory to maven.

4) Add the bin directory to your PATH, eg:

export PATH=/usr/local/maven-2.0/bin:$PATH or set PATH="c:\program files\maven-2.0\bin";%PATH% 5) Set the maven home M2_HOME=

6) Run "mvn --version" to verify that it is correctly installed.

What is Archetype?
In short, Archetype is a Maven project templating toolkit. An archetype is defined as an original pattern or model from which all other things of the same kind are made. The names fits as we are trying to provide a system that provides a consistent means of generating Maven projects. Archetype will help authors create Maven project templates for users, and provides users with the means to generate parameterized versions of those project templates. Using archetypes provides a great way to enable developers quickly in a way consistent with best practices employed by your project or organization. Within the Maven project we use archetypes to try and get our users up and running as quickly as possible by providing a sample project that demonstrates many of the features of Maven while introducing new users to the best practices employed by Maven. In a matter of seconds a new user can have a working Maven project to use as a jumping board for investigating more of the features in Maven. We have also tried to make the Archetype mechanism additive and by that we mean allowing portions of a project to be captured in an archetype so that pieces or aspects of a project can be added to existing projects. A good example of this is the Maven site archetype. If, for example, you have used the quick start archetype to generate a working project you can then quickly create a site for that project by using the site archetype within that existing project. You can do anything like this with archetypes. You may want to standardize J2EE development within your organization so you may want to provide archetypes for EJBs, or WARs, or for your web services. Once these archetypes are created and deployed in your organization's repository they are available for use by all developers within your organization.

What makes up an Archetype?
Archetypes are packaged up in a JAR and they consist of the archetype metadata which describes the contents of archetype and a set of Velocity templates which make up the prototype project. If you would like to know how to make your own archetypes please refer to our Guide to creating archetypes.

How do I make my first Maven project?
To create our first Maven project we are going to use Maven's archetype mechanism. An archetype is defined as an original pattern or model from which all other things of the same kind are made. In Maven, an archetype is a template of a project which is combined with some user input to produce a working Maven project that has been tailored to the user's requirements.

mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=my-app

Substitute the words in bold to your company requirements.

To create an web application use: mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp

How do I use external dependencies?
For each external dependency, you'll need to define at least 4 things: groupId, artifactId, version, and scope. The groupId, artifactId, and version are the same as those given in the pom.xml for the project that built that dependency. The scope element indicates how your project uses that dependency, and can be values like compile, test, and runtime. For more information on everything you can specify for a dependency, see the Project Descriptor Reference. For more information about the dependency mechanism as a whole, see Introduction to Dependency Management.

com.mycompany.app my-other-app ...   ...      com.mycompany.app my-app 1.0-SNAPSHOT compile For each external dependency, you'll need to define at least 4 things: groupId, artifactId, version, and scope. The groupId, artifactId, and version are the same as those given in the pom.xml for the project that built that dependency. The scope element indicates how your project uses that dependency, and can be values like compile, test, and runtime.

With this information about a dependency, Maven will be able to reference the dependency when it builds the project. Where does Maven reference the dependency from? Maven looks in your local repository (~/.m2/repository is the default location) to find all dependencies. Example in MS Windows: “C:\Dokumente und Einstellungen\username\.m2\repository”

What about dependencies built somewhere else?
How do they get into my local repository? Whenever a project references a dependency that isn't available in the local repository, Maven will download the dependency from a remote repository into the local repository. You probably noticed Maven downloading a lot of things when you built your very first project (these downloads were dependencies for the various plugins used to build the project). By default, the remote repository Maven uses can be found (and browsed) at http://www.ibiblio.org/maven2.

You can also set up your own remote repository (maybe a central repository for your company) to use instead of or in addition to ibiblio. For more information on repositories you can refer to the Introduction to Repositories.

Installing 3rd party JARs
Often times you will have 3rd party JARs that you need to put in your local repository for use in your builds. The JARs must be placed in the local repository in the correct place in order for it to be correctly picked up by Maven. To make this easier, and then error prone, we have provide a goal in the install plug-in which should make this relatively painless. To install a JAR in the local repository use the following command:

mvn install:install-file -Dfile= -DgroupId= -DartifactId= -Dversion= -Dpackaging=