Can a parent property be made final so that it cannot be overridden by a child

22,088

Solution 1

No, you can't. The idea is that if it shouldn't be possible to override a value, don't use a property. If you have no other option, you might want to force it with http://maven.apache.org/enforcer/enforcer-rules/requireProperty.html which will break the build if a property has a different value than expected.

Solution 2

Only way I know to do this for sure is to define the property on the command line, e.g. mvn -DsomeProperty=strongValue <mvn goals/phases here>.

You might experiment with profiles defined in the parent; I suspect that won't help.

Share:
22,088

Related videos on Youtube

zacheusz
Author by

zacheusz

I'm a programmer. Programming is a way of thinking, but I translated this thinking into various languages: Basic, Turbo Pascal, C, C++, Java, Python...

Updated on January 03, 2022

Comments

  • zacheusz
    zacheusz over 2 years

    Is it possible to make properties in parent pom not overridable by the module pom?

    For example:

    if module pom says:

    <properties>
        <someProperty>some value to be replaced</properties>
    </properties>
    

    and parent pom already has it declared as:

    <properties>
        <someProperty>strongValue</someProperty>
    </properties>
    

    effective module pom should be:

    <properties>
        <someProperty>strongValue</someProperty>
    </properties>
    

    but it is currently expected to be this:

    <properties>
        <someProperty>some value to be replaced</properties>
    </properties>
    

    If yes then how to achieve it?

    • rogerdpack
      rogerdpack over 2 years
      I'm confused what you're asking. I assume you have a "parent" project with a "module" within it, and you're trying to get the value from the parent even after it's been overridden in the "module" (child)?
  • zacheusz
    zacheusz over 10 years
    thx, but I need to override it from the parent POM, not from the command line (as question stated)
  • user944849
    user944849 over 10 years
    I think you are out of luck then.
  • Jon Onstott
    Jon Onstott about 10 years
    Defining properties in a profile in the parent and enabling the profile doesn't seem to activate the properties in the child project.
  • user944849
    user944849 about 10 years
    @Jon, in my experience you need to include the same profile, with the same ID and activation criteria, in the child module in order for the values defined in the parent profile to take effect when the profile is enabled. If you Google "maven profile inheritance" you'll find links to forum posts and/or JIRA issues with detailed explanations. http://jira.codehaus.org/browse/MNG-5127 is one example.
  • Jesse Glick
    Jesse Glick about 8 years
    Unfortunately this is not helpful. I would like the parent POM to define some property defaults plus a series of profiles with alternate property values, where the child POM can inherit or override the default, or you can select a profile using -P to override the override as it were. This works when the profile is defined in ~/.m2/settings.xml but not when it is defined in the parent POM. So there is no good way to share these profiles in SCM.
  • user944849
    user944849 about 8 years
    @JesseGlick, sorry, I'm just the messenger. I spent many hours trying to get the overrides to work before realizing it's not always possible. As I mentioned in an earlier comment, you might be able to make the overrides work if the child profile uses the exact same ID (and activation criteria if any). Anything else is a feature request to the Maven team. They may already have JIRAs out there for this, perhaps find and upvote them.
  • Jesse Glick
    Jesse Glick about 8 years
    I can find some MNG issues related to profile activation but I did not find anything related to overriding properties (without needing to duplicate information in the child POM).
  • Lawrence
    Lawrence over 7 years
    What do you mean, you can't? We do this all the time and it works. Of course you can override parent pom properties. Just checked my parent pom for a project where I did just that because you made me doubt. And it does work. My effective pom shows the overridden value.
  • Robert Scholte
    Robert Scholte over 7 years
    I think you misunderstood the question. Try to read it as: can a property be final (as in Java) so it cannot be overridden by a child? To this question the answer is "no".
  • zacheusz
    zacheusz about 5 years
    yeah, this is obvious - what if you can't change the child pom?
  • Simon Logic
    Simon Logic almost 3 years
    title should be changed, right now it is confusing
  • haridsv
    haridsv over 2 years
    I edited the title and body based on the above clarification from @RobertScholte.