Fabrizio Giudici is a Senior Java Architect with a long Java experience in the industrial field. He runs Tidalwave, his own consultancy company, and has contributed to Java success stories in a number of fields, including Formula One. Fabrizio often appears as a speaker at international Java conferences such as JavaOne and Devoxx and is member of JUG Milano and the NetBeans Dream Team. Fabrizio is a DZone MVB and is not an employee of DZone and has posted 67 posts at DZone. You can read more from them at their website. View Full User Profile

Automating Maven switch to alternate settings

06.11.2012
| 5030 views |
  • submit to reddit

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.

Published at DZone with permission of Fabrizio Giudici, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Martin Vaněk replied on Tue, 2012/06/12 - 4:43am

Hmmm, that's smart.

Fabrizio Giudici replied on Sun, 2012/06/17 - 6:07am

After some testing, I've found a problem with NetBeans 7.2: while compiling (and launching other Maven goals) is pretty fine, there can be problems with the editor completion and such. In fact, it seems that the IDE, even though it uses my patched script for running Maven, still uses the default settings.xml file. If some super POMs used by a project are only available in the alternate repository pointed by the alternate settings.xml, the editor can't find them. Going to check how to fix this.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.