/ MAVEN

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 <arg>     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.

Nicolas Fränkel

Nicolas Fränkel

Developer Advocate with 15+ years experience consulting for many different customers, in a wide range of contexts (such as telecoms, banking, insurances, large retail and public sector). Usually working on Java/Java EE and Spring technologies, but with focused interests like Rich Internet Applications, Testing, CI/CD and DevOps. Also double as a trainer and triples as a book author.

Read More
Maven between different environments
Share this