Nicolas Frankel is an IT consultant with 10 years experience in Java / JEE environments. He likes his job so much he writes technical articles on his blog and reviews technical books in his spare time. He also tries to find other geeks like him in universities, as a part-time lecturer. Nicolas is a DZone MVB and is not an employee of DZone and has posted 221 posts at DZone. You can read more from them at their website. View Full User Profile

Maven Between Different Environments

07.08.2013
| 4925 views |
  • submit to reddit

 

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.

From http://blog.frankel.ch/maven-between-different-environments 
Published at DZone with permission of Nicolas Frankel, author and DZone MVB. (source)

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

Comments

John J. Franey replied on Mon, 2013/07/08 - 9:18am

Nicholas,

What prevents you from using the 'profile' tag multple times, one for each customer, in a single settings file?


You can set and export a shell environment variable to specify the profile.  For example, source this file into your current shell environment:


export CUSTNAME='Acme, Inc'
<settings>
  <profiles>
    <profile>
      <id>Acme, Inc</id>
      <activation>
        <property>
          <name>env.CUSTNAME</name>
          <value>Acme, Inc</value>
        </property>
      </activation>
      ......
    </profile>
  </profiles>
</settings>

The benefits are: All settings are in a single file.  There are no copies of shared configuration when using a single file.  Continued use of mvn command, not an alias.

And in eclipse:: project -> properties -> maven -> profiles, set profile to 'Acme, Inc'.

The benefit of this approach in eclipse: the maven settings file is defined once, and never changed; the active profile is set at the project level.


Nicolas Frankel replied on Mon, 2013/07/08 - 9:35am in response to: John J. Franey

Perhaps because profiles do not seem to meet my requirements regarding proxies and servers ?

Btw, my first name is spelled Nicolas, without an 'h'

John J. Franey replied on Mon, 2013/07/08 - 10:43am in response to: Nicolas Frankel

Nicolas,

I apologize for the misspelling of your name.  This was not an intentional slight.

For proxies, these are by default active.   You can instead explicitly set them to inactive, and within a specific customer profile, override to active.

For servers, this is of course more problematic because the server is a project level concern, not an environment concern.   'server' is needed when you run 'mvn deploy' to upload the project artifacts. The login parameters of a server is in the user's settings.xml to keep them private.  However, the name of the server to use is rightly controlled by the project, not the user's environment.  I think the assumption is that no matter which environment, the project will upload artifacts to the same server.  Nevertheless, it is possible to parameterize the server name in the project's pom, and to provide an environment specific value in a customer specific profile.

Comment viewing options

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