Over a million developers have joined DZone.

Automating Maven switch to alternate settings

DZone's Guide to

Automating Maven switch to alternate settings

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

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:



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


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.

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat


Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}