project specific maven settings.xml

In maven, it is not recommended to put repositories in the pom itself. However many projects have specific requirements about replacing central with a company specific repository or adding extra repositories with private dependencies.

One possible solution is to add a profile in your local settings.xml file (in ~/.m2), though it is cumbersome to have to include a -PprojectProfile parameter in every mvn command.
Another alternative having a specific settings.xml for your profile. This requires using the –settings command line parameter which is equally cumbersome.

As an alternative to make things easier, I patched the mvn startup script to detect a project specific .settings.xml file. You can put this in the parent directory or yuor project or in the directory above (allowing grouping of projects which need the same maven settings).

This was achieved using two small changes. At the end of the mvn script (the last command), I added an extra variable, $LOCAL_SETTINGS:

exec "$JAVACMD" \
  $MAVEN_OPTS \
  -classpath "${M2_HOME}"/boot/plexus-classworlds-*.jar \
  "-Dclassworlds.conf=${M2_HOME}/bin/m2.conf" \
  "-Dmaven.home=${M2_HOME}"  \
  ${CLASSWORLDS_LAUNCHER} $LOCAL_SETTINGS "$@"

This is calculated earlier in the script (I put it just before the SKIP_MAVEN_RC stuff, but anywhere will do):

LOCAL_SETTINGS=""
SETTINGS_DIR=`pwd`
SETTINGS_FILE=".settings.xml"
while [ "" == "$LOCAL_SETTINGS" -a "/" != "$SETTINGS_DIR" ] ; do
    if [ -r "$SETTINGS_DIR/$SETTINGS_FILE" ] ; then
        LOCAL_SETTINGS="--settings $SETTINGS_DIR/$SETTINGS_FILE" 
    fi
    if [ ! -r "$SETTINGS_DIR/pom.xml" ] ; then
        break
    fi
    SETTINGS_DIR="$(dirname "$SETTINGS_DIR")"
done

This looks in the current directory for a .settings.xml file. When not found, it moves up in the directory tree as long as there is a pom.xml file. It then checks one more parent directory.

This uses some bash specific constructs, so you have to change the first line of the script to “#!/bin/bash”.

Done.

Leave a Reply

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

question razz sad evil exclaim smile redface biggrin surprised eek confused cool lol mad twisted rolleyes wink idea arrow neutral cry mrgreen

*