Can a parent property be made final so that it cannot be overridden by a child
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.
Related videos on Youtube
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, 2022Comments
-
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 over 2 yearsI'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 over 10 yearsthx, but I need to override it from the parent POM, not from the command line (as question stated)
-
user944849 over 10 yearsI think you are out of luck then.
-
Jon Onstott about 10 yearsDefining properties in a profile in the parent and enabling the profile doesn't seem to activate the properties in the child project.
-
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 about 8 yearsUnfortunately 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 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 about 8 yearsI 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 over 7 yearsWhat 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 over 7 yearsI 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 about 5 yearsyeah, this is obvious - what if you can't change the child pom?
-
Simon Logic almost 3 yearstitle should be changed, right now it is confusing
-
haridsv over 2 yearsI edited the title and body based on the above clarification from @RobertScholte.