Migrate parent pom to gradle in a spring boot project - gradle

In my spring boot project the pom.xml has a <parent> tag which refers to the parent pom.
<parent>
<groupId>com.sintara.codify.ems</groupId>
<artifactId>ems-parent-pom</artifactId>
<version>1.0.0</version>
</parent>
I am migrating my project from maven to gradle.
How do I take care of this or parent pom concept in Gradle.
Is there any way to do it without converting it into a multi project setup.

In gradle we have similar usage. We can create a parent.gradle file in project root path, and declare common dependencies in it. just like below:
ext {
parent = [
"ems" : "com.sintara.codify.ems:ems-parent-pom:1.0.0"
]
}
And then in your child gradle file, you can use it:
dependencies {
implementation rootProject.ext.parent["ems"]
}

Related

Why spring.io generate pom without version?

I'm new to springboot and using spring.io to create project in order to create microservices.
When creating a project using spring.io website, a pom is created with all the relevant
dependencies but versions are not added.
Should I add the versions myself looking the maven repository jar (all jars include
versions on them)?
All dependencies (and configurations) are managed by Spring Boot. The parent of the project you generated with Spring Initializr has the parent set to spring-boot-starter-parent:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
The parent of spring-boot-starter-parent is spring-boot-dependencies which defines all dependency versions.
Therefore you don't need to specify any versions of the starter dependencies or their dependencies manually.
Please take a look at The Spring Boot Starter Parent on Baeldung for a quick overview.

Gradle not handling Maven parent POMs with version-range

I have a Gradle build that needs to fetch dependencies from a Nexus repo that has been populated using Maven. Some of the POMs there use version-ranges when specifying a parent POM.
In my build.gradle I put a dependency:
compile group: 'com.company.platform', name: 'abc-common', version: '[11.0,12.0['
The Gradle logs show that a request is made for the versions of abc-common, and its POM and its parent's POM are fetched:
abc-common-11.0.7-17042111.pom:
<parent>
<groupId>com.company.platform</groupId>
<artifactId>abc-parent</artifactId>
<version>11.0.7-17042111</version>
<relativePath>..</relativePath>
</parent>
abc-parent-11.0.7-17042111.pom:
<parent>
<groupId>com.company.corporate</groupId>
<artifactId>corporate-parent</artifactId>
<version>[11.0.7,11.0.8)</version>
<relativePath/>
</parent>
But Gradle fails to fetch the corporate-parent POM. Instead of requesting what versions are present on the repo, it tries to fetch using the version-range literal:
Loading http://nexus.company.com:8081/.../corporate-parent/[11.0.7,11.0.8)/corporate-parent-[11.0.7,11.0.8).pom
Am I doing something wrong? Is this a known problem with Gradle? Is there a work-around?
Any help is much appreciated. This is a show-stopper for me. If I can't find a solution, it's back to Maven.

Maven - Access properties on parent pom from a child pom

In a multi module project structure as
myApp
|-moduleA
|---pom.xml
|-moduleB
|---pom.xml
|-pom.xml
If i have the following properties in the parent.pom
<properties>
<moduleA.version>4.67</moduleA.version>
<moduleB.version>4.68</moduleB.version>
</properties>
How can i access the properties in the parent pom from any of the child poms? I tried this on the child pom but it didnt work.
<groupId>com.test</groupId>
<artifactId>moduleA</artifactId>
<version>${moduleA.version}</version>
If you have a real multi-module build you should never define the modules to have different versions. They should have the same version which make releasing possible and other things as well. Otherwise you should not use the multi-module setup than use simple single modules which are separated.
This should work. One possible reason I can think of is that perhaps you don't actually inherit the pom where these properties are defined (i.e. it's not defined as your <parent> directly or indirectly), but you only have a main pom that aggregates your projects. It's a guess, though.

How to reference a parent project from a child

I have a multi module project like this:
gwt-app
model
webapp (depends on gwt-app and model)
when I try to execute any goals in webapps, for example, launch jetty, build fails because maven can't find its dependencies (I didn't install modules into a local repo). Is there a possible way to reference the parent pom so that when I run any goals in a submodule, all its dependencies will be compiled (recompiled)?
An example of your pom files would be great but in multi module projects I always declare the dependencies in the parent pom in the dependencyManagement tag:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact-a</artifactId>
<version>1.0</version>
</dependency>
...
In the module pom I just delcare the dependency without the version:
<dependencies>
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact-a</artifactId>
</dependency>
...
That way were are sure each module uses the same version.
The thing to remember is that modules in maven do not inherit dependencies from the parent. You must declare the dependencies used in the module itself.
Another thing is, I believe that when you are running outside of an IDE (which searches the workspace for dependencies) you need to have each module installed in your local repo. I do not think maven will search for un-installed dependencies within a multi module project if you are not executing on the parent pom.
If you make your parent pom just have regular setup like
<plugins>
</plugins>
<dependencies>
</dependencies>
Then anything in those groups are inherited automatically into the child pom. Child pom just needs the parent section in it:
<parent>
</parent>
You don't even need to declare the same plugins or dependencies in the child pom in this manner. You only need to list the plugins or dependencies in the child pom IF you use or in the parent pom OR you want to override something in the parent pom. I actually just went through all of this week and have my builds working nicely now (small child poms with more things in the parent pom like plugin configurations).
I asked a question about this that might help you:
Maven - Parent Pom - Child Inheritance

What does the parent tag in Maven pom represent?

E.g.:
<parent>
<groupId>mycompany.trade.com</groupId>
<artifactId>mycompany.trade.</artifactId>
<version>1.1.1.0-SNAPSHOT</version>
</parent>
Does it mean that Maven will search for parent pom?
If yes, where, in which order? May be in folder up 1 level? Or in local repository or in repo?
Yes, maven reads the parent POM from your local repository (or proxies like nexus) and creates an 'effective POM' by merging the information from parent and module POM.
See also Introduction to the POM
One reason to use a parent is that you have a central place to store information about versions
of artifacts, compiler-settings etc. that should be used in all modules.
The common dependencies,Properties,constants etc can be definded in central parent project pom.xml
The main important thing is the parent project cannot be distributed and it looks quite similar to a regular "pom.xml" except that it also has a packaging tag
<groupId>com.company.demo</groupId>
<artifactId>MavenInheritance</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
The child now able to inherit this using
<parent>
<groupId>com.company.demo</groupId>
<artifactId>MavenInheritance</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
As the name suggests, we can point out a parent pom.xml file for the current pom.xml file. Doing so, dependencies, properties, constants and many more defined at the parent pom.xml file also get merged with the current pom.xml (child pom.xml) file. Say you have a parent tag in your projects pom.xml that looks like below:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
</parent>
Then maven reads that parent POM from your local repository (or from repository managers like sonatype, jfrog, etc that you have configured) and creates a Resultant POM by combining the parent POM and your module’s POM.
To see the combined result use the following mvn command:
mvn help:effective-pom
This is the practice that is used in multi-modules projects where we need to inherit the dependencies from the parent projects.

Resources