Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Automating Maven switch to alternate settings

DZone's Guide to

Automating Maven switch to alternate settings

· Java Zone
Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

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.

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

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

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}