Technical Leader

Maven Default Profiles

I have been using Maven for a while now and as such I have ran into my fair share of using profiles with Maven. As there are many other articles/posts about Maven profiles I will not be providing an overview here. I would like to delve into a particular area of interest when dealing with profiles and the activation aspect.

One of the many ways a profile can be activated is by ‘activeByDefault’, I currently use this both on our CI box and Windows (typically developers) box. These basically provide system properties for a database connection information and paths to property files. There has not been a time where the activeByDefault profiles has caused us any problems at all. So I was a little apprehensive when I saw James Lorenzen’s blog entry on the problems with using these types of profiles.

James professes (by way of providing a link to documentation) when a profile is activated by any other way then the so-called default profile is turned off. So I created a simple example pom in an attempt to replicate this and in doing so I ran across the same problem as he did. For the following, I use this pom as an example.

gorshing@touch ~
$ mvn clean
[INFO] Scanning for projects...
[INFO] ----------------------------------------------------------------------
[INFO] Building Unnamed - com.example:ProfileProject:pom:0.1-SNAPSHOT
[INFO]    task-segment: [clean]
[INFO] ----------------------------------------------------------------------
[INFO] [clean:clean]
[INFO] [antrun:run {execution: Testing Active by Default Profiles}]
[INFO] Executing tasks
     [echo] default profile val 54
     [echo] yep 42
[INFO] Executed tasks
[INFO] ----------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ----------------------------------------------------------------------
[INFO] Total time: 5 seconds
[INFO] Finished at: Wed Dec 31 12:29:32 CST 2008
[INFO] Final Memory: 3M/7M
[INFO] ----------------------------------------------------------------------

gorshing@touch ~
$ mvn clean -Pqual.val
[INFO] Scanning for projects...
[INFO] ----------------------------------------------------------------------
[INFO] Building Core000Build
[INFO]    task-segment: [clean]
[INFO] ----------------------------------------------------------------------
[INFO] [clean:clean]
[INFO] [antrun:run {execution: Testing Active by Default Profiles}]
[INFO] Executing tasks
     [echo] default profile val ${default.profile.val}
     [echo] yep 69
[INFO] Executed tasks
[INFO] ----------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ----------------------------------------------------------------------
[INFO] Total time: 4 seconds
[INFO] Finished at: Wed Dec 31 12:03:01 CST 2008
[INFO] Final Memory: 3M/7M
[INFO] ----------------------------------------------------------------------

As you can easily tell, this is exactly as the documentation specifies. The key difference between my experience and his is solely based on where the profiles are located. Given the setup I am using at work, the ‘activeByDefault’ profile isn’t in the pom itself, but is in ${M2_HOME}/conf/settings.xml.

I have added the following to my ${M2_HOME}/conf/settings.xml configuration, and using this pom (same as above only without the activeByDefault profile entry).

    <profile>
      <id>default.val</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <default.profile.val>54</default.profile.val>
      </properties>
    </profile>

As you can tell in the following listing, the activeByDefault truly does what one might expect.

gorshing@touch ~
$ mvn clean
[INFO] Scanning for projects...
[INFO] ----------------------------------------------------------------------
[INFO] Building Unnamed - com.example:ProfileProject:pom:0.1-SNAPSHOT
[INFO]    task-segment: [clean]
[INFO] ----------------------------------------------------------------------
[INFO] [clean:clean]
[INFO] [antrun:run {execution: Testing Active by Default Profiles}]
[INFO] Executing tasks
     [echo] default profile val 54
     [echo] some val 42
[INFO] Executed tasks
[INFO] ----------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ----------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Fri Jan 02 18:42:53 CST 2009
[INFO] Final Memory: 3M/7M
[INFO] ----------------------------------------------------------------------

gorshing@touch ~
$ mvn clean -Pqual.val
[INFO] Scanning for projects...
[INFO] ----------------------------------------------------------------------
[INFO] Building Unnamed - com.example:ProfileProject:pom:0.1-SNAPSHOT
[INFO]    task-segment: [clean]
[INFO] ----------------------------------------------------------------------
[INFO] [clean:clean]
[INFO] [antrun:run {execution: Testing Active by Default Profiles}]
[INFO] Executing tasks
     [echo] default profile val 54
     [echo] some val 69
[INFO] Executed tasks
[INFO] ----------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ----------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Fri Jan 02 18:43:11 CST 2009
[INFO] Final Memory: 3M/6M
[INFO] ----------------------------------------------------------------------

I am curious as to if this is expected results, or if this is a bug which might be resolved …. if that happens then it will most defiantly break some of my builds.

Something to look into and chew on.

Update: I asked on #maven @ irc.codehaus.org and received the following from wsmoak (these are of course her opinion):

activeByDefault was originally intended to be active only if no other profiles were explicitly activated ... a cheap way to activate exactly one of a set of profiles ... however I think it's been "improved" since then. not sure exactly when/how ... I generally avoid using it and don't recommend it