Does using activeByDefault go against maven best practices?
Asked Answered
V

1

12

I wish to find out whether Never use <activeByDefault> is a maven best practice?

The advice here around this seems pretty sound, but do you think it's absolute or if there are some cases in which it would not apply ?

Am posting a snapshot of the content on that page for posterity:

enter image description here

Versatile answered 23/4, 2013 at 10:37 Comment(4)
The docs read: "automatically be active for all builds unless another profile in the same POM is activated". Sounds different than the quote. If one wants "alwaysActive" (which I made up), why make a profile to begin with? If it is for the purpose of being able to disable it, properties would allow for that just fine (as the quote said). Activation through properties is popular among many (those seem to like -Dxxx over -Pxxx; I don't care either way). I've not seen activeByDefault a lot in the wild...Swanky
@SanderVerhagen Would you interpret in the same POM as being in the same effective POM ? If so, in a multi-module build if a profile inherited from a parent is activated would that disable an activeByDefault profile in the child ? Consider this: there is a 'dev' and a 'production' profile, with the former being activated by the absence of a 'prod' property and the latter being activated by the presence of the same. In that case, devs don't have that property and the dev profile is activated by default. So just the presence or absence of property switches between the two profiles.Versatile
See also How to keep Maven profiles which are activeByDefault active even if another profile gets activated?Letterperfect
@SanderVerhagen: because profile cannot remove module from parent project, so defining a default profile is the only way to ensure that one of the 2 modules is included, one default and one non-defaultApodosis
S
0

TL;DR

No, using activeByDefault is NOT against maven best practices.

Details

As described in docs this feature is quite simple:

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.

However in practice you may (and will) have a lot of different conditions for your project defaults:

  • true active by default = always active, unless explicitly deactivated
  • legacy systems = active, unless JDK is above (or below) specific version
  • else-profile = active, when base profile is deactivated
  • environment specific
  • filesystem specific
  • OS specific
  • ...

Thus to use activeByDefault you need to meet three conditions:

  • you need such behaviour, as described in docs
  • simple project structure
  • controlled build environment (dev, CI)

I saw small projects where it was effectively used. I saw huge projects, where this feature was prohibited. And of course I saw projects with this feature misuse, caused by developer's ignorance (rtfm).

Conclusion

This feature could be useful for a small part of Maven community. It's an example of uncommon API, just like java.lang.Long#getLong - useful in rare cases, but ignored by the majority of developers.

PS

Unconditional active by default profile:

<profile>
    <id>active-unless-explicitly-deactivated</id>
    <activation>
         <file><exists>.</exists></file>
    </activation>
    ...
</profile>

and it's deactivation:

./mvnw -P !active-unless-explicitly-deactivated ...
Score answered 19/2, 2023 at 20:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.