Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activate Maven profile if another profile is not activated

Tags:

The question is related to Maven: Only activate profile A if profile B is not activated?, but it's more specific.

If I type one of the following:

mvn clean install -PspecificProfile mvn clean install -Dsmth -PspecificProfile mvn clean install -Dsmth -PspecificProfile,anotherProfile 

then I want to activate the specificProfile profile. (+the additional specified profiles)

If I type anything else like:

mvn install mvn clean install mvn clean install -Dsmth mvn clean install -Dsmth -PanotherProfile mvn clean install -Dsmth -PdefaultProfile mvn clean install -Dsmth -PdefaultProfile,anotherProfile 

then I want to activate the defaultProfile(+the additional specified profiles).

Idea:

if ( specific profile P is used via command line ) {     activate P; } else {     activate the default profile; } activate other specified profiles; 

Examples:

mvn ...                          // default mvn ... -PspecificProfile        // specificProfile           (no default!) mvn ... -Px                      // default + x mvn ... -Px,y                    // default + x + y mvn ... -Px,specificProfile      // x + specificProfile       (no default!) mvn ... -Px,specificProfile,y    // x + specificProfile + y   (no default!) 

I tried to do something like this (in pom.xml):

<profile>     <id>defaultProfile</id>     <activation>         <property>!x</property>     </activation>     ... </profile> <profile>     <id>specificProfile</id>     <properties>         <x>true</x>     </properties>     ... </profile> 

but it doesn't work.

like image 472
ROMANIA_engineer Avatar asked Sep 04 '14 13:09

ROMANIA_engineer


People also ask

Why profile is used 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.


1 Answers

Profile x will be the the only active profile when you call mvn ... -P x. Reason from the maven documentation:

  Profiles can be explicitly specified using the -P CLI option.   This option takes an argument that is a comma-delimited list of profile-ids to use. When this option is specified, no profiles other than those specified in the option argument will be activated. 

Here's a workaround:

<profiles>     <profile>         <id>default</id>         <activation>             <activeByDefault>true</activeByDefault>             <property>                 <name>!specific</name>             </property>         </activation>     </profile>     <profile>         <id>specific</id>         <activation>             <property>                 <name>specific</name>             </property>         </activation>     </profile>     <profile>         <id>x</id>         <activation>             <property>                 <name>x</name>             </property>         </activation>     </profile>     <profile>         <id>y</id>         <activation>             <property>                 <name>y</name>             </property>         </activation>     </profile> </profiles> 

The commands:

mvn ...                        // default mvn ... -Dspecific             // specific Profile         (no default!) mvn ... -Dx                    // default + x mvn ... -Dx -Dy                // default + x + y mvn ... -Dx -Dspecific         // x + specific Profile     (no default!) mvn ... -Dx -Dspecific -Dy     // x + specific Profile + y (no default!) 

Execute mvn ... help:active-profiles to get the list of the ids of active profiles.

like image 180
user2432405 Avatar answered Oct 13 '22 08:10

user2432405