Understanding Maven

What is Maven?

Maiven is a:

  • build tool
  • project management tool

Maven was started as a way to simplify the build process in the jakarta turbine project and has evolved to be a software project management tool which is based on a project object model (pom).

  • build
  • test
  • deploy

The pom document (xml file) describes all

  • dependencies
    				
    				<dependencies> 
  • plug-ins
    	
    <build>
    	<plugins>
    		...
    	
    	
  • svn
  • repositories
  • properties
    	
    <properties>
    	<spring.version>3.0.5.RELEASE</spring.version>
    	<spring-security.version>3.0.2.RELEASE</spring-security.version>
    	...
    </properties>
        
    	

of the project.

Common Activities:

  • creating the project structure (mvn archetype:generate)

  • building, publishing and deploying

  • multiple jars 

  • dependencies and versions

Plug-ins execution framework

Maven can also be thought of as a plug-in execution framework that coordinates the execution of plug-ins in a well-defined way.

Basically everything accomplished in maven is the result of a plug-in executing. For example using the tomcat application container to test a web application maven provides a plug-in for this container, so we don't have to bother to install it.

To ran the tomcat plug-in:

mvn tomcat:run

(stopped by <ctrl-c> or mvn tomcat:stop)

There are plug-in for:

  • compiling the source code,

  • for running tests,

  • for creating jars,

  • for creating javadocs,

  • for running an application container

  • etc.

Dependencies management system

It allows us to include all of our libraries automatically in our project, without having to manage them. to import dependencies we need to add xml tags to our pom xml file, specifying the groupid, artifactid, and the version of the dependency itself.

Nomenclature

  • artifactid ==> the name of the project (e.g. myproject_app)

  • groupid ==> the path were the project (e.g jar or war files) will be saved once deployed or installed locally, which follows the package name structure (e.g. com.puglieseweb.myproject)

  • (artifact) repositories ==> maven tries to promote the notion of a user local repository where jars, or any project artifacts, can be stored and used for any number of builds.

if we don’t configure any repository into our pom file, maven will use the default repositories set in the {maven home}/setting.xml file.

maven repositories:

  • hold build artifacts and dependencies of varying types.

  • there are only two types of repositories: local and remote.

  • the local repository: refers to a copy on your own installation that is a cache of the remote downloads, and also contains the temporary build artifacts that you have not yet released.

  • remote repositories refer to any other type of repository, accessed by a variety of protocols such as file:// and http://.

maven first looks for the dependencies in a local repository, then in remote repositories. otherwise we have either to find the right repository or install it manually.

we can always download the dependency/library and install it locally on our machine.

      1. maven archetype

  • archetype ==> a template for our project that is reusable for similar projects. they are downloaded and installed local repository. for example there are archetypes to create java spring-hibernate, velocity, gwt projects and more.

you can either create an archetype based on an exisistin project,:

mvn archetype:create-from-project

or manually create an archetype from scratch.

  1. the archetype descriptor (in meta-inf/maven/archetype.xml): decribe how to construct a new project form the archetype-resources provided.

  2. the template project in archetype-resources: the jar that is built is composed only of resources, so everything else is contained under src/main/resources

deployment

the clean, install and deploy phases are valid lifecyclephases and invoking them will trigger all the phases preceding them, and the goals bound to these phases.

mvn clean install

this command invokes the clean phase and then the install phase sequentially:

  1. mvn clean install

tells maven to build all the modules and to install them into the local repository.

  1. mvn clean

removes files generated at build-time in a project's directory (target by default)



mvn install

put our packaged maven project into a local repository, for local application using this project as a dependency.

mvn release

basically put the current code in a tag on our scm (source control management system), change the version in your projects.

  1. mvn deploy

put your packaged maven project into a remote repository for sharing with other developers.

  1. deployment (release plug-in)

relasing software is often a difficult and tedious exercise (you have to include dependencies, database, view, ans so forth)

maven provide a release plug-in that provides the basic functions of a standard release process.

  1. mvn prepare & mvn perform (maven release plug-in)

this command are not a valid phase of maven. but if refers to the mavenreleaseplugin that is used to automate and simplify release management.

releasing a project is done in two steps: prepare and perform.

preparing a release goes through the following release phases:

  • check that there are no uncommitted changes in the sources

  • check that there are no snapshot dependencies

  • change the version in the poms from x-snapshot to a new version (you will be prompted for the versions to use)

  • transform the scm information in the pom to include the final destination of the tag

  • run the project tests against the modified poms to confirm everything is in working order

  • commit the modified poms

  • tag the code in the scm with a version name (this will be prompted for)

  • bump the version in the poms to a new value y-snapshot (these values will also be prompted for)

  • commit the modified poms

and then: mvn perform

performing a release runs the following release phases:

  • checkout from an scm url with optional tag

  • run the predefined maven goals to release the project (by default, deploy site-deploy