springspring-bootmavenpom.xmlparent-pom

Why can't I import classes from a dependency that exists in the parent POM?


I am a beginner in Spring. In my project, the pom.xml defines the parent as spring-boot-starter-parent, and I see that the parent of spring-boot-starter-parent is spring-boot-dependencies. The spring-boot-dependencies already has the lombok dependency, but I am unable to import it in my project. I'm not sure if my understanding of the parent's role is correct. I hope someone can help clarify this.

Thank you.

mypom.xml

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    .............
</project>

spring-boot-starter-parent-3.1.2.pom

   ..............
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>3.1.2</version>
  </parent>
  <artifactId>spring-boot-starter-parent</artifactId>
  <packaging>pom</packaging>
  <name>spring-boot-starter-parent</name>
  <description>Parent pom providing dependency and plugin management for applications built with Maven</description>
  ..........

spring-boot-dependencies-3.1.2.pom

.............line 1306
      <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>${lombok.version}</version>
      </dependency>
.............

Solution

  • If you check spring-boot-starter-parent pom file you will see it also has a parent pom file named spring-boot-dependencies. In spring-boot-dependencies.pom, dependencies are declared inside dependencyManagement section, which means when you set spring-boot-starter-parent as your parent pom, you inherit the versions of the dependencies declared in dependency-management part but not the dependencies. So when you add lombok dependency to your project without a version, it will provide the same version that spring-boot manages. If you inherited all the dependencies declared in a parent pom, you would have huge jar files with so many dependencies which are not needed.

    I hope it helps,