48

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.

3
  • 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
    Commented Nov 14, 2012 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. Commented Nov 14, 2012 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
    Commented Nov 14, 2012 at 16:16

3 Answers 3

69

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>
7
  • What version of maven are you using? That should not make any difference.
    – maba
    Commented Nov 14, 2012 at 17:00
  • 2
    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
    Commented Apr 5, 2016 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 <project> root and declare them in the <profiles> (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
    Commented Dec 6, 2017 at 23:45
  • what if there is a common module that should be built before all the modules?
    – Vishrant
    Commented Jan 25, 2019 at 20:50
  • 10
    It is worth to mention to remove any <modules> declaration in the pom.xml and only use the ones inside the <profiles>.
    – sahlouls
    Commented Feb 22, 2019 at 8:20
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.

2
  • 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. Commented Nov 14, 2012 at 15:10
  • IOW, I want a way to achieve this only modifying the pom.xml. Commented Nov 14, 2012 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

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.