swisstech.net

tech and photography

Automatically switch your mvn settings

2012-10-18

I have one primary development notebook and take that with me wherever I go. Now in a company setting you usually have proxies and whatnot. And I think it’s an official best practice to roll your own company or departement maven proxy/cache.

So dependent on where you are, you might need a different maven ~/.m2/settings.xml file. Here’s a very simple shell function you can add to your ~/.bashrc

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
function mvn {
        MVN="$(which mvn)"
        if [ -n "$(ifconfig eth0 | grep YOUR-WORK-IP-HERE)" ]; then
                echo ">>> Running mvn whith work config"
                ${MVN} -gs ${HOME}/.m2/settings-work.xml $*
        else
                echo ">>> Running mvn with vanilla config"
                ${MVN} $*
        fi
}

This just checks for the ip of eth0 and calls mvn with a special settings.xml. In all other cases mvn is run with the vanilla config (or none, since the settings.xml is optional).

Of course you can extend on this to check for something else, basically anything is possible.

Simple yet effective.