如何从 Gradle 多模块构建中的父配置中获取 Spring Cloud Config 依赖项以解析?

How to get Spring Cloud Config dependencies to resolve from parent configuration in Gradle multi-module build?

为了使用 Vault 为 Spring Cloud Config 服务器和(Java 和非 Java)客户端组合一个示例项目,我决定采用多模块 Gradle 构建。由于我已经有两个工作 Java 项目(服务器和客户端),我想我可以将通用配置拉到父 build.gradle 并有条件地应用 Java 子项目的配置。我从服务器子项目开始,运行 遇到了一个我一直无法解决的问题。

父模块的 build.gradle 如下所示:

plugins {
  id 'org.springframework.boot' version '2.3.0.RELEASE' apply false
  id 'io.spring.dependency-management' version '1.0.9.RELEASE' apply false
  id 'idea'
}

ext {
   set('springCloudVersion', "Hoxton.SR4")
}

allprojects {
  repositories {
    mavenCentral()
  }

  version = '0.0.1-SNAPSHOT'
}


subprojects {
  // We'll need dependencies for Docker and Docker compose

  afterEvaluate { project ->
    if (project.usesSpringBoot) {
      println("Configuring ${springCloudVersion}")
      configure(project) {
        apply plugin: 'org.springframework.boot'
        apply plugin: 'io.spring.dependency-management'
        apply plugin: 'java'

        group = 'com.daecabhir'
        sourceCompatibility = '11'

        dependencies {
          implementation 'org.springframework.boot:spring-boot-starter-actuator'
          implementation 'org.springframework.boot:spring-boot-starter-web'
          implementation 'org.springframework.cloud:spring-cloud-starter-config'
          developmentOnly 'org.springframework.boot:spring-boot-devtools'
          testImplementation('org.springframework.boot:spring-boot-starter-test') {
            exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
          }
        }

        dependencyManagement {
          imports {
            mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
          }
        }

        test {
          useJUnitPlatform()
        }
      }
    }
  }
}

cloud-config-server 子项目的 build.gradle 看起来像:

ext {
  usesSpringBoot = true
}

不幸的是,尝试 ./gradlew build 给我留下了一个错误,它无法从 Spring Cloud Config 中找到 class:

/home/gwright/projects/cloud-config-vault-example/cloud-config-server/src/main/java/com/daecabhir/cloudconfigserver/CloudConfigServerApplication.java:5: error: package org.springframework.cloud.config.server does not exist
import org.springframework.cloud.config.server.EnableConfigServer;
                                              ^
/home/gwright/projects/cloud-config-vault-example/cloud-config-server/src/main/java/com/daecabhir/cloudconfigserver/CloudConfigServerApplication.java:7: error: cannot find symbol
@EnableConfigServer
 ^
  symbol: class EnableConfigServer
2 errors

当我检查依赖项时,我在编译时得到以下信息:

compileClasspath - Compile classpath for source set 'main'.
+--- org.springframework.boot:spring-boot-starter-actuator -> 2.3.0.RELEASE
|    +--- org.springframework.boot:spring-boot-dependencies:2.3.0.RELEASE
|    |    +--- com.fasterxml.jackson.core:jackson-databind:2.11.0 (c)
|    |    +--- io.micrometer:micrometer-core:1.5.1 (c)
|    |    +--- org.springframework:spring-web:5.2.6.RELEASE (c)
|    |    +--- org.springframework:spring-webmvc:5.2.6.RELEASE (c)
|    |    +--- org.springframework.boot:spring-boot-actuator-autoconfigure:2.3.0.RELEASE (c)
|    |    +--- org.springframework.boot:spring-boot-starter:2.3.0.RELEASE (c)
|    |    +--- org.springframework.boot:spring-boot-starter-actuator:2.3.0.RELEASE (c)
|    |    +--- org.springframework.boot:spring-boot-starter-json:2.3.0.RELEASE (c)
|    |    +--- org.springframework.boot:spring-boot-starter-tomcat:2.3.0.RELEASE (c)
|    |    +--- org.springframework.boot:spring-boot-starter-web:2.3.0.RELEASE (c)
|    |    +--- org.springframework.boot:spring-boot:2.3.0.RELEASE (c)
|    |    +--- org.springframework.boot:spring-boot-autoconfigure:2.3.0.RELEASE (c)
|    |    +--- org.springframework.boot:spring-boot-starter-logging:2.3.0.RELEASE (c)
|    |    +--- jakarta.annotation:jakarta.annotation-api:1.3.5 (c)
|    |    +--- org.springframework:spring-core:5.2.6.RELEASE (c)
|    |    +--- org.yaml:snakeyaml:1.26 (c)
|    |    +--- org.springframework.boot:spring-boot-actuator:2.3.0.RELEASE (c)
|    |    +--- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.11.0 (c)
|    |    +--- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.11.0 (c)
|    |    +--- com.fasterxml.jackson.module:jackson-module-parameter-names:2.11.0 (c)
|    |    +--- org.apache.tomcat.embed:tomcat-embed-core:9.0.35 (c)
|    |    +--- org.glassfish:jakarta.el:3.0.3 (c)
|    |    +--- org.apache.tomcat.embed:tomcat-embed-websocket:9.0.35 (c)
|    |    +--- org.springframework:spring-beans:5.2.6.RELEASE (c)
|    |    +--- org.springframework:spring-aop:5.2.6.RELEASE (c)
|    |    +--- org.springframework:spring-context:5.2.6.RELEASE (c)
|    |    +--- org.springframework:spring-expression:5.2.6.RELEASE (c)
|    |    +--- com.fasterxml.jackson.core:jackson-annotations:2.11.0 (c)
|    |    +--- com.fasterxml.jackson.core:jackson-core:2.11.0 (c)
|    |    +--- ch.qos.logback:logback-classic:1.2.3 (c)
|    |    +--- org.apache.logging.log4j:log4j-to-slf4j:2.13.2 (c)
|    |    +--- org.slf4j:jul-to-slf4j:1.7.30 (c)
|    |    +--- org.springframework:spring-jcl:5.2.6.RELEASE (c)
|    |    +--- org.springframework.security:spring-security-crypto:5.3.2.RELEASE (c)
|    |    +--- ch.qos.logback:logback-core:1.2.3 (c)
|    |    +--- org.slf4j:slf4j-api:1.7.30 (c)
|    |    \--- org.apache.logging.log4j:log4j-api:2.13.2 (c)
|    +--- org.springframework.boot:spring-boot-starter:2.3.0.RELEASE
|    |    +--- org.springframework.boot:spring-boot-dependencies:2.3.0.RELEASE (*)
|    |    +--- org.springframework.boot:spring-boot:2.3.0.RELEASE
|    |    |    +--- org.springframework.boot:spring-boot-dependencies:2.3.0.RELEASE (*)
|    |    |    +--- org.springframework:spring-core -> 5.2.6.RELEASE
|    |    |    |    \--- org.springframework:spring-jcl:5.2.6.RELEASE
|    |    |    \--- org.springframework:spring-context -> 5.2.6.RELEASE
|    |    |         +--- org.springframework:spring-aop:5.2.6.RELEASE
|    |    |         |    +--- org.springframework:spring-beans:5.2.6.RELEASE
|    |    |         |    |    \--- org.springframework:spring-core:5.2.6.RELEASE (*)
|    |    |         |    \--- org.springframework:spring-core:5.2.6.RELEASE (*)
|    |    |         +--- org.springframework:spring-beans:5.2.6.RELEASE (*)
|    |    |         +--- org.springframework:spring-core:5.2.6.RELEASE (*)
|    |    |         \--- org.springframework:spring-expression:5.2.6.RELEASE
|    |    |              \--- org.springframework:spring-core:5.2.6.RELEASE (*)
|    |    +--- org.springframework.boot:spring-boot-autoconfigure:2.3.0.RELEASE
|    |    |    +--- org.springframework.boot:spring-boot:2.3.0.RELEASE (*)
|    |    |    \--- org.springframework.boot:spring-boot-dependencies:2.3.0.RELEASE (*)
|    |    +--- org.springframework.boot:spring-boot-starter-logging:2.3.0.RELEASE
|    |    |    +--- org.springframework.boot:spring-boot-dependencies:2.3.0.RELEASE (*)
|    |    |    +--- ch.qos.logback:logback-classic -> 1.2.3
|    |    |    |    +--- ch.qos.logback:logback-core:1.2.3
|    |    |    |    \--- org.slf4j:slf4j-api:1.7.25 -> 1.7.30
|    |    |    +--- org.apache.logging.log4j:log4j-to-slf4j -> 2.13.2
|    |    |    |    +--- org.slf4j:slf4j-api:1.7.25 -> 1.7.30
|    |    |    |    \--- org.apache.logging.log4j:log4j-api:2.13.2
|    |    |    \--- org.slf4j:jul-to-slf4j -> 1.7.30
|    |    |         \--- org.slf4j:slf4j-api:1.7.30
|    |    +--- jakarta.annotation:jakarta.annotation-api -> 1.3.5
|    |    +--- org.springframework:spring-core -> 5.2.6.RELEASE (*)
|    |    \--- org.yaml:snakeyaml -> 1.26
|    +--- org.springframework.boot:spring-boot-actuator-autoconfigure:2.3.0.RELEASE
|    |    +--- org.springframework.boot:spring-boot-dependencies:2.3.0.RELEASE (*)
|    |    \--- org.springframework.boot:spring-boot-actuator:2.3.0.RELEASE
|    |         \--- org.springframework.boot:spring-boot-dependencies:2.3.0.RELEASE (*)
|    \--- io.micrometer:micrometer-core -> 1.5.1
|         \--- org.hdrhistogram:HdrHistogram:2.1.12
+--- org.springframework.boot:spring-boot-starter-web -> 2.3.0.RELEASE
|    +--- org.springframework.boot:spring-boot-dependencies:2.3.0.RELEASE (*)
|    +--- org.springframework.boot:spring-boot-starter:2.3.0.RELEASE (*)
|    +--- org.springframework.boot:spring-boot-starter-json:2.3.0.RELEASE
|    |    +--- org.springframework.boot:spring-boot-dependencies:2.3.0.RELEASE (*)
|    |    +--- org.springframework.boot:spring-boot-starter:2.3.0.RELEASE (*)
|    |    +--- org.springframework:spring-web -> 5.2.6.RELEASE
|    |    |    +--- org.springframework:spring-beans:5.2.6.RELEASE (*)
|    |    |    \--- org.springframework:spring-core:5.2.6.RELEASE (*)
|    |    +--- com.fasterxml.jackson.core:jackson-databind -> 2.11.0
|    |    |    +--- com.fasterxml.jackson.core:jackson-annotations:2.11.0
|    |    |    \--- com.fasterxml.jackson.core:jackson-core:2.11.0
|    |    +--- com.fasterxml.jackson.datatype:jackson-datatype-jdk8 -> 2.11.0
|    |    |    +--- com.fasterxml.jackson.core:jackson-core:2.11.0
|    |    |    \--- com.fasterxml.jackson.core:jackson-databind:2.11.0 (*)
|    |    +--- com.fasterxml.jackson.datatype:jackson-datatype-jsr310 -> 2.11.0
|    |    |    +--- com.fasterxml.jackson.core:jackson-annotations:2.11.0
|    |    |    +--- com.fasterxml.jackson.core:jackson-core:2.11.0
|    |    |    \--- com.fasterxml.jackson.core:jackson-databind:2.11.0 (*)
|    |    \--- com.fasterxml.jackson.module:jackson-module-parameter-names -> 2.11.0
|    |         +--- com.fasterxml.jackson.core:jackson-core:2.11.0
|    |         \--- com.fasterxml.jackson.core:jackson-databind:2.11.0 (*)
|    +--- org.springframework.boot:spring-boot-starter-tomcat:2.3.0.RELEASE
|    |    +--- org.springframework.boot:spring-boot-dependencies:2.3.0.RELEASE (*)
|    |    +--- jakarta.annotation:jakarta.annotation-api -> 1.3.5
|    |    +--- org.apache.tomcat.embed:tomcat-embed-core -> 9.0.35
|    |    +--- org.glassfish:jakarta.el -> 3.0.3
|    |    \--- org.apache.tomcat.embed:tomcat-embed-websocket -> 9.0.35
|    |         \--- org.apache.tomcat.embed:tomcat-embed-core:9.0.35
|    +--- org.springframework:spring-web -> 5.2.6.RELEASE (*)
|    \--- org.springframework:spring-webmvc -> 5.2.6.RELEASE
|         +--- org.springframework:spring-aop:5.2.6.RELEASE (*)
|         +--- org.springframework:spring-beans:5.2.6.RELEASE (*)
|         +--- org.springframework:spring-context:5.2.6.RELEASE (*)
|         +--- org.springframework:spring-core:5.2.6.RELEASE (*)
|         +--- org.springframework:spring-expression:5.2.6.RELEASE (*)
|         \--- org.springframework:spring-web:5.2.6.RELEASE (*)
\--- org.springframework.cloud:spring-cloud-starter-config -> 2.2.2.RELEASE
     +--- org.springframework.cloud:spring-cloud-starter:2.2.2.RELEASE
     |    +--- org.springframework.boot:spring-boot-starter:2.2.5.RELEASE -> 2.3.0.RELEASE (*)
     |    +--- org.springframework.cloud:spring-cloud-context:2.2.2.RELEASE
     |    |    \--- org.springframework.security:spring-security-crypto:5.2.2.RELEASE -> 5.3.2.RELEASE
     |    +--- org.springframework.cloud:spring-cloud-commons:2.2.2.RELEASE
     |    |    \--- org.springframework.security:spring-security-crypto:5.2.2.RELEASE -> 5.3.2.RELEASE
     |    \--- org.springframework.security:spring-security-rsa:1.0.9.RELEASE
     |         \--- org.bouncycastle:bcpkix-jdk15on:1.64
     |              \--- org.bouncycastle:bcprov-jdk15on:1.64
     +--- org.springframework.cloud:spring-cloud-config-client:2.2.2.RELEASE
     |    +--- org.springframework.boot:spring-boot-autoconfigure:2.2.5.RELEASE -> 2.3.0.RELEASE (*)
     |    +--- org.springframework.cloud:spring-cloud-commons:2.2.2.RELEASE (*)
     |    +--- org.springframework.cloud:spring-cloud-context:2.2.2.RELEASE (*)
     |    +--- org.springframework:spring-web:5.2.4.RELEASE -> 5.2.6.RELEASE (*)
     |    +--- com.fasterxml.jackson.core:jackson-annotations:2.10.2 -> 2.11.0
     |    \--- com.fasterxml.jackson.core:jackson-databind:2.10.2 -> 2.11.0 (*)
     \--- com.fasterxml.jackson.core:jackson-databind:2.10.2 -> 2.11.0 (*)

表面上看起来很合理,但事实并非如此。似乎 Gradle 尊重依赖关系,但依赖管理并未完全解决它们。当我回退到使用以下父项 build.gradle:

cloud-config-server 子项目中完全指定配置时
plugins {
  id 'org.springframework.boot' version '2.3.0.RELEASE' apply false
  id 'io.spring.dependency-management' version '1.0.9.RELEASE' apply false
  id 'idea'
}

ext {
   set('springCloudVersion', "Hoxton.SR4")
}

allprojects {
  repositories {
    mavenCentral()
  }

  version = '0.0.1-SNAPSHOT'
}

和子项目 build.gradle 的:

plugins {
    id 'org.springframework.boot'
    id 'io.spring.dependency-management'
    id 'java'
}

group = 'com.daecabhir'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.cloud:spring-cloud-config-server'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

test {
    useJUnitPlatform()
}

构建工作正常,并且依赖关系得到更充分的“解决”(特别是 spring-cloud-config-server 依赖关系):

compileClasspath - Compile classpath for source set 'main'.
+--- org.springframework.boot:spring-boot-starter-web -> 2.3.0.RELEASE
|    +--- org.springframework.boot:spring-boot-dependencies:2.3.0.RELEASE
|    |    +--- com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.11.0 (c)
|    |    +--- org.springframework:spring-web:5.2.6.RELEASE (c)
|    |    +--- org.springframework:spring-webmvc:5.2.6.RELEASE (c)
|    |    +--- org.springframework.security:spring-security-crypto:5.3.2.RELEASE (c)
|    |    +--- org.springframework.boot:spring-boot-starter:2.3.0.RELEASE (c)
|    |    +--- org.springframework.boot:spring-boot-starter-actuator:2.3.0.RELEASE (c)
|    |    +--- org.springframework.boot:spring-boot-starter-json:2.3.0.RELEASE (c)
|    |    +--- org.springframework.boot:spring-boot-starter-tomcat:2.3.0.RELEASE (c)
|    |    +--- org.springframework.boot:spring-boot-starter-validation:2.3.0.RELEASE (c)
|    |    +--- org.springframework.boot:spring-boot-starter-web:2.3.0.RELEASE (c)
|    |    +--- org.yaml:snakeyaml:1.26 (c)
|    |    +--- org.springframework.boot:spring-boot:2.3.0.RELEASE (c)
|    |    +--- org.springframework.boot:spring-boot-autoconfigure:2.3.0.RELEASE (c)
|    |    +--- org.springframework.boot:spring-boot-starter-logging:2.3.0.RELEASE (c)
|    |    +--- jakarta.annotation:jakarta.annotation-api:1.3.5 (c)
|    |    +--- org.springframework:spring-core:5.2.6.RELEASE (c)
|    |    +--- com.fasterxml.jackson.core:jackson-databind:2.11.0 (c)
|    |    +--- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.11.0 (c)
|    |    +--- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.11.0 (c)
|    |    +--- com.fasterxml.jackson.module:jackson-module-parameter-names:2.11.0 (c)
|    |    +--- org.apache.tomcat.embed:tomcat-embed-core:9.0.35 (c)
|    |    +--- org.glassfish:jakarta.el:3.0.3 (c)
|    |    +--- org.apache.tomcat.embed:tomcat-embed-websocket:9.0.35 (c)
|    |    +--- org.springframework:spring-beans:5.2.6.RELEASE (c)
|    |    +--- org.springframework:spring-aop:5.2.6.RELEASE (c)
|    |    +--- org.springframework:spring-context:5.2.6.RELEASE (c)
|    |    +--- org.springframework:spring-expression:5.2.6.RELEASE (c)
|    |    +--- com.fasterxml.jackson.core:jackson-annotations:2.11.0 (c)
|    |    +--- org.springframework.boot:spring-boot-actuator-autoconfigure:2.3.0.RELEASE (c)
|    |    +--- io.micrometer:micrometer-core:1.5.1 (c)
|    |    +--- org.hibernate.validator:hibernate-validator:6.1.5.Final (c)
|    |    +--- org.slf4j:slf4j-api:1.7.30 (c)
|    |    +--- org.apache.httpcomponents:httpclient:4.5.12 (c)
|    |    +--- org.apache.httpcomponents:httpcore:4.4.13 (c)
|    |    +--- com.fasterxml.jackson.core:jackson-core:2.11.0 (c)
|    |    +--- ch.qos.logback:logback-classic:1.2.3 (c)
|    |    +--- org.apache.logging.log4j:log4j-to-slf4j:2.13.2 (c)
|    |    +--- org.slf4j:jul-to-slf4j:1.7.30 (c)
|    |    +--- org.springframework:spring-jcl:5.2.6.RELEASE (c)
|    |    +--- org.springframework.boot:spring-boot-actuator:2.3.0.RELEASE (c)
|    |    +--- jakarta.validation:jakarta.validation-api:2.0.2 (c)
|    |    +--- org.jboss.logging:jboss-logging:3.4.1.Final (c)
|    |    +--- com.fasterxml:classmate:1.5.1 (c)
|    |    +--- commons-codec:commons-codec:1.14 (c)
|    |    +--- ch.qos.logback:logback-core:1.2.3 (c)
|    |    \--- org.apache.logging.log4j:log4j-api:2.13.2 (c)
|    +--- org.springframework.boot:spring-boot-starter:2.3.0.RELEASE
|    |    +--- org.springframework.boot:spring-boot-dependencies:2.3.0.RELEASE (*)
|    |    +--- org.springframework.boot:spring-boot:2.3.0.RELEASE
|    |    |    +--- org.springframework.boot:spring-boot-dependencies:2.3.0.RELEASE (*)
|    |    |    +--- org.springframework:spring-core -> 5.2.6.RELEASE
|    |    |    |    \--- org.springframework:spring-jcl:5.2.6.RELEASE
|    |    |    \--- org.springframework:spring-context -> 5.2.6.RELEASE
|    |    |         +--- org.springframework:spring-aop:5.2.6.RELEASE
|    |    |         |    +--- org.springframework:spring-beans:5.2.6.RELEASE
|    |    |         |    |    \--- org.springframework:spring-core:5.2.6.RELEASE (*)
|    |    |         |    \--- org.springframework:spring-core:5.2.6.RELEASE (*)
|    |    |         +--- org.springframework:spring-beans:5.2.6.RELEASE (*)
|    |    |         +--- org.springframework:spring-core:5.2.6.RELEASE (*)
|    |    |         \--- org.springframework:spring-expression:5.2.6.RELEASE
|    |    |              \--- org.springframework:spring-core:5.2.6.RELEASE (*)
|    |    +--- org.springframework.boot:spring-boot-autoconfigure:2.3.0.RELEASE
|    |    |    +--- org.springframework.boot:spring-boot:2.3.0.RELEASE (*)
|    |    |    \--- org.springframework.boot:spring-boot-dependencies:2.3.0.RELEASE (*)
|    |    +--- org.springframework.boot:spring-boot-starter-logging:2.3.0.RELEASE
|    |    |    +--- org.springframework.boot:spring-boot-dependencies:2.3.0.RELEASE (*)
|    |    |    +--- ch.qos.logback:logback-classic -> 1.2.3
|    |    |    |    +--- ch.qos.logback:logback-core:1.2.3
|    |    |    |    \--- org.slf4j:slf4j-api:1.7.25 -> 1.7.30
|    |    |    +--- org.apache.logging.log4j:log4j-to-slf4j -> 2.13.2
|    |    |    |    +--- org.slf4j:slf4j-api:1.7.25 -> 1.7.30
|    |    |    |    \--- org.apache.logging.log4j:log4j-api:2.13.2
|    |    |    \--- org.slf4j:jul-to-slf4j -> 1.7.30
|    |    |         \--- org.slf4j:slf4j-api:1.7.30
|    |    +--- jakarta.annotation:jakarta.annotation-api -> 1.3.5
|    |    +--- org.springframework:spring-core -> 5.2.6.RELEASE (*)
|    |    \--- org.yaml:snakeyaml -> 1.26
|    +--- org.springframework.boot:spring-boot-starter-json:2.3.0.RELEASE
|    |    +--- org.springframework.boot:spring-boot-dependencies:2.3.0.RELEASE (*)
|    |    +--- org.springframework.boot:spring-boot-starter:2.3.0.RELEASE (*)
|    |    +--- org.springframework:spring-web -> 5.2.6.RELEASE
|    |    |    +--- org.springframework:spring-beans:5.2.6.RELEASE (*)
|    |    |    \--- org.springframework:spring-core:5.2.6.RELEASE (*)
|    |    +--- com.fasterxml.jackson.core:jackson-databind -> 2.11.0
|    |    |    +--- com.fasterxml.jackson.core:jackson-annotations:2.11.0
|    |    |    \--- com.fasterxml.jackson.core:jackson-core:2.11.0
|    |    +--- com.fasterxml.jackson.datatype:jackson-datatype-jdk8 -> 2.11.0
|    |    |    +--- com.fasterxml.jackson.core:jackson-core:2.11.0
|    |    |    \--- com.fasterxml.jackson.core:jackson-databind:2.11.0 (*)
|    |    +--- com.fasterxml.jackson.datatype:jackson-datatype-jsr310 -> 2.11.0
|    |    |    +--- com.fasterxml.jackson.core:jackson-annotations:2.11.0
|    |    |    +--- com.fasterxml.jackson.core:jackson-core:2.11.0
|    |    |    \--- com.fasterxml.jackson.core:jackson-databind:2.11.0 (*)
|    |    \--- com.fasterxml.jackson.module:jackson-module-parameter-names -> 2.11.0
|    |         +--- com.fasterxml.jackson.core:jackson-core:2.11.0
|    |         \--- com.fasterxml.jackson.core:jackson-databind:2.11.0 (*)
|    +--- org.springframework.boot:spring-boot-starter-tomcat:2.3.0.RELEASE
|    |    +--- org.springframework.boot:spring-boot-dependencies:2.3.0.RELEASE (*)
|    |    +--- jakarta.annotation:jakarta.annotation-api -> 1.3.5
|    |    +--- org.apache.tomcat.embed:tomcat-embed-core -> 9.0.35
|    |    +--- org.glassfish:jakarta.el -> 3.0.3
|    |    \--- org.apache.tomcat.embed:tomcat-embed-websocket -> 9.0.35
|    |         \--- org.apache.tomcat.embed:tomcat-embed-core:9.0.35
|    +--- org.springframework:spring-web -> 5.2.6.RELEASE (*)
|    \--- org.springframework:spring-webmvc -> 5.2.6.RELEASE
|         +--- org.springframework:spring-aop:5.2.6.RELEASE (*)
|         +--- org.springframework:spring-beans:5.2.6.RELEASE (*)
|         +--- org.springframework:spring-context:5.2.6.RELEASE (*)
|         +--- org.springframework:spring-core:5.2.6.RELEASE (*)
|         +--- org.springframework:spring-expression:5.2.6.RELEASE (*)
|         \--- org.springframework:spring-web:5.2.6.RELEASE (*)
\--- org.springframework.cloud:spring-cloud-config-server -> 2.2.2.RELEASE
     +--- org.springframework.cloud:spring-cloud-config-client:2.2.2.RELEASE
     |    +--- org.springframework.boot:spring-boot-autoconfigure:2.2.5.RELEASE -> 2.3.0.RELEASE (*)
     |    +--- org.springframework.cloud:spring-cloud-commons:2.2.2.RELEASE
     |    |    \--- org.springframework.security:spring-security-crypto:5.2.2.RELEASE -> 5.3.2.RELEASE
     |    +--- org.springframework.cloud:spring-cloud-context:2.2.2.RELEASE
     |    |    \--- org.springframework.security:spring-security-crypto:5.2.2.RELEASE -> 5.3.2.RELEASE
     |    +--- org.springframework:spring-web:5.2.4.RELEASE -> 5.2.6.RELEASE (*)
     |    +--- com.fasterxml.jackson.core:jackson-annotations:2.10.2 -> 2.11.0
     |    \--- com.fasterxml.jackson.core:jackson-databind:2.10.2 -> 2.11.0 (*)
     +--- org.springframework.boot:spring-boot-starter-actuator:2.2.5.RELEASE -> 2.3.0.RELEASE
     |    +--- org.springframework.boot:spring-boot-dependencies:2.3.0.RELEASE (*)
     |    +--- org.springframework.boot:spring-boot-starter:2.3.0.RELEASE (*)
     |    +--- org.springframework.boot:spring-boot-actuator-autoconfigure:2.3.0.RELEASE
     |    |    +--- org.springframework.boot:spring-boot-dependencies:2.3.0.RELEASE (*)
     |    |    \--- org.springframework.boot:spring-boot-actuator:2.3.0.RELEASE
     |    |         \--- org.springframework.boot:spring-boot-dependencies:2.3.0.RELEASE (*)
     |    \--- io.micrometer:micrometer-core -> 1.5.1
     |         \--- org.hdrhistogram:HdrHistogram:2.1.12
     +--- org.springframework.boot:spring-boot-starter-web:2.2.5.RELEASE -> 2.3.0.RELEASE (*)
     +--- org.springframework.boot:spring-boot-starter-validation:2.2.5.RELEASE -> 2.3.0.RELEASE
     |    +--- org.springframework.boot:spring-boot-dependencies:2.3.0.RELEASE (*)
     |    +--- org.springframework.boot:spring-boot-starter:2.3.0.RELEASE (*)
     |    +--- org.glassfish:jakarta.el -> 3.0.3
     |    \--- org.hibernate.validator:hibernate-validator -> 6.1.5.Final
     |         +--- jakarta.validation:jakarta.validation-api:2.0.2
     |         +--- org.jboss.logging:jboss-logging:3.3.2.Final -> 3.4.1.Final
     |         \--- com.fasterxml:classmate:1.3.4 -> 1.5.1
     +--- org.springframework.security:spring-security-crypto:5.2.2.RELEASE -> 5.3.2.RELEASE
     +--- org.springframework.security:spring-security-rsa:1.0.9.RELEASE
     |    \--- org.bouncycastle:bcpkix-jdk15on:1.64
     |         \--- org.bouncycastle:bcprov-jdk15on:1.64
     +--- org.eclipse.jgit:org.eclipse.jgit:5.1.3.201810200350-r
     |    +--- com.jcraft:jsch:0.1.54
     |    +--- com.jcraft:jzlib:1.1.1
     |    +--- com.googlecode.javaewah:JavaEWAH:1.1.6
     |    \--- org.slf4j:slf4j-api:1.7.2 -> 1.7.30
     +--- org.eclipse.jgit:org.eclipse.jgit.http.apache:5.1.3.201810200350-r
     |    +--- org.eclipse.jgit:org.eclipse.jgit:5.1.3.201810200350-r (*)
     |    +--- org.apache.httpcomponents:httpclient:4.5.5 -> 4.5.12
     |    |    +--- org.apache.httpcomponents:httpcore:4.4.13
     |    |    \--- commons-codec:commons-codec:1.11 -> 1.14
     |    \--- org.apache.httpcomponents:httpcore:4.4.9 -> 4.4.13
     +--- org.yaml:snakeyaml:1.25 -> 1.26
     \--- com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.10.2 -> 2.11.0
          +--- com.fasterxml.jackson.core:jackson-databind:2.11.0 (*)
          +--- org.yaml:snakeyaml:1.26
          \--- com.fasterxml.jackson.core:jackson-core:2.11.0

所以我的问题是,我需要做什么才能根据属性有选择地指定父项目中的配置,从而完全解决依赖关系?我如何指定导致它不能完全解决依赖关系的配置是什么?

FWIW,项目的完整代码可以在这里找到:

我已经对你的回购进行了 PR:https://github.com/daecabhir/cloud-config-vault-example/pull/3


与其使用 属性 来控制项目是否为 Spring 启动项目(对我不起作用),您可以定义一个项目列表 Spring 启动基于项目,然后应用您已完成的默认设置。

plugins {
  id 'org.springframework.boot' version '2.3.0.RELEASE' apply false
  id 'io.spring.dependency-management' version '1.0.9.RELEASE' apply false
  id 'idea'
}

ext {
   set('springCloudVersion', "Hoxton.SR4")
}

allprojects {
  repositories {
    mavenCentral()
  }

  version = '0.0.1-SNAPSHOT'
}

// List of Spring Boot based projects
def springBootProjects = [
  project(":cloud-config-server"),
  project(":cloud-config-client")
]

获得列表后,只需遍历它们,对它们应用基于的配置:

springBootProjects.each { springBootProject ->
  configure(springBootProject) {
    println("Configuring $name as a Spring Boot project")

    apply {
      plugin("org.springframework.boot")
      plugin("io.spring.dependency-management")
      plugin("java")
    }

    group = "com.daecabhir"
    sourceCompatibility = "11"

    dependencies {
      implementation 'org.springframework.boot:spring-boot-starter-actuator'
      implementation 'org.springframework.boot:spring-boot-starter-web'
      developmentOnly 'org.springframework.boot:spring-boot-devtools'
      testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
      }
    }

    dependencyManagement {
      imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
      }
    }

    tasks {
      test {
        useJUnitPlatform()
      }
    }
  }
}

您将 spring-cloud-starter-config 作为服务器应用程序的导入,但这是不正确的,因为 spring-cloud-starter-config 模块引入了 spring-cloud-config-client,这不是配置服务器所需要的它会导致导入问题。

因此与其将依赖项定义为基本依赖项,不如将其移至项目本身:

cloud-config-server/build.gradle

dependencies {
  implementation "org.springframework.cloud:spring-cloud-config-server"
}

cloud-config-client/build.gradle

dependencies {
  implementation "org.springframework.cloud:spring-cloud-starter-config"
}

进行这些更改后,结果输出为:

$ ./gradlew assemble

> Configure project :
Configuring cloud-config-server as a Spring Boot project
Configuring cloud-config-client as a Spring Boot project

BUILD SUCCESSFUL in 2s
6 actionable tasks: 6 up-to-date