Over a million developers have joined DZone.

Automating Maven switch to alternate settings

· Java Zone

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

I extensively use Maven for my own projects and for some of my customers' ones, that I have on my laptop. Now there are two problems:

  1. I have my own Maven repository mirror (available on the internet), my customers have their own (available in private intranets);
  2. My projects are all open source, thus they require no privacy; my customers' ones aren't, so they are stored into an encrypted partition. But, note: what about their artifacts? They get installed into the same $HOME/.m2/repository folder as open source stuff. I don't want to move the whole Maven repository to the encrypted partition as I would run into a performance hit even when I don't need encryption.

The solution is easy: use different settings.xml files, with different proxies and paths for the local repositories. But manually switching them is boring and error-prone. Thus, I've prepared a very simple wrapper to the Maven shell script:

 

#!/bin/sh

case "$PWD" in
    */Business/Customers/ACME*) SETTINGS='acme-settings.xml' ;;
    */Business/Customers*)      SETTINGS='customers-settings.xml' ;;
    *)                          SETTINGS='settings.xml' ;;
    esac

echo SETTINGS: $SETTINGS

eval /Users/fritz/Applications/apache-maven-3.0.4/bin/mvn.original -s $HOME/.m2/$SETTINGS $*

 

As you can see, one out of three different settings files is picked in function of the current directory: one very specific to a customer (e.g. it sets the corporate Maven repository), one for all the customers (it only sets the local repository into the encrypted folder) and one for my own stuff.

Too bad that it's not possible to spread artifacts to different local repositories in function of the groupId: with my solution you end up with a lot of duplicates (common dependencies of all projects are duplicated into the plaintext and the enctypted repo). But it solves the primary problem for me. 

It also works with the NetBeans IDE: you just need to configure an external Maven installation instead of the embedded one.

PS If you're thinking that a more elegant solution would use profiles, well, unfortunately <localRepository> cannot be specified inside a profile.

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}