How to use Spring Boot BOM, instead of parent?

2.8k Views Asked by At

I need to use BOMs in my project, so simplify the task of managing dependencies. This is the requirement, I cannot pivot at this stage.

I removed parent spring-boot and added AWS, Spring Boot and Junit5 as BOMs. The issue is, even though I have them, dependencies aren't downloaded correctly. Am I missing some plugins or I am doing something else wrong?

I have tried:

  • Run code without BOMs
  • Research BOM usage
  • Run project with parent of Spring Boot
  • Research similar projects on GitHub

All of the above worked correctly. The issues I am getting after adding BOMs are:

package org.junit.jupiter.api does not exist
package org.mockito does not exist
cannot find symbol
[ERROR]   symbol: class ExtendWith

My pom.xml file:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>bom-experiments</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <junit-jupiter.version>5.7.2</junit-jupiter.version>
        <aws-sdk.version>2.18.16</aws-sdk.version>
        <mockito-inline.version>4.8.0</mockito-inline.version>
        <org.springframework.boot.version>2.7.5</org.springframework.boot.version>
        <org.springframework.version>5.3.24</org.springframework.version>

        <lombok.version>1.18.24</lombok.version>
        <maven-compiler-plugin.version>3.10.1</maven-compiler-plugin.version>
        <maven-surefire-plugin.version>3.0.0-M7</maven-surefire-plugin.version>
        <junit-jupiter-params.version>${junit-jupiter.version}</junit-jupiter-params.version>
        <kubernetes-maven-plugin.version>1.9.0</kubernetes-maven-plugin.version>

        <!-- Plugins -->
        <mockito.version>4.9.0</mockito.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-framework-bom</artifactId>
                <version>${org.springframework.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${org.springframework.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>software.amazon.awssdk</groupId>
                <artifactId>bom</artifactId>
                <version>${aws-sdk.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.junit</groupId>
                <artifactId>junit-bom</artifactId>
                <version>${junit-jupiter.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-bom</artifactId>
                <version>${mockito.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- Secret managers -->
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>secretsmanager</artifactId>
        </dependency>

        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>ssm</artifactId>
        </dependency>

        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>sts</artifactId>
        </dependency>


        <!-- Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <scope>provided</scope>
        </dependency>

        <!-- Testing -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-inline</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-junit-jupiter</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>bom-experiments</finalName>
        <sourceDirectory>${project.basedir}</sourceDirectory>
        <testSourceDirectory>${project.basedir}/src/test</testSourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven-surefire-plugin.version}</version>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${org.springframework.boot.version}</version>
            </plugin>
        </plugins>
    </build>

</project>

Am I understanding BOM concept wrong? Why are there no dependencies, even though they are declared in pom.xml? How can I run this code without parent but with BOM?

2

There are 2 best solutions below

0
Dmytro Chasovskyi On BEST ANSWER

The issue I had, was related to a misconfiguration in pom.xml

Instead of

<sourceDirectory>${project.basedir}</sourceDirectory>

I have to use

<sourceDirectory>${project.basedir}/src/main</sourceDirectory>

This said, it is possible to package differently from pom packaging, so I can use jar or other alternatives.

0
Michael Katt On

Importing the BOM via dependencyManagement and scope import copies their dependencyManagement configuration into your current dependencyManagement, as you would have written these dependencyManagement configuration.

The section dependencyManagement should only be used within POM of packaging pom. The resulting pom artefact could be used as parent of other POM.

Therefore you should not use BOM without parent.

Have a closer look at https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html