Wednesday Jun 13

AddThis

AddThis Social Bookmark Button

Maven

AddThis Social Bookmark Button

Maven Tutorials on Java Tutorials

Maven
is a software tool for project management and build automation.

While primarily used for Java programming, it can also be used to build and manage projects written in C#, Ruby, Scala, and other languages. Maven serves a similar purpose to the Apache Ant tool, but it is based on different concepts and works in a profoundly different manner.

Maven is hosted by the Apache Software Foundation, where it was formerly part of the Jakarta Project. Maven uses a construct known as a Project Object Mode l (POM) to describe the software project being built, its dependencies on other external modules and components, and the build order. It comes with pre-defined targets for performing certain well-defined tasks such as compilation of code and its packaging.

Maven dynamically downloads Java libraries and Maven plugins from one or more repositories. Maven provides built-in support for retrieving files from the Maven 2 Central Repository and other Maven repositories, and can upload artifacts to specific repositories after a successful build.

A local cache of downloaded artifacts acts as the primary means of synchronizing the output of projects on a local system. Maven is built using a plugin-based architecture that allows it to make use of any application controllable through standard input.

Theoretically, this would allow anyone to write plugins to interface with build tools (compilers, unit test tools, etc.) for any other language. In reality, support and use for languages other than Java has been minimal. Currently a plugin for the .Net framework exists and is maintained, and a C/C++ native plugin is maintained for Maven 2 maven-native and maven-nar

Source : Wikipedia

Last Updated on Sunday, 06 May 2012 11:20
Sébastien Dante Ursini
PDF Print E-mail
Monday, 11 April 2011 10:05
Written by Sébastien Dante Ursini
AddThis Social Bookmark Button

Maven release plugin tutorial

Maven Release Plugin introduction

This Maven release plugin tutorial shows you how to tag your Maven project in Subversion and release the versioned jar to a remote repository (Nexus), and all of this with a single maven command! You can even automate the entire release process by running the command in a cronjob or a Continuous Integration system. Find below a cookbook on the plugin configuration and some maven release plugin tips and troubleshooting.

Source code management configuration

Maven release tutorial scm configuration

<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/maven-v4_0_0.xsd">
    ....
    <scm>
        <connection>scm:svn:https://javaquity.svn.sourceforge.net/svnroot/javaquity/ubiteckSample/mvc/trunk</connection>
        <developerConnection>scm:svn:https://javaquity.svn.sourceforge.net/svnroot/javaquity/ubiteckSample/mvc/trunk</developerConnection>
        <url>scm:svn:https://javaquity.svn.sourceforge.net/svnroot/javaquity/ubiteckSample/mvc/trunk</url>
    </scm> 
    .... 
</project>

Define in line 4-6 the url of your source code management system.

if you have an anonymous user you can checkout your project using the scm plugin . To checkin and release you probably need to define the user and password of your scm repository in your settings.xml file as described below

Maven release plugin server configuration

<settings>
    ...
      <servers>
        ...

        <server>
            <id>myproject.myhost.com</id>
            <username>scp-login</username>
            <password>scp-password</password>
        </server>
    </servers>
    ...
</settings>


Define in line 7 the id of the server. It should be equals to the host name of your scm url.
In line 8 and 9 the login and password to your scm repository

Maven release versions

First what does mean SNAPSHOT ? SNAPSHOT is a special version in Maven that indicates the latest code; typically TRUNK or HEAD in your source control. With this version, Maven will automatically grab the latest SNAPSHOT every time you build. On the other hand, when you are using 2.0, once maven has downloaded this artifact, it never tries to get a new 2.0.

In this concept, a version just has a -SNAPSHOT suffix which marks the version as a SNAPSHOT (or non-release) version.

A SNAPSHOT version (e.g. 1.0-SNAPSHOT) would always be assumed more recent than the corresponding release version (e.g. 1.0)

Why should you use SNAPSHOT ? You should use SNAPSHOT for rapidly moving code where bug fixes and enhancements are coming fast.

Releasing a sample

Let's take the version 1.0-SNAPSHOT of our project :

maven pom version

<project>
    ...
    <version>1.0-SNAPSHOT</version>
    ...
</project>

Release prerequisites

  • Make sure that your maven pom.xml does not include any SNAPSHOT dependencies.
  • Make sure that all local changes are checked in in your source code management system.
  • Make sure all unit tests pass

All of the above are required. The next step will gracefully fail with a warning if the above conditions are not met.

Release preparation

This step does everything needed in order to make a Maven release, including tagging the project , but does not actually make the release. It will check for local modifications, SNAPSHOT dependencies, calculate the release version, the next SNAPSHOT version.

In order to create the new tags you need the default subversion directory structure already in place :

Subversion default project structure : branches,tags,trunk

Maven release prepare

mvn release:prepare

You will be asked to specify the version for your release and also the next SNAPSHOT version that you want to assign to your project. Accept the default values if unsure.

In this example we will press two times <enter> key to keep the Maven proposed value

Name Value
SCM Release tag or label mvcSample-1.0
New development version 1.1-SNAPSHOT

Maven release dos console

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building MVC Sample 1 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-release-plugin:2.0:prepare (default-cli) @ mvcSample ---
[INFO] Resuming release from phase 'input-variables'
What is SCM release tag or label for "MVC Sample 1"? (com.ubiteck.sample:mvcSample) mvcSample-1.0: :
What is the new development version for "MVC Sample 1"? (com.ubiteck.sample:mvcSample) 1.1-SNAPSHOT: :
[INFO] Transforming 'MVC Sample 1'...
[INFO] Not generating release POMs
[INFO] Executing goals 'clean verify'...
[WARNING] Moven will be executed in interactive mode, but no input stream has been configured for this MavenInvoker instance.
[INFO] 
[INFO] Scanning for projects...
....

If everything works fine you should see a new tag in your tags folder with the the release version you specified in point 3 above.

Maven will collect all the information that you provide and write a local file called "release.properties". This file lets you continue from where you left in case of errors.

You can undo everything youve done so far with mvn release:clean to start over

Make the maven release

Once youve actually prepared the release using mvn release:prepare, you can make the release by typing the following command

Distribution requirements

You need to have the distribution management attribute defined in your pom. Let's define a nexus repository to release our project

Maven release POM Nexus Repository

<project>
    ....
    <distributionManagement>
           <repository>
              <id>nexus-release</id>
              <name>Nexus Release Repository</name>
               <url>http://www.java-tutorial.ch/nexus/content/repositories/releases/</url>
           </repository>
    </distributionManagement>    
</project>

Then we need to define the corresponding login and password according to the nexus repository

Maven Release Server login Password Configuration

<settings>
    ...
      <servers>
        ...

        <server>
            <id>www.java-tutorial.ch</id>
            <username>nexus-login</username>
            <password>nexus-password</password>
        </server>
    </servers>
    ...
</settings>

And the right connector defined

maven webdav connector

<build> 
    ...
    <extensions> 
        <extension> 
            <groupId>org.apache.maven.wagon</groupId> 
            <artifactId>wagon-webdav-jackrabbit</artifactId> 
            <version>1.0-beta-7</version> 
        </extension> 
    </extensions> 
</build> 

Maven release prepare

mvn release:perform

This command uses release.properties created by the previous step and does the following

  1. Checks out code tagged in the previous step. It figures out the tag version from the release.properties file.
  2. Runs maven deploy site-deploy goals to deploy the released version. This step is responsible for deploying the jar to the remote repository.
  3. Clean up. After the release is done, release.properties file will be deleted.

Congratulation you did your first release !

If you found this article useful please click on the following Google +1 button to promote this website.

Thanks in advance

Other source versionning systems

If you use an SCM such as Subversion, the release plugin will tag the remote repository. Thats fine, but if you are using a distributed SCM such as Git or Mercurial , you will need to change the default behavior which:

  • makes local commits to upgrade the versions,
  • makes a local tag for the new version
  • pushes all the changes upstream

This behavior is awful, as it forces you into a model where you always have an upstream repository. Even if this is your case like most projets do, you are likely to have issues in Maven launching your DVCS push command. You will probably prefer to do it by yourself, which is an extra advantage if you run into an issue at the later stages of the release process, because you can always undo your commits locally.

Include the following snippet in the <build><plugins> section of your project's pom.xml-file to make the release plugin behave as one would expect with a DVCS::

git and maven release

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-release-plugin</artifactId>
    <configuration>
        <localCheckout>true</localCheckout>
        <pushChanges>false</pushChanges>
    </configuration>
</plugin>

Troubleshooting maven release Troubleshooting

Error Release Problem Error Bootstraping - No Goal has been specified for this build...

[ERROR] Failed to execute goal org.apache.maven.plugins:moven-scm-plugin:1.1:bootstrap (default-cli) on project mvcSample: Result of mvn execution is: '1'. Release failed. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Moven with the -e switch.
[ERROR] Re-run Moven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

Did you define a goal for scm plugin

 <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-scm-plugin</artifactId>
         <version>1.1</version>
         <configuration>
           <goals>install</goals>
         </configuration>
 </plugin>

Problem Release Repository Element Repository element was not specified in the POM inside distributionManagement element

Did you define the distribution management attribute in your project object model ?

Maven Release Connector Webdav No connector available to access repository nexus-release (scp://devhcrserv.s-h.ch/nexus/content/repositories/releases/)

Did you define a connector

e.g. : Webdav connector

maven webdav connector

<build> <extension> <extension> <groupId>org.apache.maven.wagon</groupId> <artifactId>wagon-webdav-jackrabbit</artifactId> <version>1.0-beta-7</version> </extension> </extensions> </build> 

Nexus Error 503 Failed to transfer file when you deploy in a nexus repository (return code 503)

Verifications :

  1. You are probably trying to deploy in the wrong repository. e.g. Snashot instead of release
  2. You defined the wrong user/password to access the repository
  3. You have local modification not checked in

Failed to transfer file Return code is: 401 Failed to transfer file Return code is: 401

Failed to transfer file return code 401

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.5:deploy (default-deploy) on project root: Failed to deploy artifacts: Could not transfer artifact ... : 
Failed to transfer .... Return code is: 401 -> [Help 1]



You have an http error 401 unauthorized

Verifications :

  1. You defined the wrong user/password to access the nexusrepository
  2. You don't define a user password to access the nexus repository

Conclusion

If you enjoyed this tutorial and want to promote it don't hesitate to click on

Maven related articles

Last Updated on Wednesday, 27 April 2011 18:11
PDF Print E-mail
Monday, 11 April 2011 09:28
AddThis Social Bookmark Button

Maven Apache

Maven Installation Guides

Maven lifecycle

The default lifecycle phases :

  • validate : validate the project is correct and all necessary information is available
  • compile : compile the source code of the project
  • test : test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
  • package : take the compiled code and package it in its distributable format, such as a JAR.
  • integration-test : process and deploy the package if necessary into an environment where integration tests can be run
  • verify : run any checks to verify the package is valid and meets quality criteria
  • install : install the package into the local repository, for use as a dependency in other projects locally
  • deploy : done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

There are two other Maven lifecycles of note beyond the default list above. They are

  • clean : cleans up artifacts created by prior builds
  • site : generates site documentation for this project

Maven related articles

Last Updated on Thursday, 16 June 2011 17:02
PDF Print E-mail
Monday, 11 April 2011 09:25
AddThis Social Bookmark Button

Maven compile plugin example

Maven Compiler Plugin Configuration

The fist step to build a java application is to be able to compile a maven projet . According to the dependencies created in the POM file (Project object model) Maven is able to define the classpath and compile using the default jdk using the following command :

Maven compile plugin configuration

mvn compile

Maven compile incrementally. An incremental build reuses the results of a previous build to perform an optimized build based on the changes since the last build. If your code has been compiled by Eclipse you can have some surprises due to some tips and tricks used by the Eclipse compiler. To ensure a full compilation by the default Sun JDK you should make a cleanup before compiling. It will delete the previously compiled classes contained in the {project directory}/target/classes

Maven Apache compile plugin tutorial

mvn clean compile

If you want to compile without your Unit test defined in {project directory}/src/test/java your can ask maven to skip the test

Apache Maven compile plugin

mvn compile -DskipTest=true

Compiler version

If you need to define the compiler version you can set the source and target parameter in the Compiler Plugin :

Maven compiler plugin configuration

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
    <source>1.6</source>
    <target>1.6</target>
  </configuration>
</plugin>

Maven Tutorial related articles

Last Updated on Monday, 18 April 2011 07:52
PDF Print E-mail
Friday, 08 April 2011 15:37
AddThis Social Bookmark Button

Maven offer a great way to find unused libraries in your project. You have as well a list of the dependencies undeclared in the project

Find unused jars using maven

mvn dependency:analyse

The result look like

[WARNING] Used undeclared dependencies found:
[WARNING]    xerces:xmlParserAPIs:jar:2.6.2:compile
[WARNING]    javax.persistence:persistence-api:jar:1.0:compile
[WARNING]    com.l2fprod.common:l2fprod-common-shared:jar:6.9.1:compile
[WARNING]    commons-logging:commons-logging:jar:1.1:compile
[WARNING]    jfree:jcommon:jar:1.0.6:compile
[WARNING]    org.swinglabs:swingx-ws:jar:1.0:compile
[WARNING] Unused declared dependencies found:
[WARNING]    org.hibernate:hibernate-commons-annotations:jar:3.3.0.ga:compile
[WARNING]    antlr:antlr:jar:2.7.6:compile
[WARNING]    javax.transaction:jta:jar:1.1:compile
[WARNING]    org.easymock:easymock:jar:3.0:test
[WARNING]    com.kenai.nbpwr:org-jdesktop-swingx-ws:nbm:1.0-201002281504:compile
[WARNING]    com.jyloo:synthetica-addons:jar:0.9.7:compile
[WARNING]    com.jidesoft:jide-oss:jar:2.4.8:compile
[WARNING]    com.oracle:ojdbc6:jar:11.2.0.1.0:runtime
Last Updated on Tuesday, 26 April 2011 20:13
PDF Print E-mail
Friday, 08 April 2011 11:38
AddThis Social Bookmark Button

To obtain your project dependency tree with Maven execute the following maven command :

Maven dependency tree command

>mvn dependency:tree

The result should look like

 +- org.hibernate:hibernate-annotations:jar:3.3.0.ga:compile
  \- javax.persistence:persistence-api:jar:1.0:compile
 +- org.hibernate:hibernate-commons-annotations:jar:3.3.0.ga:compile
 +- org.hibernate:hibernate-c3p0:jar:3.3.0.GA:compile
 +- org.hibernate:hibernate-core:jar:3.3.0.GA:compile
 |  |  +- antlr:antlr:jar:2.7.6:compile
 |  |  \- dom4j:dom4j:jar:1.6.1:compile
 |  +- c3p0:c3p0:jar:0.9.1:compile
 |  \- org.slf4j:slf4j-api:jar:1.4.2:compile
 +- javax.transaction:jta:jar:1.1:compile
 +- jfree:jfreechart:jar:1.0.3:compile
 |  \- jfree:jcommon:jar:1.0.6:compile
 +- org.unhcr.tools:sybase-installer:jar:1.0:compile
 +- org.easymock:easymock:jar:3.0:test
 |  +- cglib:cglib-nodep:jar:2.2:test
 |  \- org.objenesis:objenesis:jar:1.2:test
 +- junit:junit:jar:4.8.2:test (scope not updated to compile)
....

Maven Tutorial related articles

Page 4 of 4