Automating Maven switch to alternate settings
Join the DZone community and get the full member experience.Join For Free
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:
- I have my own Maven repository mirror (available on the internet), my customers have their own (available in private intranets);
- 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.
Opinions expressed by DZone contributors are their own.