Maven3 alpha and CLI plugin for Maven2
By Andrius Miasnikovas
Today I tried out the new Maven-3.0-alpha-6 on a multi-module project which uses profiles inside POM files and profiles.xml file. The first thing I noticed that Maven3 still uses the .m2 directory for local repository. I kind of thought they would change that, but I guess this is good for backward compatibility. Another thing that immediately strikes you is that Maven became more strict. I was getting warning messages because the POM file wasn’t living up to Maven’s standards of well defined build script. Bellow the warning there was also a line encouraging me to fix them because in later Maven versions this might break my build. All those warnings actually worked out for the best since now I have a well formatted POM file. The speed of the build didn’t really improve much, but there is a very nice feature that allows you to resume your build. If one of your modules in a multi-module project failed to compile you could fix the problem and resume build with the following line
mvn <goal> -rf :my-module
There’s another change that I have mixed feelings about – support for Maven profiles defined in profiles.xml file has been dropped. On one hand that’s a good thing because it did bring it’s share of problems like profile resolution from a child module that exists structurally deeper in you project folder. On the other hand it’s easier to have profiles defined in a separate file and share them with the team than say having the profiles in a settings.xml file. As you might already know Maven 3 introduces the possibility to write POM files in Groovy which might be more readable in some cases, but I wouldn’t go rewriting my existing POM files just because it’s possible. It’s almost as if it’s becoming a trend and tools and frameworks are expanding the way they can be configured by using the programmatic approach, e.g. Spring 3.0 introduces Java-based configuration which allows bean definition and creation in a Java file with @Configuration annotation.
As Maven3 is still in it’s alpha stage there’s no rush to upgrade the actual development environment, Maven 2.2.x is still my main day-to-day tool. Today I came across an interesting blog entry that talks about using command-line interface (CLI) for Maven. The idea behind it is to save build time by eliminating the startup cost for Maven. Do note that the article is a bit old and the actual settings that worked for me are the following. First we configure the plugin repository that we’ll use.
<pluginRepositories>
<pluginRepository>
<id>twdata-m2-repository</id>
<name>Twdata Repository</name>
<url>http://twdata-m2-repository.googlecode.com/svn/</url>
</pluginRepository>
</pluginRepositories>
Then we include the actual CLI plugin.
<plugin>
<groupId>org.twdata.maven</groupId>
<artifactId>maven-cli-plugin</artifactId>
<version>1.0.3</version>
</plugin>
Then all that’s left is executing the CLI plugin while inside your project folder.
mvn cli:execute-phase
This will give you a prompt line where you’ll be able to execute all of maven’s goals without the mvn prefix like.
maven2> clean install -Pmy-profile
I noticed one thing that you cannot leave a space between the -P and the profile name when specifying the profiles. Maven allows you to do it both ways, but this is CLI plugin so the rules are a bit different. The nice feature is that it allows you to specify aliases for your long build lines which is very useful for people like me who don’t like remembering long parameter lines.