December 20, 2008

Maven2 Goodies

Here is a short summary of good Maven2 goodies that I have came across.

The imported dependencies are located at the Central Maven2 Repository: http://repo1.maven.org/maven2/

Archetypes
The simplest way to start a new project is to use an archetype. To browse available archetypes go to http://docs.codehaus.org/display/MAVENUSER/Archetypes+List

Plugin
One good thing about Maven except the standirezed directore structure is all the plugin to maven.

List of all Maven Internal Plugins: http://maven.apache.org/plugins/
External Plugin Search Page: http://mvnrepository.com/

To list available goals for a plugin:

mvn help:describe -Dplugin=jaxb2

To list available configurations for a plugin:

mvn help:describe -Dplugin=jaxb2 -Dfull

Source and Javadoc
How to add source files and javadoc to maven project:

mvn dependency:sources

And when to generate eclipse project

mvn eclipse:eclipse -DdownloadSources=true -DdownloadJavadocs=true

Name convention for source and javadoc resources:

foo-1.0.jar (or whatever artifact is referred to in the pom.xml)
foo-1.0-sources.jar (optional, jar containing java sources)
foo-1.0-javadoc.jar (optional, jar containing javadocs)

Dependency
When using Maven the number of dependencies tends to grow and after a while there is no way to tell if a jar file is really needed, but there is, use the below command to resolve unuzed dependency. On caution is that some jar file are not directly referenced from the code, but rather dynamically loaded during runtime. I have seen that these are not always correctly resolved, so always test your system after removing a dependency.

 
mvn dependency:resolve
...
[WARNING] Unused declared dependencies found:
[WARNING] javax.xml.bind:jaxb-api:jar:2.1:compile
[WARNING] com.sun.xml.bind:jaxb-impl:jar:2.1.9:compile
...


mvn dependency:tree
...
[INFO] [dependency:tree]
[INFO] se.msc:xml:jar:0.0.1-SNAPSHOT
[INFO] +- junit:junit:jar:4.5:test
[INFO] +- javax.xml.bind:jaxb-api:jar:2.1:compile
[INFO] | +- javax.xml.stream:stax-api:jar:1.0-2:compile
[INFO] | \- javax.activation:activation:jar:1.1:compile
[INFO] \- com.sun.xml.bind:jaxb-impl:jar:2.1.9:compile
...

Eclipse Maven Integration - m2eclipse
If you are developing with eclipse and using Maven2 you should use m2eclipse plugin.

Some times when creating new workspace the central maven repository disappear, you experience that when getting zero find result, when adding a new depency. To fix that open View 'Maven Index' and add the central maven repository.

Building Standalone with Maven2
If you are building a standalone app and want to run it from the command line, and you want to get all dependencies classpath, execute the following command.

mvn dependency:build-classpath