Automatically including sub-modules of another maven project - java

I have my project structure like this.
pro
pro-common
pom.xml
pro-list-history ==> [1] Packaging type pom
pro-list-main
pro-list-entities
pom.xml
pro-list-daos
pom.xml
pro-list-services
pom.xml
pom.xml
pro-search
pom.xml
pro-customers
pom.xml
pom.xml
pro2
pro-list-history
pro-list-main
pro-list-entities
pom.xml
pro-list-daos
pom.xml
pro-list-services
pom.xml ==> Want to use [1]
pom.xml
pom.xml
My question is is okay to use groupId and artifactID from [1] in the second project as shown above?
The referred module packaging type is pom.
Put a dependency section in the project two at the specified module as shown above, and specified as pom. Build is fine, but its not importing the dependencies from that project.
Can anyone help?

First, you can depend on a POM dependency. See: Netbeans: maven dependencies of type pom for an example and discussion.
However, I think you are asking if there is a short-hand way to import all the sub-modules of a "parent" module by specifying its pom.If so, see this question:
Maven - include all submodules of a pom as dependencies in another module

You can just reference the sub-module in another module like you would a file:
<modules>
<module>inside-project-module</module>
<module>inside-project-module</module>
<module>inside-project-module</module>
<module>../OtherProject/outside-project-module</module>
<modules>

Related

Maven Reference Pom in Child Module By Name

We are using a parent pom that has a child module where we have 2 pom files - the one named pom.xml and other being images-pom.xml.
This is the situation because we are doing some naming changes and for the time being we want to have them both.
In our parent pom we have the following code
<modules>
<module>child</module>
</modules>
<packaging>pom</packaging>
By default it seems that this is looking and trying to build the pom.xml - but in reality we want to use the images-pom.xml Is there any way to achieve this without creating a new module and using profiles?

What is managed dependencies in maven repository website?

I know maven dependencies have different scopes.
But what is Managed Dependencies?
The dependency you are looking at is not a jar file but a pom.xml that is meant to be used as "bom" (bill of materials). It contains the preferred versions of dependencies, so you would not need to inherit from that parent pom to use those version numbers but you can import them. See BOM section in the introduction: https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#bill-of-materials-bom-poms
if you look into that pom.xml file: https://search.maven.org/artifact/org.apache.logging.log4j/log4j/2.13.3/pom you will see the dependencyManagegement section. If you import that pom that section is added to your own dependencyManagement secion (sort of). Its not dependencies yet, just preferred versions.
these kind of dependencies can only be added into the dependencyManagement section of the pom.xml - I assume the gradle dependency resolution follows that behaviour but I'm not sure about that, bom support took a while in gradle to be supported.

Maven multi module jenkins project

I've a multi-module maven application that we need build through Jenkins.
Project structure is like:
a.xml
<artifactId>parent-1</artifactId>
<modules>
<module>lookup-1</module>
<module>lookup-2</module>
</modules>
lookup-1.xml
<artifactId>lookup-1</artifactId>
<parent>
<groupId>com.lookup</groupId>
<artifactId>parent-1</artifactId>
<version>1.0.1-SNAPSHOT</version>
</parent>
<name>lookup-1</name>
lookup-2.xml
<artifactId>lookup-2</artifactId>
<parent>
<groupId>com.lookup</groupId>
<artifactId>parent-1</artifactId>
<version>1.0.1-SNAPSHOT</version>
</parent>
<name>lookup-2</name>
now,
mvn clean install -f a.xml works fine. For this I created one Jenkins job.
Now, same as a.xml, I've another project with b.xml. b.xml has the same code as of a.xml except different Ids. So, I've created another jenkins job for b.xml.
Both jobs work fine. But, now I want to build both these project from single Jenkins job based on which project we commit in Git. For, this I want to have a new project(pom.xml) and where I want to put both a and b under modules tag. Like this:
pom.xml
<name>combined_project</name>
<artifactId>combined_project</artifactId>
<modules>
<module>a</module>
<module>b</module>
</modules>
a.xml
<artifactId>parent-1</artifactId>
<name>a</name>
<parent>
<artifactId>combined_project</artifactId>
</parent>
<modules>
<module>lookup-1</module>
<module>lookup-2</module>
</modules>
But, its not working for me. I'm getting following exception in maven:
Child module D:\....\a of D:\....\pom.xml does not exist
Maven not able to find child module.
I've following project structure:
project
|_ lookup-1
|_ lookup-1.xml
|_ lookup-2
|_ lookup-2.xml
|_ a.xml
|_ b.xml
|_ pom.xml
Any hint?
a.xml has 2 sub modules lookup-1 and lookup-2. By default maven is looking for the sub modules in the nested maps. So you need to have the following directory structure:
<parent>
|
+- a/pom.xml
| |
| +- lookup-1/pom.xml
| |
| +- lookup-2/pom.xml
|
+- b/pom.xml
|
+- pom.xml
Alternative is to change the parent into
<name>combined_project</name>
<artifactId>combined_project</artifactId>
<modules>
<module>a</module>
<module>b</module>
<module>lookup-1</module>
<module>lookup-2</module>
</modules>
Maven follows a project structure and each <module> will contain a pom.xml file. As you have explicitly stated the pom file in command line that file will be selected as the pom.xml for that project. However, for every submodule that is listed maven will search for the exact file named "pom.xml" for each submodule.
From the folder structure provided, it can be inferred that a.xml can act as a pom file when explicitly specified in command-line but it cannot act as a module as it does not have a dedicated folder with pom.xml.
A maven project in general has the following structure:
archetype-id(folder)
pom.xml(packaging: pom)(with one module module-artifact-id)
module-artifact-id(folder)
pom.xml
If you are attempting to build only the module that has been updated then the module that is not checked in recently will not be part of the newly created archive. But if you still would want to do it for some "special" reason. The following method might be the one you are looking for:
(I wouldn't recommend it -- as it is messy and goes against conventions)
Have two sub-modules in a single project and also Have two .xml files (pom files to be specified in commandline). In one xml file lets say "a.xml" "module-1" is to be specified as a module and in "b.xml" "module-2" is to be specified as a module.
Using the currently modified module invoke the build on that pom file. Example, if module-1 is modified specify a.xml in the command line as the pom.xml.
Please note that I have not attempted it but if it is possible this is one of the ways I can think of by which we can accomplish what you are looking for.
Reference for passing parameters : Pass a dynamic parameter in Jenkins build
Two possibilities :
Either you need to fix the path of your submodules in the modules section of the parent pom.
While creating a jenkins job you will have to tell jenkins to clone submodules as well. when I started with jenkins, I faced similar problem. The solution was to enable "Recursively update submodules" checkbox under Advanced scm behaviors in Jenkins UI.

Why sometimes dependency not contain version property in pom.xml?

I am a newbie of Maven, currently reading Hadoop source code, and found something interesting in some pom.xml files:
Some of the dependency node do not contain version node at all.
Question: why is it like this?
for instance, this pom.xml.
Because specific version of dependency in parent pom.xml file
https://github.com/apache/hadoop/blob/trunk/pom.xml
Reference: https://maven.apache.org/guides/introduction/introduction-to-the-pom.html
As I commented at first, a pom file can have a parent (via inheritance) and such a parent may provide some governance and harmonization across all of its children. A classic case is to provide versioning for certain dependencies via a dependencyManagement section.
is used by POMs to help manage dependency information across all of its children. If the my-parent project uses dependencyManagement to define a dependency on junit:junit:4.0, then POMs inheriting from this one can set their dependency giving the groupId=junit and artifactId=junit only, then Maven will fill in the version set by the parent. The benefits of this method are obvious. Dependency details can be set in one central location, which will propagate to all inheriting POMs.
The mentioned pom has indeed a parent pom:
<parent>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-project-dist</artifactId>
<version>3.0.0-SNAPSHOT</version>
<relativePath>../../hadoop-project-dist</relativePath>
</parent>
Which in chain has another parent pom file which defines several dependencies as part of its dependencies management section.
If you really want to check the effective (merged) pom your build is using, you could run:
mvn help:effective-pom -Doutput=effective-pom.xml
And the maven-help-plugin will produce an additional pom as specified by the command above, merging the current pom file and all of its anchestors.
In Maven you can inherit from parents folder in order to merge or inherit some properties. This can be the version of the modules. Usually you have a "super" POM in the root folder of your project and you put there all the commons dependencies in order to controll them in an easier way. I.e. If you must change one module version, you only need to change in the "super" POM and not in each POM inside each subfolder that need it. If you need more information about POM inheritance the documentation has a couple of useful examples.
https://maven.apache.org/guides/introduction/introduction-to-the-pom.html#Project_Inheritance

Taking out a part of a maven project as a module

I have a maven module A that is dependent on the classes in the module B (both are child modules in a project).
In the A's pom.xml I have the following:
<dependencies>
<dependency>
<groupId>test.pack</groupId>
<artifactId>B</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
But when I try to build the A, the B does not appear in the dependencies correctly, I get compilation errors in the class that is in the A's test.pack.packFromA package in the import statement, which looks like import test.pack.packFromB.*.
So, my B dependency doesn't work correctly. But I thought classes from the B would be packed and added in the classpath, so I could use them. I tried to add <type> in that dependency, but that didn't help. What am I doing wrong here? Thanks in advance.
EDIT:
The <modules> part of parent's pom.xml looks like that:
<modules>
<module>B</module>
<module>A</module>
</modules>
Here is the error I get: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project A: Compilation failure: Compilation failure:
Have you done mvn install in module B, before you tried to build module A?
(If you do "mvn package" instead of mvn install, module B won't be placed in the repo, so Maven won't find it when building module A).
Also, have you tried building the multi-module project from the parent module? (When you do this, Maven will build the modules in the correct order)
If that's not it, verify that the classes you are referencing are located under src/main/java/test/pack/packFromB in module B. If they are under src/test/java.. you need a specific type of dependency for that.
Hope that helps.

Categories

Resources