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

3

27

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?

Quinlan answered 5/9, 2013 at 16:28 Comment(1)
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)?Unshackle
I
12

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.

Imperturbable answered 8/9, 2013 at 17:17 Comment(4)
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.Amylene
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".Imperturbable
title should be changed, right now it is confusingAdenocarcinoma
I edited the title and body based on the above clarification from @RobertScholte.Heliostat
E
10

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.

Edlin answered 5/9, 2013 at 16:52 Comment(7)
thx, but I need to override it from the parent POM, not from the command line (as question stated)Quinlan
I think you are out of luck then.Edlin
Defining properties in a profile in the parent and enabling the profile doesn't seem to activate the properties in the child project.Palpate
@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.Edlin
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.Playreader
@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.Edlin
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).Playreader
V
-2

A child POM can overwrite the value of a property defined in a parent pom. So it works by just putting a section in the child POM and set the values to desired values.

Veriee answered 17/1, 2019 at 18:46 Comment(1)
yeah, this is obvious - what if you can't change the child pom?Quinlan

© 2022 - 2024 — McMap. All rights reserved.