Hubert
Hubert

Reputation: 1175

maven -> profile -> activation - all conditions are required or just one?

Configuration:
- Maven: 3.0.5
- Java: 1.6.0_45

Description:

Let's say we have profile configuration like below:

<profiles>
    <profile>
        <id>profile-1</id>
        <activation>
            <jdk>1.6</jdk>
            <property>
                <name>name</name>
                <value>Hubert</value>
            </property>
        </activation>
    </profile>
    <profile>
        <id>profile-2</id>
        <activation>
            <jdk>1.6</jdk>
            <property>
                <name>name</name>
                <value>Wiktoria</value>
            </property>
        </activation>
    </profile>
</profiles>

We have two profiles: profile-1 and profile-2.

Profile profile-1 should be active when two requirements are met:
- jdk is version 1.6
- property name has value Hubert

Question:

Let's check this configuration:

mvn -Dname=Hubert help:active-profiles

As a result I get that there are two active profiles: profile-1 and profile-2.
Hmm...
Profile profile-2 should not be active since property name has value different from expected Wiktoria.

Could someone explain me why this work like this? Is it a normal behavior?
Thanks.

Upvotes: 7

Views: 2283

Answers (2)

Hubert
Hubert

Reputation: 1175

NOTE: This is only a suplement to Chasmo's correct answer.

There is a book from sonatype describing Maven. In section Sonatype book (section 5.3.1) we can find:

A profile is activated when all activation criteria has been satisfied.

This is not true. Truth is that one condition is enought to activate profile, which is of course equal to OR logical condition. This behavior is described in Maven docs:

Activation occurs when one or more of the specified criteria have been met. When the first positive result is encountered, processing stops and the profile is marked as active.

This is for me neither intiutive nor very useful. But thats how maven works at the time of writing that.

There is a ticket MNG-4565 for AND conjunction. This is marked as Bug, but acording to Maven doc it is not, so this ticket has been opened for almost 4 years. The most useful part is last comment to this ticket written by Ronny Pscheidl. His comment points to this source: and-activation-profile-selector. This changes default maven OR condition to AND condition. Tested. Works. But of course if you decide to use this, you have one more thing to remember of.

Upvotes: 2

Martin Seeler
Martin Seeler

Reputation: 6982

The problem here is that the activation list with your trigger conditions is connected with OR. They do have a ticket to provide multiple activation triggers, but it's still open. That means, that it matches your sdk rule which is true and therefore active.

<profile>
    <id>profile-1</id>
    <activation> <!-- true || true = true -->
        <jdk>1.6</jdk> <!-- true -->
        <property> <!-- true -->
            <name>name</name>
            <value>Hubert</value>
        </property>
    </activation>
</profile>
<profile>
    <id>profile-2</id>
    <activation> <!-- true || false = true -->
        <jdk>1.6</jdk> <!-- true -->
        <property> <!-- false -->
            <name>name</name>
            <value>Wiktoria</value>
        </property>
    </activation>
</profile>

Upvotes: 6

Related Questions