46

I have the following XML in my maven POM.xml:

<profiles>
  <profile>
     <id>default</id>
     <activation>
        <activeByDefault>true</activeByDefault>
        <property>
           <name>default</name>
           <value>!disabled</value>
        </property>
     </activation>
     <modules>
        <module>m1</module>
        <module>m2</module>
        <module>m3</module>
     </modules>
  </profile>
  <profile>
     <id>x</id>
     <modules>
        <module>m1</module>
     </modules>
  </profile>
</profiles>

What I'm trying to achieve is this:

  1. When I run mvn install, I want it to build m1, m2 and m3 projects.

  2. When I run mvn install -Px, I want it to only build m1.

My current problem is that with the code above, option 2 builds all m1, m2 and m3.

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
Galder Zamarreño
  • 5,027
  • 2
  • 26
  • 34
  • Why do you have the activation properties set for the default profile? You say that you want it to run by default but also if that property does not have the value "disabled". Remove the whole property part from first profile. – maba Nov 14 '12 at 15:33
  • I've done that but does not help. The problem now is that no modules are built when 'mvn install' is run. – Galder Zamarreño Nov 14 '12 at 16:09
  • 2
    I tested the very same profiles without the `property` part and it works just as you want. Try with these commands: `mvn help:active-profiles` and `mvn -Px help:active-profiles`. – maba Nov 14 '12 at 16:16

3 Answers3

65

Found the solution guys, define 'x' profile first and the 'default' and it works fine (insane Maven!!). Here's the final result:

   <profiles>
      <!-- DO NOT CHANGE THE *ORDER* IN WHICH THESE PROFILES ARE DEFINED! -->
      <profile>
         <id>x</id>
         <modules>
            <module>m1</module>
         </modules>
      </profile>
      <profile>
         <id>default</id>
         <activation>
            <activeByDefault>true</activeByDefault>
         </activation>
         <modules>
            <module>m1</module>
            <module>m2</module>
            <module>m3</module>
         </modules>
      </profile>
   </profiles>
Galder Zamarreño
  • 5,027
  • 2
  • 26
  • 34
  • What version of maven are you using? That should not make any difference. – maba Nov 14 '12 at 17:00
  • 1
    Ordering does not make sense here.. i was looking for something similar.. I can call any profile and when i want to build all modules i just do not give the profile ,so modules n sequence are executed – Sohan Apr 05 '16 at 10:09
  • 4
    I agree. I was having the same issue but the resolution turned out to be that when I use profiles, I have to explicitly remove my modules from the root and declare them in the (thus creating a default profile). I tried switching the order to validate this answer and it did not change anything here. @Galder Zamarreño what maven version were you using? I want to try and reproduce to ensure it's not a maven bug. Mine was 3.5.0. – cleberz Dec 06 '17 at 23:45
  • what if there is a common module that should be built before all the modules? – Vishrant Jan 25 '19 at 20:50
  • 9
    It is worth to mention to remove any `` declaration in the pom.xml and only use the ones inside the ``. – sahlouls Feb 22 '19 at 08:20
  • @sahlouls you are my hero. it was worth mentioning – Andrea deCandia Aug 26 '19 at 10:16
  • If you remove `` from the root of pom.xml, then you should include it in the profile, which has all the modules, in the `mvn install` command. Like, before: `mvn clean install -Pneeded-profile1,needed-profile2`, after: `mvn clean install -Pprofile-where-modules-declared,needed-profile1,needed-profile2` – Dmytro Moskovchenko Nov 04 '22 at 14:07
6

You can disable maven profiles that have runByDefault set to true from the command line like so:

mvn install -P !default

Note, this requires Maven version 2.0.10.

EMMERICH
  • 3,424
  • 2
  • 18
  • 14
  • That'd be great but what happens is that I cannot change the actual maven commands. For IDEs, to figure out which projects to import into the workspace, they use something like 'mvn install', and the 2nd command is what one cloud provider runs for me, I cannot change that either. – Galder Zamarreño Nov 14 '12 at 15:10
  • IOW, I want a way to achieve this only modifying the pom.xml. – Galder Zamarreño Nov 14 '12 at 15:11
-15

Just add a space after -P the sintax of the command is

mvn install -P x

And not like you are using

mvn install -Px

Take a look at Maven - Introduction to profiles