Only one Maven profile executed when using activeByDefault
Asked Answered
T

1

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.

Tarpan answered 17/9, 2015 at 9:50 Comment(2)
I
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.

Intrust answered 17/9, 2015 at 11:42 Comment(4)
Is there a way to execute both profiles without specifying additional maven parameters through command line?Tarpan
@Tarpan Why do you have two profiles to begin with? When should one be activated and not the other one?Intrust
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.Tarpan
@Tarpan 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: https://mcmap.net/q/450365/-how-to-invoke-the-same-maven-build-twice-in-one-call/1743880Intrust

© 2022 - 2024 — McMap. All rights reserved.