Andrew trained as a biologist before transmuting into a software engineer. He's been coding in Java since 1997, and has done a bit of scientific software, a bit of mobile gaming and lots of enterprise software. He's particularly interested in humane and sustainable development. Professionally, that means code maintainability and agile methods. Outside the professional sphere: permaculture, gardening, cooking, green transport and raising children (not by order of importance). He also loves ensemble singing, but that’s on hold for now. Andrew is a DZone MVB and is not an employee of DZone and has posted 9 posts at DZone. You can read more from them at their website. View Full User Profile

Maven Profile Best Practices

11.27.2010
| 58057 views |
  • submit to reddit

Maven profiles, like chainsaws, are a valuable tool, with whose power you can easily get carried away, wielding them upon problems to which they are unsuited. Whilst you're unlikely to sever a leg misusing Maven profiles, I thought it worthwhile to share some suggestions about when and when not to use them. These three best practices are all born from real-world mishaps:

  • The build must pass when no profile has been activated
  • Never use <activeByDefault>
  • Use profiles to manage build-time variables, not run-time variables and not (with rare exceptions) alternative versions of your artifact

I'll expand upon these recommendations in a moment. First, though, let's have a brief round-up of what Maven profiles are and do.

Maven Profiles 101

A Maven profile is a sub-set of POM declarations that you can activate or disactivate according to some condition. When activated, they override the definitions in the corresponding standard tags of the POM. One way to activate a profile is to simply launch Maven with a -P flag followed by the desired profile name(s), but they can also be activated automatically according to a range of contextual conditions: JDK version, OS name and version, presence or absence of a specific file or property. The standard example is when you want certain declarations to take effect automatically under Windows and others under Linux. Almost all the tags that can be placed directly in a POM can also be enclosed within a <profile> tag.

The easiest place to read up further about the basics is the Build Profiles chapter of Sonatype's Maven book. It's freely available, readable, and explains the motivation behind profiles: making the build portable across different environments.

The build must pass when no profile has been activated

(Thanks to Nicolas Frankel for this observation.)

Why?

Good practice is to minimise the effort required to make a successful build. This isn't hard to achieve with Maven, and there's no excuse for a simple mvn clean package not to work. A maintainer coming to the project will not immediately know that profile wibblewibble has to be activated for the build to succeed. Don't make her waste time finding it out.

How to achieve it

It can be achieved simply by providing sensible defaults in the main POM sections, which will be overridden if a profile is activated.

Never use <activeByDefault>

Why not?

This flag activates the profile if no other profile is activated. Consequently, it will fail to activate the profile if any other profile is activated. This seems like a simple rule which would be hard to misunderstand, but in fact it's surprisingly easy to be fooled by its behaviour. When you run a multimodule build, the activeByDefault flag will fail to operate when any profile is activated, even if the profile is not defined in the module where the activeByDefault flag occurs.

(So if you've got a default profile in your persistence module, and a skinny war profile in your web module... when you build the whole project, activating the skinny war profile because you don't want JARs duplicated between WAR and EAR, you'll find your persistence layer is missing something.)

activeByDefault automates profile activation, which is a good thing; activates implicitly, which is less good; and has unexpected behaviour, which is thoroughly bad. By all means activate your profiles automatically, but do it explicitly and automatically, with a clearly defined rule.

How to avoid it

There's another, less documented way to achieve what <activeByDefault> aims to achieve. You can activate a profile in the absence of some property:

<profile id="nofoobar">
<activation>
<property>
<name>!foo.bar</name>
</property>
</activation>
</profile>

This will activate the profile "nofoobar" whenever the property foo.bar is not defined.

Define that same property in some other profile: nofoobar will automatically become active whenever the other is not. This is admittedly more verbose than <activeByDefault>, but it's more powerful and, most importantly, surprise-free.

Use profiles to adapt to build-time context, not run-time context, and not (with rare exceptions) to produce alternative versions of your artifact

Profiles, in a nutshell, allow you to have multiple builds with a single POM. You can use this ability in two ways:

  • Adapt the build to variable circumstances (developer's machine or CI server; with or without integration tests) whilst still producing the same final artifact, or
  • Produce variant artifacts.

We can further divide the second option into: structural variants, where the executable code in the variants is different, and variants which vary only in the value taken by some variable (such as a database connection parameter).

If you need to vary the value of some variable at run-time, profiles are typically not the best way to achieve this. Producing structural variants is a rarer requirement -- it can happen if you need to target multiple platforms, such as JDK 1.4 and JDK 1.5 -- but it, too, is not recommended by the Maven people, and profiles are not the best way of achieving it.

The most common case where profiles seem like a good solution is when you need different database connection parameters for development, test and production environments. It is tempting to meet this requirement by combining profiles with Maven's resource filtering capability to set variables in the deliverable artifact's configuration files (e.g. Spring context). This is a bad idea.

Why?

  • It's indirect: the point at which a variable's value is determined is far upstream from the point at which it takes effect. It makes work for the software's maintainers, who will need to retrace the chain of events in reverse
  • It's error prone: when there are multiple variants of the same artifact floating around, it's easy to generate or use the wrong one by accident.
  • You can only generate one of the variants per build, since the profiles are mutually exclusive. Therefore you will not be able to use the Maven release plugin if you need release versions of each variant (which you typically will).
  • It's against Maven convention, which is to produce a single artifact per project (plus secondary artifacts such as documentation).
  • It slows down feedback: changing the variable's value requires a rebuild. If you configured at run-time you would only need to restart the application (and perhaps not even that). One should always aim for rapid feedback.
  •  

    Profiles are there to help you ensure your project will build in a variety of environments: a Windows developer's machine and a CI server, for instance. They weren't intended to help you build variant artifacts from the same project, nor to inject run-time configuration into your project.

     

    How to achieve it

    If you need to get variable runtime configuration into your project, there are alternatives:

    • Use JNDI for your database connections. Your project only contains the resource name of the datasource, which never changes. You configure the appropriate database parameters in the JNDI resource on the server.
    • Use system properties: Spring, for example, will pick these up when attempting to resolve variables in its configuration.
    • Define a standard mechanism for reading values from a configuration file that resides outside the project. For example, you could specify the path to a properties file in a system property.

    Structural variants are harder to achieve, and I confess I have no first-hand experience with them. I recommend you read this explanation of how to do them and why they're a bad idea, and if you still want to do them, take the option of multiple JAR plugin or assembly plugin executions, rather than profiles. At least that way, you'll be able to use the release plugin to generate all your artifacts in one build, rather than a single one at a time.

    Further reading

    This article is a completely reworked version of a post from my blog.

    Published at DZone with permission of Andrew Spencer, author and DZone MVB.

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

    Comments

    Daniel Johnson replied on Fri, 2011/01/28 - 4:53pm

    Is there anyway to activate a profile based on a user defined variable not being defined? Such as:
    export SETTINGS=/path/to/settings/file mvn
    and in the pom I replaced the normal < with { and > with } because I was having trouble getting the code block working.
    
    {profile id=user-settings}
      {activation}
        {property}
           {name}$SETTINGS{/name}
        {/property}
      {/activation}
      {properties}
        {settings}$SETTINGS{/settings}
      {/properties}
    {/profile}
    {profile id=default-settings}
      {activation}
        {property}
           {name}!$SETTINGS{/name}
        {/property}
      {/activation}
      {properties}
        {settings}/path/to/default/settings/file{/settings}
      {/properties}
    {/profile}
    
    

    Andrew Spencer replied on Fri, 2011/02/11 - 4:02pm in response to: Daniel Johnson

    The Maven doc says (I haven't tried it myself) that you need to put "env.SETTINGS" rather than just "SETTINGS" to access an environment variable.  Alternatively, you could keep your POM as it is, and supply the variable as a java system property: mvn -DSETTINGS=/path/to/file ... (though by convention, Java system properties are usually lower-case).

     I can never get the code formatting right on here either.

    Renzo Kottmann replied on Thu, 2014/03/13 - 3:38am

     timeless, simple great best practice

    Comment viewing options

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