7

I have two profiles defined in my pom.xml:

<profiles>
    <profile>
        <id>nobrand</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <brand>nobrand</brand>
        </properties>
    </profile>
    <profile>
        <id>mybrand</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <brand>mybrand</brand>
        </properties>
    </profile>
</profiles>

The <build> section simply uses the property that is set by each profile.

Both profiles are set as active by default. However, only the last one is executed by mvn package. Why? I expected the build to be executed twice.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Misterer
  • 73
  • 1
  • 4
  • See also [Does using activeByDefault go against maven best practices?](http://stackoverflow.com/questions/16167206/does-using-activebydefault-go-against-maven-best-practices) – Vadzim Jan 23 '17 at 13:06
  • See also [How to keep Maven profiles which are activeByDefault active even if another profile gets activated?](http://stackoverflow.com/questions/5309379/how-to-keep-maven-profiles-which-are-activebydefault-active-even-if-another-prof) – Vadzim Jan 23 '17 at 14:51

1 Answers1

7

Profiles that are active by default are automatically deactivated when another profile is activated. Since both of your profiles are activated by default, the second one turns active and deactivates the first one.

Quoting from the Maven docs about the activeByDefault property (emphasis mine):

This profile will automatically be active for all builds unless another profile in the same POM is activated using one of the previously described methods. All profiles that are active by default are automatically deactivated when a profile in the POM is activated on the command line or through its activation config.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • Is there a way to execute both profiles without specifying additional maven parameters through command line? – Misterer Sep 17 '15 at 14:22
  • @Misterer Why do you have two profiles to begin with? When should one be activated and not the other one? – Tunaki Sep 17 '15 at 14:41
  • What I want to have is parameterized builds which would use different resources based on those parameters. For example, this could be used to build customized/branded applications. The possible values of the parameters are static, so it would be nice if I could run `mvn package` and have all possible builds done at once rather than having to specify each one via command line. The profiles above only work correctly if a single profile is specified which is inconvenient (I'd have to write a script to build all of them at once). Maybe I should use something else instead of profiles here. – Misterer Sep 17 '15 at 14:57
  • @Misterer Yes, profiles are not going to cut it. You could trick Maven to execute a plugin twice and changing the properties inside each plugin execution. See this question: http://stackoverflow.com/q/7239786/1743880 – Tunaki Sep 17 '15 at 15:02