Maven command-line tips
In an ever-growing sea of parameters that one needs to memorize for tens or even hundreds of command-line programs I decided to keep a list of some of the more useful and frequently used Maven parameters in case I forget anything.
This should be run from a parent POM’s directory. It lets you view the POM file that would be in effect if you were building the project. The result is a composite POM containing all your child POMs that you defined as modules in the parent POM.
Below is another useful way to track down build issues, specifically dependency problems. You get a list of dependencies that you specified for your project and all the indirect dependencies that are needed for this build. Don’t forget to specify a profile with the -P option if you’re using one, as it may alter the list of dependencies that are used.
You can also analyze the dependencies used in a project and find out which ones are unused or undeclared, simply execute the following.
Sometimes one needs to compare the dependencies of several projects. In such a situation an alphabetically ordered dependency resolution really helps. There’s one goal for your library dependencies and one for the plug-in dependencies.
mvn dependency:resolve mvn dependency:resolve-plugins
When using profiles there’s a way to find out which ones are being used by Maven for your build. This is useful if you have some profiles that are automatically activated by a trigger other than you specifying them on the command line.
One thing to note about Maven is when you execute a phase it executes all the other phases and goals leading up to the one specified. For example the line mvn package would execute the phases validate, compile, test and package. In contrast executing a specific goal like mvn jar:jar does not invoke any other phases or goals.
Maven pulls a lot of libraries and plug-ins from online repositories, but sometimes it’s useful to do an offline build if you don’t have access to the Internet or just for the sake of speed, because Maven wouldn’t try to check each library for new version. To prepare for the offline build execute the following goal:
Actually if you’ve done the build before and haven’t added any new libraries you could probably skip the above step and still get a working build, but doing this is recommended as Maven does a thorough check and makes sure that you really have everything you need for the offline build. The actual build is dead-simple, just add the parameter -o to the command line that you would usually use to build the project.
mvn -o clean install
When you’re using Maven in a combination with some source control manager (and you should) you can recursively update your project and sub-projects provided of course that you have configured the SCM in your parent POM.
If you need Maven can also generate diff files (comparing your local changes with the last revision) to be used when fixing a bug and submitting a patch or similar scenario.
One way to avoid remembering the long archetype lines is to use the following which lets you choose from a menu of archetype catalogs and then asks you for some data like group, artifact identifiers that is used to generate a new project.
If you wish to create your own archetype using your current project as a template execute this line.
Maven is an extensive tool and has parts of help built into it. For example you can get more information about property variables that can be used in a POM file by executing the following.
If you’re not sure about what phases Maven executes when you specify some phase you can use the following to get a list of phase for the current lifecycle.
mvn help:describe -Dcmd=install
Or you can get more information about the specific plug-in including the goal it supports by issuing.
mvn help:describe -Dplugin=org.apache.maven.plugins:maven-jar-plugin
And last but not least here are a few pages that help you find a library needed for your project in a public Maven repository. Though if you’re serious about Maven it is highly recommended to have some kind of local repository manager like Nexus.