maven profile activation with default profile without tag "activeByDefault"

D.Razvan picture D.Razvan · Oct 17, 2016 · Viewed 7.6k times · Source

I am looking for a way to setup multiple profiles, one of which I want to be set as default (running just mvn clean install should choose that profile). I am not interested in any solution that takes use of activeByDefault since it impedes me from creating separate profiles in another module and using them at the same time with the default profile.

Currently I am using the property approach that looks something like:

<profiles>
    <profile>
        <id>prof1</id>
        <activation>
            <property>
                <name>!myprop</name>
            </property>
        </activation>
    </profile>
    <profile>
        <id>prof2</id>
        <activation>
            <property>
                <name>myprop</name>
                <value>prof2</value>
            </property>
        </activation>
    </profile>
    <profile>
        <id>prof3</id>
            <activation>
            <property>
                <name>myprop</name>
                <value>prof3</value>
            </property>
        </activation>
        </profile>
</profiles>

This means that I have to compile my project with mvn clean install -Dmyprop=prof3 if i want to select prof3 instead of prof1(which is default by not setting any property).

However I want to achieve the same effect but compile my project with -Pprofilename instead of setting a property. I have tried adding the property in each profile under <properties> tag but it seems the default is always ran. I read that pom properties are not available to use for activating profiles.

Is there any workaround to this ? such as having a settings or properties file (don't want to change it everytime I compile) from which I can get the property and achieve my goal of compiling using -PprofileName ?

Answer

Jens picture Jens · Oct 17, 2016

You can use activeByDefault for the Profile which should running if you do not activate an other:

        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>

For more Information see http://maven.apache.org/guides/introduction/introduction-to-profiles.html