views:

1063

answers:

2

Scenario:

  1. Given
    1. Parent POM which defines a profile and a child (as module)
    2. Child project(s) that will be using the profile by referring to the parent POM.
  2. The intent is to skip profile execution in the parent and execute it in the child only
  3. Profile has activation section <activation><property><name>foo</name></property><activation>
  4. Since parent does not define foo property - the profile is inactive and will not be executed for the parent build
  5. Now, I'm defining <properties><foo>true</foo></properties> in the child with hope that the property will be picked up when child build is executed and profile will be activated. No such luck. Profile is never activated, which tells me that property is never set.
  6. Just to note: mvn package -Dfoo=true activates profile in both parent and child

Am I trying to do the impossible or just doing it wrong?

P.S. Hmmm - even if I define property in the parent, the profile is not triggered. What gives?

+1  A: 

The profile can only be activated by properties passed from the command line. This is because properties in the POM can only be processed once the POM has been parsed, at which point it is too late to resolve the profile activation.

You're in a bit of a catch-22 with this approach unless you are able to pass the property from the command line, specify profile activation in your settings.xml (generally not a great idea), or use the workaround in my previous answer to use the presence of a marker file.

One final alternative if you're on Maven 2.1.0+ is to deactivate the profile via the command line for the parent POM only, this is still obviously not ideal.

You can deactivate a profile with either the character '!' or '-' like this:

mvn install -P !profile-1,!profile-2
Rich Seller
I was afraid so. It actually seems that execution of profile is set and cannot be changed after build is started (even with the marker file). With deactivation what are these -1 and -2 are referring to? If I have parent and child and profile "build" will the following suppose to cancel profile on parent? (In my tests it's not) `mvn install -P !buiild-1`
DroidIn.net
I don't get it exactly. Why don't the `<missing>` or `<exists>` profile triggers work for you?
Pascal Thivent
the example is to deactivate two profiles called `profile-1` and `profile-2`, there is no special significance to the `-1` or `-2`. In your case it would simply be `mvn install -P !build`
Rich Seller
But that would deactivate the whole profile and it will not be execute nether on parent nor on child? Or am I missing something?
DroidIn.net
You would only pass the argument to the build of the parent project so the profile would not be active when the parent is installed/deployed. The profile would however be in the installed pom, so that when a child is built the profile is inherited and active (assuming you don't explicitly deactivate it with the argument)
Rich Seller
I don't know what's wrong with me but I cannot visualize what you describing. I'm passing argument on command line - right? That will start the build and disable profile so it will not be executed in parent. However - when in the next step the child is build, that profile will still be disabled and that's exactly what I experience. Sorry for being so persistent but I want to get to the bottom of this, can you maybe post an example?
DroidIn.net
Sorry I see your problem now, it won't work in a reactor build as the properties are defined at the build level so will be applied for all builds in the reactor. I don't see a way to resolve this unless you make your parent not specify the child as a module and instead use a separate aggregator pom
Rich Seller
Bummer. Does that that apply to "missing file" trick as well?
DroidIn.net
no, the the file is checked within each project, in each project it either exists or it is missing, so you can control the profile activations that way
Rich Seller
I couldn't make it work with file. Same behavior as with property. Pardon a silly question but did you actually run it and verified that it is working? I would love to see that code
DroidIn.net
+1  A: 

To directly answer my own question: in multi-module build all properties are set before build is run so it is impossible to activate/deactivate profile in one of the modules during the build based on setting the propety in the child POM. However if you are looking for way of doing it by using other means please read this comment

DroidIn.net