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
|
|
|
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
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
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 �:
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.
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 :
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 |
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 you�ve done so far with mvn release:clean to start over
Make the maven release
Once you�ve actually prepared the release using mvn release:prepare, you can make the release by typing the following command
You need to have the distribution management attribute defined in your pom. Let's define a nexus repository to release our project
Then we need to define the corresponding login and password according to the nexus repository
And the right connector defined
This command uses release.properties created by the previous step and does the following
- Checks out code tagged in the previous step. It figures out the tag version from the release.properties file.
- Runs maven deploy site-deploy goals to deploy the released version. This step is responsible for deploying the jar to the remote repository.
- 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. That�s 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::
Troubleshooting
Error Bootstraping - No Goal has been specified for this build...
Did you define a goal for scm plugin
Repository element was not specified in the POM inside distributionManagement element
Did you define the distribution management attribute in your project object model ?
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
Failed to transfer file when you deploy in a nexus repository (return code 503)
Verifications :
- You are probably trying to deploy in the wrong repository. e.g. Snashot instead of release
- You defined the wrong user/password to access the repository
- You have local modification not checked in
Failed to transfer file Return code is: 401
You have an http error 401 unauthorized
Verifications :
- You defined the wrong user/password to access the nexusrepository
- 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
|
|
|
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 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 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
If you want to compile without your Unit test defined in {project directory}/src/test/java your can ask maven to skip the test
Compiler version
If you need to define the compiler version you can set the source and target parameter in the Compiler Plugin :
Maven Tutorial related articles
|
|
|
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
The result look like
|
|
|
To obtain your project dependency tree with Maven execute the following maven command :
The result should look like
- Maven installation procedure for Windows
- Maven installation guide for Linux
- Maven release
- Maven compilation plugin tutorial
- Find unused jars using Maven
Page 4 of 4