How does Maven dependency work with Multiple Parents? - java

Looking at the Maven Dependencies with respect to Multiple Parent Ids. If such is the structure of Parent-ids. Note below mentioned all are Maven Projects and each contains its own pom.xml.
Parent-1
Project_1
Project_11
Parent-2
Project_2
Project_22
Scenario 1:
Let us say Project_2 is dependent on Project_1 and Project_11....Then which of the following is true?
1. Add both the dependencies in Parent-2 pom.xml and expect Project_2 to discover sources of Project_1 ? Logic being that Parent-2 is anyways the parent of Project_2 and therefore building Parent-2 would enable all its child to discover the dependencies.
2. Dependency of the Project_2 has to be done at its own level..which means Project_2 pom.xml has to include both Project_1 and Project_11 .
Is there any other way to manage the dependencies?

You can add a dependency to Parent-2, but then it becomes a dependency of both Project_2 and Project_22. This is probably not what you want. So it it would be better to add the dependencies at the level of Project_2.

Related

How version of lib defined in classpath?

i have hirarhy of modules.
I set up version of hibernate-validator as 6.1.5.Final
But when i build project version of library another
maven dependencies: tree ouput
org.hibernate:hibernate-validator:jar:5.3.4.Final:compile
I can not understand how it works.
I put all dependency tree here https://paste2.org/CwB2H4W2
Problems with dependencies should always bring you to the "Dependency Hierarchy" Tab of your POM.xml. There you will see your projects dependencies, and dependencies of that dependencies.
If I had to guess I'd say there you will find a module providing the dependency you think you don't use.
Further information to maybe change this:
First declared dependencies get used first. So if you define your dependency before the Module that brings the other dependency in, Maven should select yours.

Integration testing of Maven Plugin submodule

I have Maven project with multiple submodules. One of these submodules, let's say submodule X depends on all other submodules, as it is Maven Plugin that integrates everything else.
Now I want to use this Maven Plugin during verify stage in my parent POM to do real run of it over entire project (kind of eating own dogfood).
Unfortunatelly I'm getting error:
The projects in the reactor contain a cyclic reference
So how can I make such dogfood integration test for a Maven Plugin submodule?
I would review aggregation and inheritance: they are indeed two different concepts in Maven often used in combination but which can however be used in a complete separated manner as well.
In this case, your plugin submodule is certainly defined as a module in the aggregator pom (that is, it is a submodule). But I also presume the plugin submodule also has as parent pom the aggregator pom as well (that is, the aggregator pom is also the parent pom, which is a normal approach, but not always required).
Is the latter required? You could keep on having the multi-module/aggregator approach without necessarely having the aggregator as parent of the plugin submodule. As such, the plugin subModule would still be a module of the aggregator pom but it would not have as a parent the aggregator pom, decoupling it from it and as such breaking the cyclic dependencies.
Possible drawback: in the aggregator pom you were also defining common things (dependencies management, properties) required also in the plugin submodule. If the case, you would then need to duplicate/review these common settings only for the concerned submodule.
Small suggestion from my side. Cyclic means you have some transitive dependency in your modules. Can please check the dependency tree of your modules and resolve the circular dependency first.

Building multiple modules together using maven

We have 3 modules say A,B and C.These 3 modules are cross dependent.To compile A I want B and C.To compile B I want A and C.To compile C we want B and A.So how to build these 3 modules together and we want to get A.jar,B.Jar and C.war.
Use <dependencies> section in pom.xml to add a dependency for one another module.
Create a parent pom for all of your modules. When building parent, maven builds all children (except if you use -pl :mymodule selector on command line). See parent section of pom
You can also add a 4th child module D (without artefact, just a pom module) with A, B, and C dependencies or a subset. When you build D, maven compiles A, B, C with the option -am (also-make) which forces maven to build the selected module and dependencies too.
Note that you must avoid cyclic dependencies into your code or maven will fail to build your project. If you have a cyclic dependency, extract the code need by two modules into one another and adding a dependency in both
What you are explaining is the cyclic dependency. Maven does not support cyclic dependencies and your build will fail if your try to do that.
You can create a multi-module maven project. Move all the common functionality to Module A. Add the dependency of Module A in Module B and Module C's pom.xml <dependencies>...</dependencies> section.
Maven will build the modules in the order you specify in the parent project pom.xml <modules>...</modules> section. In this way you create A.jar, B.jar and C.war by defining the <packaging>...</packaging>as jar or war in their respective module's pom.xml
These links will be helpful - link1 link2

Maven dependency grouping via poms

I have a multi-module maven project that uses some dependencies from netflix. The trouble is that those dependencies are built using gradle and use a runtime scope. When I build my project I then have only the direct dependency loaded but not it's own dependencies.
To fix that I added dependencyManagement entries to transform all the runtime scopes to compile scopes. They usually go in pack of 10 to 20 entries so what I want is to be able to make some pom that would only address this issue and be able to load it any time I need a dependency.
I know this can be done as i've read Maven pull dependencies via middle pom file and How to use POMs as a dependency in Maven? but I was wondering how I may carry those poms inside my current multi-module project.
I'd like to have those in my parent module and avoid needing to create one sub-module per pom. Like pom-dep1.xml, pom-dep2.xml... and have those bundled in my build. Is that possible ?
Thanks

Multi-module IntelliJ project with maven - How to add dependencies from one module to another?

Let's say I have a maven project which has some maven modules inside.
My main module depends on the other modules, so when I compile the main module they should be compiled together.
The question is, how to add these modules as dependencies to the main module?
I know if I have a custom lib that I want to use with maven, let's say a utilities project, I have to compile the jar of the project, do a mvn install:install-file on it to install it on the local repository and then add it to the pom.xml.
Do I have to do this with all my modules and add the dependency to the pom.xml on my main module? Because if it should be done like this, there will be a lot of work to do when changing code on the other modules.
What is the best practice to use avoid the trouble of compiling/installing the modules to local repository?
The question is, how to add these modules as dependencies to the main module?
The same way you add any other dependency to your maven project. By adding group id, artifact id and version to <dependency> element
Do I have to do this with all my modules and add the dependency to the pom.xml on my main module?
If your main module depends on some module A then only the pom of the main module should contain dependency declaration towards module A. You do that for all the dependencies of your module.
I don't know what you mean by "a lot of work when changing the code on other modules". Maven has nothing to do with code changes, it just builds the projects whatever they look like at the given moment...
What is the best practice to use avoid the trouble of compiling/installing the modules to local repository?
Any project that you invoke mvn install on gets built and it's jar copied to local repository. That's all you need to do to get the jar into the repo. This will also put all the dependent jars, if available, into the local repo.
As for best practices for multi module projects:
If your parent project (the one that has modules inside) has <modules> section that lists the modules of your application, and modules are in subdirectories of your parent project, then you simply mvn install (or whatever you want to do) the parent project and that will cause all the modules to be built in order defined by declared dependencies between them. That means that if your main module has dependency on module A, then module A will be built before the main module. This way you can build and install all your modules with one command. On the other hand this approach makes more tight coupling between modules which is not desired in some cases, so it depends on your use case whether it is a good approach or not.

Categories