D.Razvan
D.Razvan

Reputation: 435

maven profile activation with default profile without tag "activeByDefault"

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 ?

Upvotes: 3

Views: 9953

Answers (3)

JimHawkins
JimHawkins

Reputation: 4998

Project specific command line settings

I'm not complete sure if it helps to solve your issue, but since Maven 3.3.1, an additional way exits to configure projects.

In your project base directory create a directory named .mvn and put a file named maven.config in it with this content:
-Pprof1

If you execute mvn clean install , profile prof1 will be used. If you enter mvn clean install -Pprof3 then the build performs with profile prof3.

See also:
Maven 3.3.1 release notes
and
Maven 3.3.9 release notes

Upvotes: 0

Phil
Phil

Reputation: 1256

The documentation linked by @Jens explains the 5 ways to trigger a profile. I don't believe any combination will give you the "other or default" triggering that you want except for activeByDefault.

Without seeing the other poms that explain why you can't use activeByDefault it's going to be hard to help find an entirely maven based solution. Perhaps simplify your build profile structure?

An alternative is to create an alias for mvn called dmvn for default maven that does mvn -Pprof1, then use mvn whenever you want prof2 or 3.

Upvotes: 1

Jens
Jens

Reputation: 69450

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

Upvotes: 5

Related Questions