others-how to import spring boot 3 if already has a parent in maven pom?

1. Purpose

In this post, I will show you how to import spring boot 3 if your maven pom.xml already has a parent.

If your pom.xml is just like this:

<parent>
	<groupId>com.bswen</groupId>
	<artifactId>bswen-parent</artifactId>
	<version>1.0.0</version>
</parent>

Now if you want to import spring boot 3 parent as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

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

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.3</version>
    </parent>
    <!-- Additional lines to be added here... -->
</project>

Now our maven will have two different parent, it’s impossible, how to deal with this situation?



2. Solution

Here is the solution:

<dependencyManagement>
    <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>3.2.3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

Here is the key points to take away:

  • Use dependencyManage to import spring boot 3 dependencies, this will not import them directly, but only define the versions of them
  • We specify the scope to import to avoid adding all dependencies directly.

Here is the introduction of scope import:

import This scope is only supported on a dependency of type pom in the  section. It indicates the dependency is to be replaced with the effective list of dependencies in the specified POM's  section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.

And for the <parent> node, here is the explanation:

<parent>The purpose of the tag is to make a project inherit all configurations of the pom in the parent project. That is to say, our project inherits the parent project spring-boot-starter-parent.

In this way, we find that the first method <parent> and the second method <dependencyManagement> achieve the same effect.



3. Summary

In this post, I demonstrated how to import spring boot 3 using <dependencyManagement> instead of <parent> if you already have a <parent> node in your maven pom.xml, they achieve the same effect in essence. That’s it, thanks for your reading.