2

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 ?

Yanflea
  • 3,876
  • 1
  • 14
  • 14

1 Answers1

1

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.

Michael
  • 6,141
  • 2
  • 20
  • 21
  • Thanks ! I finally ended up to the same explanation thanks to that documentation : http://docs.codehaus.org/display/MAVENUSER/Profiles, where it is effectively precised that activation on property/absence of property works only with the command line (as far as I know, this is not mentionned in the Developper Ref Guide from Sonatype)... Once you understand that it concerns the command line, everything becomes clear. Was about to post it but you did it first :). – Yanflea Feb 24 '12 at 06:02
  • Yep, I had the same issue before. I was trying to set a property from settings.xml and use that to trigger different profiles. It worked, but only due to the fact that I happen to name the profiles the same in settings.xml and in profiles.xml (maven 2.2.1) so it activated both (be careful of that btw, if you name a profile, it activates them all over, including if you did activation by default true on one, anything w/ that name is now active). I finally figured out that the activation is only for -D properties. Then it all made perfect sense. – Michael Feb 24 '12 at 13:16