Table of Contents

Notes on Maven

Intro

Hi, these are my notes on Maven. Probably here are a lot of inaccuracies, because I am using this to clarify myself and to help me to order my mind in the process of using this tool in my projects.

Some notes of the Maven Architecture

Plugins and Goals

Maven uses a lot of plugins.

Plugins are jar files who make things for Maven.

Examples of plugins are:

Plugins have goals. For example, the compiler plugin have the compile goal. To invoke this goal, you just have to call mvn compiler:compile.

Maven lifecycle

Every project has his lifecycle, which is of course configurable. There can be many.

This is the default lifecycle:

process-resources 
   |
   + compile
        |
        + process-classes
             |
             + process-test-resources
                  |
                  + test-compile
                       |
                       + test
                            |
                            + prepare-package
                                 |
                                 + package

Of course, there is a correspondence between phases and goals. Here are ilustrated some of them:

process-resources -------------------------> resources:resources
   |
   + compile ------------------------------> compiler:compile
        |
        + process-classes
             |
             + process-test-resources -----> resources:testResources
                  |
                  + test-compile ----------> compile:testCompile
                       |
                       + test -------------> surefire:test
                            |
                            + prepare-package
                                 |
                                 + package > jar:jar

Getting Help

You can get help from Maven itself. There is a plugin used to get help from the different goals available.

mvn help:describe -Dplugin:PUT-HERE-THE-PLUGIN

Migrating an existing project to maven

These are my notes about migrating an existing, not mavenized project into maven. I've selected a project which is relatively simple: it only run as a batch program, it doesn't have GUI or web interface.

Let's start with it.

I've created a new project with:

rluna@petrus:~/work-mvn$ mvn archetype:create \
                   -DgroupId=com.supermanhamuerto.nestor \
                   -DpackageName=com.supermanhamuerto \
                   -DartifactId=jnestor 

Some changes in the ''pom.xml'' file

The version of this project is currently the 3.0. If I succeeded, I rather prefer to have it in the version 4.0: mostly because it is more than probable that many changes will be made, including changes in the structure of directories, methods for deploying the application and things like this.

I've changed the version:

<version>4.0</version>

The name, website and other details:

  <name>Jnestor</name>
  <url>http://www.jnestor.supermanhamuerto.com/</url>
  <description>
	Another Majordomo in Java
 
	JNestor is a software developed in java whose 
	main purpose is to read a mailbox and perform 
	some automatic functions. I've started this 
	project with the idea of serving as an interface 
	between Manits Bug Tracker and any mailbox.  
 
  </description>
  <inceptionYear>2012</inceptionYear>

License info (I think it's important to have declared here):

  <licenses>
  	<license>
  		<name>Apache 2</name>
  		<url>http://www.apache.org/licenses/LICENSE-2.0.html</url>
  	</license>
  </licenses>

Mailing list:

  <mailingLists>
  	<mailingList>
  		<name>jnestor-general</name>
  		<post>jnestor-general@lists.sourceforge.net</post>
  		<subscribe>https://lists.sourceforge.net/lists/listinfo/jnestor-general</subscribe>
  		<unsubscribe>https://lists.sourceforge.net/lists/listinfo/jnestor-general</unsubscribe>
  		<archive>http://sourceforge.net/mailarchive/forum.php?forum_name=jnestor-general</archive>
  	</mailingList>
  </mailingLists>  

Next, copy the source code

I've copied the Java source code into the src\main\java\ project created. This will be my first stage.

Open the project in eclipse

Then, I've opened the project in eclipse in order to manipulate the project in a more “normal” way. I've been using eclipse since I've started in Java, so don't ask me about other IDEs.

The eclipse I have right now is the Juno version with the Maven Integration for Eclipse plugin in order to execute mvn install from the ide, update the pom.xml file directly from eclipse and –of course– debug the application directly from eclipse.

So, open eclipse and File → import → Existing Maven Projects.

In Root directory, select the workspace you are working in and select the “new project you have created:

Click on finish and that's it.

Compiler version

Sorry, but I prefer to compile and run the source code in version 1.6, so I edited the pom.xml file and configure it:

  <build>
  	<plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
           <source>1.6</source>
           <target>1.6</target>
        </configuration>
        <version>3.0</version>
      </plugin>
    </plugins>
  </build>

Dependencies, dependencies, dependencies

Once I've migrated my source code into the new project, all the jar's and libraries that I have in my original project are dissapeared: I have to provide these dependencies with maven. Let's go for. I head to www.mvnrepository.com (or http://search.maven.org) and looked for them in order to fill the gaps.

  <dependencies>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-core</artifactId>
		<version>3.2.2.RELEASE</version>
	</dependency>
    [....]
    <dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>4.11</version>
		<scope>test</scope>
    </dependency>
  </dependencies>

Check again

I've run a mvn compile each time I've inserted a new dependency an checked in eclipse that the number of errors in the project decreased. At the end, I only had two errors: one belongs to a class which is deprecated, so I don't mind to have it, and another belongs to a class called commons-lang: I use version 3 and the highest available version on the maven repository is version 2.6.

Add Groovy

Part of the project are two scripts in groovy, so let's add them. I've started by creating the folder main/groovy in order to put there the scripts in Groovy.

I have to add also the groovy dependency to my project:

	<dependency>
		<groupId>org.codehaus.groovy</groupId>
		<artifactId>groovy-all</artifactId>
		<version>2.1.1</version>
	</dependency>

Run, test...

Using eclipse I've run the project many times: several errors appeared: missing dependencies that have to be worked out, a problem with the groovy scripts and the problem of locating the applicationContext.xml file. I will document the later.

Problem with the groovy scripts

As they are another language, I had to do several things in order to integrate the groovy:

Problem with applicationContext.xml

The file applicationContext.xml must be allocated under src/main/resources.

Useful commands

mvn dependency:copy-dependencies -DoutputDirectory=target/lib

Copies all the dependencies –aka jar files– and copies them into the target/lib directory. Useful for get all the jars to run externally your project.

A simple project for download dependencies only

This simple project is for download dependencies of the maven central repository easily. Just put the dependency you want to download into the dependencies section of the project and execute mvn package: the dependencies will appear under target/lib directory.

<project 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">
  <modelVersion>4.0.0</modelVersion>
 
  <!--
 
  mvn package            -> creates the jar file and copies the dependencies into target/lib
 
  -->
 
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
 
  <name>Maven Quick Start Archetype</name>
  <url>http://maven.apache.org</url>
 
  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.4.RELEASE</version>
    <scope>test</scope>
    </dependency>
  </dependencies>
 
  <build>
  <plugins>
  	  <!--
 
        dependency plugin: copy all the dependencies into the target/lib directory
 
	  -->
      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.7</version>
          <executions>
              <execution>
                  <id>copy-dependencies</id>
                  <phase>package</phase>
                  <goals>
                    <goal>copy-dependencies</goal>
                  </goals>
                  <configuration>
                      <outputDirectory>${project.build.directory}/lib</outputDirectory>
                  </configuration>
              </execution>
          </executions>
      </plugin>
 
  </plugins>
 
 
    <pluginManagement>
        <plugins>
            <plugin>
              <groupId>org.eclipse.m2e</groupId>
              <artifactId>lifecycle-mapping</artifactId>
              <version>1.0.0</version>
 
              <configuration>
                <lifecycleMappingMetadata>
                    <pluginExecutions>
                        <pluginExecution>
                            <pluginExecutionFilter>
						          <groupId>org.apache.maven.plugins</groupId>
						          <artifactId>maven-dependency-plugin</artifactId>
						          <versionRange>[2.0,)</versionRange>
						          <goals>
						              <goal>copy-dependencies</goal>
						          </goals>                        
                            </pluginExecutionFilter>
                            <action>
                                <execute/>
                            </action>
                        </pluginExecution>
                    </pluginExecutions>
                </lifecycleMappingMetadata>
              </configuration>
 
            </plugin>        
        </plugins>
    </pluginManagement>    
 
 
  </build>
</project>