来自spring的Gradle dependencyManagement,dependency-management不起作用

xzv2uavs  于 2023-09-29  发布在  Spring
关注(0)|答案(1)|浏览(197)

我有一个多模块项目,根目录中有这样的build.gradle:

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'
    }
    }

我的子模块是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'
    }

不幸的是,我得到了'Execution failed for task ':aws-utils:compileJava '。
未能解析配置“:aws-utils:compileClasspath”的所有文件。找不到组织。projectlombok:lombok:。要求者:project:aws-utils Could not find org.springframework. Boot :spring-boot-starter-web:.要求者:project:aws-utils Could not find org.springframework. Boot :spring-boot-starter:.要求者:项目:aws-utils我尝试了clean build,也使用了--refresh-dependencies`

3yhwsihp

3yhwsihp1#

为了使io.spring.dependency-management插件工作,您需要将其应用于所有项目(或所有子项目,取决于您所需的设置)。为了实现这一点,请相应地更改您的根gradle.build文件:

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'
        }
    }
}

请记住,如果你使用org.springframework.boot插件,定义spring Boot starter项目版本是多余的。为了进一步简化你的root build.gradle文件,你可以像这样应用org.springframework.boot插件:

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'
        }
    }
}

请注意,dependencyManagement部分中与spring starter相关的依赖声明已被删除。
如果您计划在子模块中定义所有依赖项,并在项目设置中再保留一个根,则可以将与apply plugin相关的部分和整个dependencyManagement clojure移动到subprojects {}

相关问题