Home > Java > Maven between different environments

Maven between different environments

As a consultant, I find myself in different environments in need of different configurations. One such configuration is about the Maven settings file. This file is very important, for it governs such things as servers, mirrors and proxies. When you have a laptop, switching from customer configuration to home configuration and vice versa when you change place quickly becomes a bore. When you have to handle more than one customer, it escalates a nightmarish and tangled configuration mess.

In a former environment, colleagues handled Eclipse ini file switch, a very similar concern, by having a dedicated .bat to overwrite the reference file. I heard a colleague of mine do exactly the same for Maven settings file. It does the job, but it is not portable, is more than slightly intrusive and has something I cannot put quite my finger on that does not “fit”.

As IT people are, I’m lazy but idealist, so I scratched my head to handle this problem in a way I would deem more elegant. I think I may have found one, through Maven command native CLI. If you run mvn --help, you’ll get plenty of CLI options: go to the -s section.

 -s,--settings      Alternate path for the user
                               settings file

This means Maven let you use settings files other than ~/.m2/settings.xml. So you can create settings-cust.xml, set all needed configuration for this customer and run mvn -s ~/.m2/settings-cust.xml.

And since I’m really lazy, I just added the following snippet in my ~/.bash_profile to make my life even easier:

alias mvncust='mvn -s ~/.m2/settings-cust.xml'

Now, I just need to run mvncust to run Maven with all relevant configuration for this environment. And it is compatible with other options!

The only drawback I found so far is I’ve to explicitly set the settings file in Eclipse’s m2e but that doesn’t bother me much since I’ve a dedicated Eclipse instance (for configuration and plugins) for each of my environment.

Send to Kindle
Categories: Java Tags:
  1. Alex
    July 7th, 2013 at 16:03 | #1

    What about another approach I find even easier:

    cd /pathToMvn/conf
    git init
    git checkout -b anotherEnvironment

    As result, you have a branch for each environment. Switching between environments is as easy as:
    git checkout myEnvironment.

  2. July 7th, 2013 at 16:07 | #2

    Your approach is quite interesting, and it revealed what I couldn’t put my fingers on in other strategies I witnessed: you have to remember which context is currently active, whereas in my case, I have to pass it each time. In other terms, you’re stateful while I’m stateless.

  3. Marcelo
    July 8th, 2013 at 15:09 | #3

    My approach is a bit different. I have one settings file for each setup, And I simply make a symlink to ~/.m2/settings.xml pointing to the file I want to use when I need to switch environments (ln -s ~/.m2/settings-cust-n.xml ~/.m2/settings.xml). This way, I don’t have to invoke the -s Maven switch each time and also don’t have to fiddle with Eclipse settings, since the expected settings.xml is there.

    Regarding your solution, you could create different Eclipse workspaces, each one having its Maven settings pointing to a custom settings file. I used to use this approach before having the symlnk idea, since I don’t use much the mvn command line.

  1. No trackbacks yet.