Aggregation of Maven two different Poms? - java

In Maven is there any way i can aggregate different parent poms into one POM?
More Description About My Question :-
I am going to have two POM's
One POM (a) is for Dependency Management.
The other POM (b) is for Plugin Managment.
By clubbing(aggregating) these two Poms i will create One Parent POM and i will ask my other module team to use this.
i know Maven supports Inheritance .
Using Inheritance we can achieve this requirement.
Is maven supports the Composition Of two different Poms ?
If Yes how i can i achieve this ?
How composition works in maven ?

This is possible for dependency management. In the <dependencyManagement> section of your POM "b" you can add a dependency to POM "a" with scope import.
From the Maven documentation:
This scope is only used on a dependency of type pom in the section. It indicates that the specified POM should be replaced with the dependencies in that POM's section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.

Related

Is it possible to add a check that all submodules have the same version defined as in parent pom?

I have a project with numerous submodules.
In each submodule pom.xml file, I repeat exactly the same version number that was defined in parent pom.xml file.
Is it possible:
to define version number in smart way, in only one place, in only one pom.xml file?
to add some verification during build phase that all of the pom.xml files have exactly the same version defined?
For 1: This might be of help
Maven project version inheritance - do I have to specify the parent version?
For 2: You may think of writing your own maven plugin and injecting it with the maven lifecycle.
Please refer to: https://www.mojohaus.org/versions-maven-plugin/

Exclude certain dependencies from class path in a maven project

I have a maven project with some dependencies. Some of them have transitive dependencies which depend on, say, some jar A. I want to exclude this jar. Okay, I can add exclusions to every dependency which itself depends on A. But its a bit tedious. Is there any way to put this exclusion in one place in a maven project configuration?
Use a parent pom where you have <dependencyManagement/>. Then extend this parent in your POM files.
Alternatively, if it's one project that contains all these dependencies and they contain a transitive dependency with multiple versions on the dependency tree, define that dependency explicitly in your POM file and this will override the transitive versions.
See my explanations here for a similar question.
OKay, it looks like there is no way to do this.
According to documentation
Why exclusions are made on a per-dependency basis, rather than at the POM level
This is mainly done to be sure the dependency graph is predictable,
and to keep inheritance effects from excluding a dependency that
should not be excluded. If you get to the method of last resort and
have to put in an exclusion, you should be absolutely certain which of
your dependencies is bringing in that unwanted transitive dependency.

Grouping of Dependencies in Maven 3

I am using maven 3 for building my huge multi module project. I have nearly 80 projects and many dependencies are used by many project thus makes me to add the same dependency entry in all my projects. In case if i am updating/moving any of my dependency to its latter or earlier version I need to update in all other projects which uses that dependency which is quite impossible.More over it is not possible to create a parent pom for set of projects which has same dependency.
Is there any plugin just to map as below in a pom of parent project
<dependency>somegroup:somefact:anyV<dependency>
<projects>
<project>somegroup:somefact:anyV</project>
</projects>
or is it possible to implement a plugin on my own </br>
or whether maven provide any other way to do this in its style.
Maven has a concept of importing dependencies. The documentation says that this feature is for cases where extending a base POM is not possible. I have not used this feature so I'm not sure how it works, might be what you are looking for though.

Factoring out common elements of Maven pom.xml

I have a number of Maven pom.xml files in various projects that contain a lot of similar configuration (license declarations, plugins, defintion of resource folders etc.)
What is the best approach to factor out these common elements into a parent pom and have them available for re-use?
I'm particularly interested in:
What should go in the parent pom vs. being project specific?
Where to store the parent pom, e.g. on GitHub?
Any gotchas to be aware of?
For enterprise projects, you can use Maven to deploy your parent pom on a artifact repository like Sonatype Nexus. But this can be cumbersome for just personal projects.
Everything that needs to be shared must be in parent pom.

Pull dependencies of modules by referencing parent in maven java project?

I have a maven-java project (say Project A) with a parent defining modules in its pom.
I also have an external project (say Project B) that requires dependencies of two of the modules from Project A. For now, i have defined the dependency to pull each module individually.
When i replace these two with a dependency on the parent pom, it errors out on build. Is there some modification i need to make to my parent pom of Project A to make this work?
Can this be done in the first place?
Can this be done in the first place?
Declaring a dependency on an aggregating POM won't get the modules transitively. This is not going to work. It is possible to create a POM to group dependencies though.
For example, EHCache uses this technique. As mentioned in their documentation:
Maven Snippet
To include Ehcache in your project
use:
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.0.1</version>
<type>pom</type>
</dependency>
The net.sf.ehcache:ehcache artifact is precisely used to group dependencies (and is distinct from net.sf.ehcache:ehcache-parent).
References
The Maven Guide
3.6.1. Grouping Dependencies

Categories

Resources