The most usage Maven commands

Maven is our most use built tool besides the Gradle. There are a lot Maven commands to help developers build, run tests or create a new distribution. Except the default commands such as clean install I found that there are only a few commands I always use them in my jobs, and this blog post demonstrate how I use these Maven commands. You can add your commands later in the comment.

Check the new dependency updates

Keep your libraries up-to-date is the recommend. Before we start the new Sprint, we always run this command to check whether my project dependencies have the newer versions

By running in the top parent Maven project, you can get the list of newer dependencies in your all projects. Below is the sample output of this command per one project I am working on

We do not use the alpha, beta or milestone version in our production release, so we consider all of our software are up-to-date.

Change your projects version

We usually use the below Maven commands when we start to release the new product version. If you have only one Java project, you just simply use this command

Where x.y.z is the notation of the project version. But the task is a bit complicated when we have multi-modules Maven projects, while you change the version of the aggregator project, the parent and child modules versions do not change. For instance, our Maven project structure as following

In our example mycollab-parent-community is the parent file of all other Maven files.

To change the versions of all project, first of all, you must change the version of the maven parent project, in this case; our project is mycollab-parent-community

Then I run the exact command but with the aggregator maven file mycollab-deployer-community

It almost is done! There are several child modules have unchanged versions.  The command version:update-child-modules run on the aggregator project will help we fix these inconsistent changes

Finally, we run the command versions:commit in the aggregator maven file to remove the backup pom files

Maven files are verbose, but it is easy to set up a build, integrate with the continuous integration server, and keep your project layout are standard. I used a more few Maven plugins but at the end above are the most usage commands in my project.

Resolving the dependency conflict or redundant dependency

If you use Maven, Gradle or any build tool, the transitive dependencies and the Maven dependency resolver may cause the problem. The most common issues are:

  • Maven bundles the wrong version of a dependency. This issue may cause the NoClassDefFoundError or NoSuchMethodException at the runtime
  • Maven includes the redundant dependency which is required for another dependency but it is not needed for your application. It will cause your distribution file is larger, and the abnormal issue when the application runs.

The maven command dependency:tree is very useful to solve both conflicting versions and redundant dependencies.

Following is an example of running the command dependency:tree, analyzing the dependency hierarchy will help you to resolve any potential conflict version or redundant dependencies. This process may take time to learn and run the application several times to check whether there is no dependency use the removed library.


Leave a Reply

Your email address will not be published. Required fields are marked *