User Tools

Site Tools


java:maven

This is an old revision of the document!


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.

I've came across a fantastic book: Maven, the definitive guide. Absolutely recommendable. Here is the link to amazon:

http://www.amazon.com/Maven-Definitive-Guide-Sonatype-Company/dp/144936280X/ref=sr_1_3?ie=UTF8&qid=1363040267&sr=8-3&keywords=maven%2C+the+definitive+guide

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:

  • Archetype plugin
  • Jar plugin
  • Compiler plugin
  • Test plugin (Surefire)
  • Hibernate3 plugin
  • jRuby

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:

  • It is necessary to create the src/main/groovy folder and put the Groovy sources in it
  • You have to label this folder as “source folder” (right click button, build path, use as source folder
  • To support them in eclipse, I've installed the “Groovy/Grails Tool Suite for Eclipse”

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.

java/maven.1419414127.txt.gz · Last modified: 2022/12/02 22:02 (external edit)