How to have another parent dependency as well as Spring Boot parent in maven pom.xml file?
Asked Answered
M

2

7

I am currently developing a Spring Boot multi-module project.

When you set up a Spring Boot project, in your pom.xml you are required to have a parent reference to Spring Boot, e.g.

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
    </parent>

What I would like to have is my project be a submodule Spring Boot project of a wider project (i.e. I would like to package them together under one project folder in a Repo).

Now, normally in order to setup multi-module project, you put the parent pom info like above into your submodule pom.xml.

But with the requirement of having the Spring Boot parent as a requirement, it is not possible to do so.

I've seen suggestions already on the net recommending to move my submodule out and have it as a separate project independently but I think this would be my last resort right now unless that is the only way to go.

Does anyone have any ideas on how to achieve what I want or whether what I want to achieve is practically feasible or not?

Looking forward to hearing option...

Maieutic answered 13/2, 2018 at 21:21 Comment(0)
D
7

You can't have different parent POMs. Unless you want to extend the Boot parent into your own parent POM and work from there, which might be feasible.

The other option is to use the Spring Boot BOM with DependencyManagement, it's a tiny bit more of less concise when you want to override dependencies e.g. normally you just override a maven property.

However when using the BOM you need to order the overrides, and then the boot BOM last in a dependency management tag. Example from the Spring Docs,

<dependencyManagement>
<dependencies>
    <!-- Override Spring Data release train provided by Spring Boot -->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-releasetrain</artifactId>
        <version>Fowler-SR2</version>
        <scope>import</scope>
        <type>pom</type>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>1.5.10.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
</dependencies>
</dependencyManagement>

See, https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-build-systems.html#using-boot-maven-without-a-parent

For more information on setting up a Spring Boot project without using the Spring Boot Parent POM.

Dorindadorine answered 13/2, 2018 at 22:49 Comment(3)
this seems like the way to go as advised by Spring Boot themselves. πŸ‘ So i'm accepting this as answer – Maieutic
after doing further research it looks like it is advisable to prevent going towards a multi-module project setup since it defeats the purpose of what Spring Boot intends to offer. By the looks of it, appears Spring Boot is best suited to be set up as a single project micro-service. – Maieutic
It generally is but that isn't to say you shouldn't given the use case. We are using BOMs in a multi-project that creates a spring boot starter but also packages up an example implementation of a boot application using the starter. – Dorindadorine
N
0

You can create a parent pom.xml and add spring-boot parent in this pom.xml and also you have to add as module other projects with

If you add parent pom.xml in module pom.xml you can access spring-boot via parent pom.xml

parent project pom.xml

...

<groupId>com.example</groupId>
<artifactId>parent-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<name>parent-project</name>
<description>Rent A Car Operation System Parent Project</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.M3</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<modules>
    <module>module-1</module>
    <module>module-2</module>
    <module>module-3</module>
</modules>
...

module project pom.xml

...
<name>module-1</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>com.example</groupId>
    <artifactId>parent-project</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>
...

You dont need add dependencies which are already added in parent project pom.xml as well

Dont forget <packaging>pom</packaging> in parent-project pom.xml

Naranjo answered 14/2, 2018 at 7:26 Comment(1)
this is a possible solution but it sounds like a hack to me to make it work. The reason i didn't go for this approach is because in my use case only module-1 is Spring Boot and the other modules are not. Therefore i don't feel it makes sense to have Spring Boot at the very root. – Maieutic

© 2022 - 2024 β€” McMap. All rights reserved.