springgradledependenciesdependency-management

Gradle dependencyManagement from spring.dependency-management is not working


I have a multimodule project, with such build.gradle in the root:

    plugins {
    id "io.spring.dependency-management" version "1.1.3"
    id "org.springframework.boot" version "3.1.3" apply false
    }

    allprojects {
    apply plugin: 'java'
    group 'com.epam.esm'
    sourceCompatibility = '17'
    targetCompatibility = '17'
    repositories {
        mavenCentral()
    }
    }

    dependencyManagement {
        dependencies {
        dependency 'org.projectlombok:lombok:1.18.26'
        dependency 'org.springframework.boot:spring-boot-starter-web:3.1.3'
        dependency 'org.springframework.boot:spring-boot-starter:3.1.3'
        dependency 'com.fasterxml.jackson.core:jackson-databind:2.14.2'
        dependency 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.14.2'
        dependency 'org.mapstruct:mapstruct-processor:1.5.3.Final'
        dependency 'org.mapstruct:mapstruct:1.5.3.Final'
        dependency 'org.zalando:problem-spring-web:0.27.0'
        dependency 'com.amazonaws:aws-java-sdk-s3:1.12.6'
    }
    imports {
        mavenBom 'org.springframework.cloud:spring-cloud-dependencies:2022.0.3'
    }
    }

And my submodule's build.gradle:

    dependencies {
    implementation 'org.projectlombok:lombok'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'com.fasterxml.jackson.core:jackson-databind'
    implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310'
    implementation 'org.mapstruct:mapstruct-processor'
    implementation 'org.mapstruct:mapstruct'
    implementation 'org.zalando:problem-spring-web'
    implementation 'com.amazonaws:aws-java-sdk-s3'
    }

Unfortunately I got `Execution failed for task ':aws-utils:compileJava'.

Could not resolve all files for configuration ':aws-utils:compileClasspath'. Could not find org.projectlombok:lombok:. Required by: project :aws-utils Could not find org.springframework.boot:spring-boot-starter-web:. Required by: project :aws-utils Could not find org.springframework.boot:spring-boot-starter:. Required by: project :aws-utils`

I tried clean build, also with --refresh-dependencies


Solution

  • In order to make io.spring.dependency-management plugin work, you need to apply it to all your projects (or all your subprojects, depending on your desired setup). In order to achieve that, change your root gradle.build file accordingly:

    plugins {
        id "io.spring.dependency-management" version "1.1.3"
        id "org.springframework.boot" version "3.1.3" apply false
    }
    
    allprojects {
        apply plugin: 'io.spring.dependency-management'
        apply plugin: 'java'
        group 'com.epam.esm'
        sourceCompatibility = '17'
        targetCompatibility = '17'
        repositories {
            mavenCentral()
        }
        dependencyManagement {
            dependencies {
                dependency 'org.projectlombok:lombok:1.18.26'
                dependency 'com.fasterxml.jackson.core:jackson-databind:2.14.2'
                dependency 'org.springframework.boot:spring-boot-starter-web:3.1.3'
                dependency 'org.springframework.boot:spring-boot-starter:3.1.3'
                dependency 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.14.2'
                dependency 'org.mapstruct:mapstruct-processor:1.5.3.Final'
                dependency 'org.mapstruct:mapstruct:1.5.3.Final'
                dependency 'org.zalando:problem-spring-web:0.27.0'
                dependency 'com.amazonaws:aws-java-sdk-s3:1.12.6'
            }
            imports {
                mavenBom 'org.springframework.cloud:spring-cloud-dependencies:2022.0.3'
            }
        }
    }
    

    Bear in mind that if you use org.springframework.boot plugin, defining spring boot starter project versions is redundant. To further simplify your root build.gradle file, you can apply org.springframework.boot plugin as well like that:

    plugins {
        id "io.spring.dependency-management" version "1.1.3"
        id "org.springframework.boot" version "3.1.3" apply false
    }
    
    allprojects {
        apply plugin: 'io.spring.dependency-management'
        apply plugin: 'org.springframework.boot'
        apply plugin: 'java'
        group 'com.epam.esm'
        sourceCompatibility = '17'
        targetCompatibility = '17'
        repositories {
            mavenCentral()
        }
        dependencyManagement {
            dependencies {
                dependency 'org.projectlombok:lombok:1.18.26'
                dependency 'com.fasterxml.jackson.core:jackson-databind:2.14.2'
                dependency 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.14.2'
                dependency 'org.mapstruct:mapstruct-processor:1.5.3.Final'
                dependency 'org.mapstruct:mapstruct:1.5.3.Final'
                dependency 'org.zalando:problem-spring-web:0.27.0'
                dependency 'com.amazonaws:aws-java-sdk-s3:1.12.6'
            }
            imports {
                mavenBom 'org.springframework.cloud:spring-cloud-dependencies:2022.0.3'
            }
        }
    }
    

    Note that the dependency declarations related to spring starters in dependencyManagement section were removed.

    If you plan to define all your dependencies in submodules and keep the root one more for the project setup, you can move sections related to apply plugin and the whole dependencyManagement clojure to subprojects {}