Search code examples
mavenmaven-3profiles

Confused that order matters when defining conditional profiles in maven


I have a configurable property line.ending that I used during the assembly phase of the building of my project to specify the line ending type of my application property files. For that I have created two profiles LF_DOS and LF_UNIX, so that when I launch :

mvn install 

or

mvn install -P LF_DOS

line.ending equals 'dos', and when I launch :

mvn install -P LF_UNIX

line.ending equals 'unix'.

My first attempt to do this was simply :

    <profile>
        <id>LF_UNIX</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <properties>
            <line.ending>unix</line.ending>
        </properties>
    </profile>
    <profile>
        <id>LF_DOS</id>
        <activation>
            <property>
                <name>!line.ending</name>
            </property>
        </activation>
        <properties>
            <line.ending>dos</line.ending>
        </properties>
    </profile>

Unfortunately, this always gave me line.ending=dos, whatever LF_UNIX is set or not. Weird... But, the more confusing to me, is that I solved the problem just by changing the profile declaration order, like this :

    <profile>
        <id>LF_DOS</id>
        <activation>
            <property>
                <name>!line.ending</name>
            </property>
        </activation>
        <properties>
            <line.ending>dos</line.ending>
        </properties>
    </profile>
    <profile>
        <id>LF_UNIX</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <properties>
            <line.ending>unix</line.ending>
        </properties>
    </profile>

This works exactly like I want.

My questions is : is this a bug ? Or is it something to know about maven profiles, a kind of limitation that makes profiles order declaration particularly matter in such a case ?


Solution

  • The confusion lies in your understanding of how profile activation works.

    You think that this:

    <activation>
      <property>
        <name>!line.ending</name>
      </property>
    </activation>
    

    means if I don't have a maven property named "line.ending" set, activate this profile. What it really means if I didn't specify -Dline.ending=X on the command line, activate this profile. So unless you run something like this:

    mvn clean install -Dline.ending=unix
    

    You are activating this profile and thus having the value set to dos.