m for mvnw – Running maven wrapper a more convenient way

Maven wrapper is a cool tool thing for running maven builds with the correct maven version. This introduces a script that is already known for gradle. Simply type m on the command line and run the maven wrapper. Even in case the wrapper is not in the current directory.

The script

When using gradle it’s suggested to use the gradle wrapper. It simply runs gradle in the correct version. And there is a maven wrapper that does the same for maven. But I haven’t seen that g script for maven at all.

So there it is:

#!/usr/bin/env sh
D=$(pwd)
M="mvnw"
while [ ! -x "$D/$M" -a "$D" != "/" ]; do
    D=$(dirname $D)
done
if [ ! -x "$D/$M" ]; then
    echo "No Maven found in current or parent directories!"
    exit 1
fi
echo "Run Maven $D/$M ..."
nice -n 1 "$D/$M" $@

Installation

In order to get the system to your environment it simply must be added to your path. Adding it to your path can be achieved by adding the script into a bin directory of your choice. For a user the ~/bin directory can be chosen. Otherwise an alias that references the script would be ok too.

E.g. for getting a ~/bin directory into your path add

PATH="$HOME/bin:$PATH

to your .profile. Alternatively to your .bashrc when using bash or .zshrc for zsh.

Usage

When being in a maven project simply run

m {please enter your goals}

# something like
m clean build

And you’re done.

In case you are in a subdirectory of your project. Simply run

m {please enter your goals}

# something like 
m clean build

And you’re done too.

Of course the directory must be the root maven project or a maven module in a multi module build.

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.