Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 778
Hubert Avatar asked Dec 18 '13 13:12

Hubert


People also ask

Why is the use of the profile required in Maven?

A profile in Maven is an alternative set of configuration values which set or override default values. Using a profile, you can customize a build for different environments. Profiles are configured in the pom. xml and are given an identifier.


2 Answers

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>
like image 134
Martin Seeler Avatar answered Nov 15 '22 10:11

Martin Seeler


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.

like image 25
Hubert Avatar answered Nov 15 '22 10:11

Hubert