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?

haridsv
  • 9,065
  • 4
  • 62
  • 65
zacheusz
  • 8,750
  • 3
  • 36
  • 60
  • 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)? – rogerdpack Dec 10 '21 at 21:59

3 Answers3

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.

Brad Cupit
  • 6,530
  • 8
  • 55
  • 60
Robert Scholte
  • 11,889
  • 2
  • 35
  • 44
  • 3
    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. – Lawrence Sep 23 '16 at 11:49
  • 2
    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". – Robert Scholte Sep 24 '16 at 15:44
  • 2
    title should be changed, right now it is confusing – Simon Logic Jul 05 '21 at 09:19
  • I edited the title and body based on the above clarification from @RobertScholte. – haridsv Jan 03 '22 at 06:43
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.

user944849
  • 14,524
  • 2
  • 61
  • 83
  • 2
    thx, but I need to override it from the parent POM, not from the command line (as question stated) – zacheusz Sep 05 '13 at 17:01
  • 2
    I think you are out of luck then. – user944849 Sep 05 '13 at 18:30
  • 1
    Defining properties in a profile in the parent and enabling the profile doesn't seem to activate the properties in the child project. – Jon Onstott May 07 '14 at 16:15
  • @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](http://jira.codehaus.org/browse/MNG-5127) is one example. – user944849 May 07 '14 at 18:36
  • 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. – Jesse Glick Apr 27 '16 at 16:21
  • @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. – user944849 Apr 27 '16 at 18:01
  • 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). – Jesse Glick May 02 '16 at 15:27
-1

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.

kanaparthikiran
  • 523
  • 12
  • 15