Possible to make Maven POM Element Not Inheritable? - maven

I have a parent pom which defines a number of plugins and dependencies I want my child poms to inherit. However, we use the scm element as part of our release builds and I would like to add the parent's scm to the pom but it not be inherited by any children pom (in case the child forgets to define it and uses the parent's). Is there a way to set an element to be not inheritable?

I don't think so.
But could you make it use a property that ends up with different values in the parent pom and the dependent pom such that it will fail in the child if they don't have their own <scm> tag? Use <profile> to define the property differently in the parent and child.
Sometimes that works.
The reason it works on occasion is that profiles are not inherited by the child but if the profile in the parent sets a property, that property is inherited by the child.
So, sometimes you can use that "feature" to accomplish what you are doing. If you activate a profile in the parent and the <scm> section is in the profile, it won't be inherited by the child.
Another possible solution is for you to use the permissions on the version control system. The user credentials that allow checkin in the child projects could, in some cases, NOT be given permission to checkin in the parent. If the child project doesn't declare their own <scm> then they won't have permission to fool with the part of the repo in the parent's <scm> section.

Related

Does child pom inherit plugins from parent pom

In a multi module maven project, does a child pom inherits plugins defined in parent pom?
Maven documentation around the plugin inheritance is a bit confusing to me. According to the documentation
If your POM declares a parent, it inherits plugin configuration from
either the build/plugins or pluginManagement sections of the parent.
So I'm assuming only the configuration will be inherited in case if the child has the same plugin defined under <build><plugins> section. If child does not have the plugin then it's not inherited from the parent.
Can someone please confirm if this is the correct understanding, or I'm missing anything here.
You inherit everything, so your understanding is incorrect.

Maven: how to not depend on Parent POM

I have a Maven multi-module project. Something like this:
- ParentProject
- ChildA
- ChildB
- ChildC
The child projects inherit from a Parent POM (ParentProject) solely for the reason of sharing stuff like <build>, <scm> and <properties>, so as to not repeat it in all the child modules. Thus, the objective of the parent-child relationship is not related to dependencies in any way. It plays a role at build-time, not at runtime, so to speak.
The child projects's artifacts are for consumption for a wider audience, hence they'll be published into a centralized repo.
How do I "break" the relationship between from the child up to the parent seen from a perspective of a consumer of a child?
Let's say another project, ProjectX, adds a dependency on ChildA. When doing this the Maven client will attempt to not only download the POM and artifact of ChildA itself but will even try to download the POM for ParentProject. However, there's absolutely no need for that POM seen from a consumer point of view. It doesn't contain information that the consumer needs to know.
How can I break this relationship from consumer's perspective? Forcing the POM for ParentProject to be published into a repo seems pointless as nobody has any need for it there.
Perhaps there's another way that Maven will let me share things like build instructions and properties between projects without mandating that a Parent POM exists in a centralized repo ?
Or perhaps there's some way I can manipulate the POM for the Child projects which gets put into the centralized repo (removing the <parent> element as it is irrelevant).
Perhaps only me but I feel that Maven is conflating two unrelated concepts here (build-time vs consume-time) and forcing unnecessary roundtrips and unnecessary artifacts in repo. I haven't dabbled with Gradle yet but I wonder if it does it any better?
Usually, the Maven POM is both build POM and consumer POM. This is not ideal, and will probably change in future versions of Maven.
At the moment, your best option seems to be the flatten Maven plugin, which allows you to remove "unnecessary" parts of the POM before uploading it.

Need Parent child maven structure in local system

I have three maven projects one parent two children, the parent project has only the Super pom, while one child has the launcher class, and other is a web service that requires that launcher class to get started.
I need all these connected in my local system. What should be the structure that I should follow?
If one project needs classes from a different jar, just declare a Maven <dependency>.

Maven parent POM vs BOM dependency management

Let's say I have a maven parent POM root which defines foo:bar:1.0.0 in dependency management. I have another parent POM parent which uses root as parent (just to add another layer to the example). Lastly I have a bill of materials bom which uses root as its parent but redefines foo:bar:2.0.0 in its dependency management.
In my project app I inherit from parent and then I import the BOM in the dependency management section of app
root (foo:bar:1.0.0) <- parent <- app+bom
^
|
bom (foo:bar:2.0.0)
Which dependency management section wins? Which version of foo:bar do I get?
I know that if I were to directly include foo:bar in the dependency management section of app, it would override that inherited from the parent. But is importing a BOM in the dependency management section equivalent to directly including it in the dependency management section, and sufficient to override that of the parent? Or does the inherited foo:bar from the parent's dependency management take precedence?
According to the maven precedence rules, the version from the root will win and therefore you will get foo:bar:1.0.0, which you will be able to see if you look at the effective POM. I think that this makes a BOM project less effective since you cannot use it to override the version from the parent and have to declare the version in the app or in the parent.
The Precedence
So, there are multiple ways of deciding the versions,
which means there is an order of precedence.
Versions provided in the direct declaration in POM take the highest precedence.
Version provided in parent pom takes second precedence.
Version from imported pom takes third place
Lastly, whatever we get from dependency mediation

Maven include another pom for plugin configuration

is there a way to include another pom or information in a maven pom ?
I have several poms which are basically not related or have different parent poms. Nevertheless for packaging it is required to have a Manifest identical to all projects.
So currently I have in a pom:
<plugin>
<artifactId>maven-assembly-plugin>
<!--- .... -->
<archive>
<manifestEntries>
<build-date>....</build-date>
<build-nr>.....</build-nr>
etc etc
I would like to avoid to paste this configuration to all severall poms.
So how can I share the configuration of a plugin without inheritance ?
Thanks
One way to do this is using pluginManagement section. plugin configurations can be defined in this section in a parent pom and will be available to inherited poms to be used as is or overridden.
Here is the relevant maven documentation. In your specific case, you would need to organize your projects/poms suitably.
The only correct answer is to use inheritance. Have an inherited ancestor with this configuration in it. Since you have existing parent POMs, these must inherit from this new parent. If this isn't possible then rethink the hierarchy of your Maven projects, or else you'll have to copy and paste the same configuration into each file and add a comment indicating the section must not be modified / must be maintained consistently with [insert list of projects here].
TLDR; Inheritance is designed specifically to resolve situations such as yours. If you can't use it then don't try to hack around it - either restructure your POMs or copy and paste!

Resources