Intro to Maven Shell

23032010

IntroductionSonatype has released a nifty utility called Maven Shell that allows developers to execute maven goals/phases in a pre-loaded shell environment, significantly reducing the build time. It does this by loading up a JVM, Maven, and any loaded plugin once and leverages that existing load for each subsequent build. I downloaded the .10 binary and played around with it with a very simple multi-module project. Here is a quick primer on some of the things you can expect from Maven Shell.

Efficiency
I used a simple multi-module JEE project shell that’s mostly void of code and unit tests to test out efficiency. Because some of the overhead associated with a build is loading numerous plugins, I wanted to run this build at least one time before I started comparing it to a build outside of Maven Shell. My first build ran as such, taking 7.420 seconds:

That’s about a decrease of ~4 seconds execution time for this very simple build, with hardly any plugins loaded. I’d imagine multi-module builds with numerous plugins executed in Maven Shell would see an exponential decrease in time to finish.

Note: After completion of this blog, I downloaded the source for Maven Shell 0.11 SNAPSHOT and built that using the .10 binary, to test these efficiency increases on an actual project with real source and real plugin inclusions. I had even better results. The initial mvnsh clean install took ~23 seconds, each subsequent clean install took ~13 seconds and the standard command line build took about ~20 seconds. That’s a savings of about ~7 seconds on every build!

NavigationMaven Shell provides a bash-esque shell for navigation and binary execution, at least on my Mac; I imagine it would be the same on Linux distros (Windows?). You navigate as you would in a regular shell, using the cd command. The command prompt will look something similar to this :

mvnsh(/logging/logger):~/Projects/fyb_maven_workspace/multi-example>

The mvnsh prefix helps identify to the user they are in the Maven Shell environment. The value in the parenthesis identifies which command group the user is currently in. Currently we are in the /logging/logger command group. We’ll cover command groups in a subsequent section. After the command group and colon, the current location in our file system is identified. In our example, we are in the ~/Projects/fyb_maven_workspace/multi-example directory.

Executing A CommandExecuting a command is as easy as it is in the bash shell, simple type the name of the command and any parameters and press return. Changing a directory:

Help
Typing help or ? and pressing return will provide the help menu. The help menu will identify all the commands available to the user. To receive help on a specific command, type help command. For example, to receive help on rm, type help rm and the user will be provided with a man page on the rm command.

Something very important to know is that help is command group aware. If the current command group is /pref, not only will you see all the default commands available, you will be presented with the commands available to the pref command group.

Command Groups
Command groups are a logical way of grouping like commands together, in a hierarchical fashion. It allows for easy execution of specific commands within a command group. For example, the /logging/logger command group has the following commands:

mvnsh(/logging):~/Projects/fyb_maven_workspace/multi-example> help logger
Help pages in group logger:
set Set the level of a logger.
list List loggers.
levels List valid logger levels.

If we are in the /logging/logger command group, we can execute the levels command to print out the list of logging levels available to the Maven reactor during a build:

FeaturesThere is a laundry list of features on the Maven Shell wiki site, look for a subsequent blog on these features in detail.

Conclusion
If you’re a developer using Maven and do a lot of your interaction with Maven from the command line, I highly recommend taking a look at Maven Shell. It’ll shave precious time from builds allowing you to accomplish more in your day all the while providing a nice interface for interacting with Maven. Keep in mind, it won’t speed up unit tests, plugin execution, etc, but will remove the overhead associated with loading these items every launch.

Note: Matthew McCullough responded below that Maven Shell does indeed use Maven 3 binaries (jars). However, from what I’ve read, it seems as though the Maven 3 developers have put significant effort into making Maven 3 backwards compatible. Your Maven 2 based projects should still work (and the ones I’ve tested with certainly do still build appropriately).

Maven Shell is indeed a Maven-3-only tool. It uses the Maven 3 binaries (JARs) at its very heart. Such a tool was not fully possible with Maven 2.

It is true that I’m looking to refocus future efforts of myself and other folks who contributed to maven-cli-plugin (for Maven 2) onto Maven Shell (for Maven 3). Maven Shell is a much more promising platform for the next few years and offers far more extensibility then we could achieve with maven-cli-plugin.

I went back and re-read where I thought Jason van Zyl stated it would run on Maven 2 and the point was it supports Maven 2 based projects through the Maven 3 binaries’ backwards compatibility feature. On the feature list in the source in github, it states it includes a Maven 3 binary. However, Maven Shell can be built with Maven 2. What comes first, the chicken or the egg? :-D

Think you could step back for those who are still stuck in the Cruise world and write on the significant differences between the Cruise and the Maven worlds?

This all sounds fantastic to me, but how can I pitch this in the face of inertia? i.e. “what we have now works well enough” (It doesn’t) I’m considering it because the company is at a tipping point and is heading into a great time for change…

Cruisecontrol and Maven are really two separate (but potentially complimentary) tools. Cruisecontrol offers scheduling an execution of “processes” (whether those processes represent traditional commandline scripts, ant builds, or other things). Maven is a dependency management and build system. Using “convention over configuration” you can very easily define project dependencies, build process and build artifact management.

So the two tools can work together. You can use Cruisecontrol to schedule and execute Maven builds (although, in my opinion Cruisecontrol offers significantly better support for Ant than Maven). If you’re looking for alternatives to Cruisecontrol I would HIGHLY recommend Hudson ( http://hudson-ci.org/ ) which takes literally minutes to download an learn, and is extremely easy to setup. It supports all the things that Cruisecontrol does and much more.

There are a bunch of commands which were not included into the mvnsh distribution, exec, cat, grep and script are some that exist already (which users have asked for) but were not included. We’ll be adding those into the next release by popular demand.

Hey there! I could have sworn I’ve been to this site before but after reading through some of the post I realized it’s new to me.
Anyhow, I’m definitely delighted I found it and I’ll be book-marking and checking back
frequently!