Spring 如何在Gradle中从父项目获取依赖项版本

Spring 如何在Gradle中从父项目获取依赖项版本,spring,gradle,Spring,Gradle,我有一个Gradle项目,我想创建一个子模块,但我在构建项目时失败了 错误消息是 这是父项目build.gradle文件: plugins { id 'org.springframework.boot' version '2.3.0.RELEASE' id 'io.spring.dependency-management' version '1.0.9.RELEASE' id 'java' } allprojects { apply plugin: 'java' group

我有一个Gradle项目,我想创建一个子模块,但我在构建项目时失败了

错误消息是

这是父项目build.gradle文件:

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

allprojects {
  apply plugin: 'java'
  group = 'com.test'

  sourceCompatibility = JavaVersion.VERSION_11
  targetCompatibility = JavaVersion.VERSION_11

  repositories {
    //local nexus
  }
}

dependencies {
  implementation 'org.springframework.boot:spring-boot-starter'
  implementation 'org.springframework.boot:spring-boot-starter-actuator'
  implementation 'org.springframework.boot:spring-boot-starter-web'
  //other dependencies
}
这是子项目build.gradle:

plugins {
  id 'java'
}

group = 'com.test'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_11

dependencies {
  compile 'org.springframework.boot:spring-boot-starter'

  testImplementation('org.springframework.boot:spring-boot-starter-test') {
    exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
  }
}

请提供帮助,提前感谢。

为了能够在没有版本的情况下指定Spring引导依赖项,您需要将Spring引导插件应用于所有模块。现在,您只在父项目中有它,但在子项目中没有

由于应用插件在默认情况下也会禁用普通jar任务,因此您需要为库更改以下内容:

// Child build file
plugins {
    // Note that there are no versions on the plugins in the child project as this is defined by the ones in the parent
    id 'org.springframework.boot'
    id 'io.spring.dependency-management'
}

bootJar {
  enabled = false
}

jar {
  enabled = true
}

dependencies {
  implementation 'org.springframework.boot:spring-boot-starter'
}
或者,您也可以放弃
io.spring.dependency management
插件(以及子项目中的
org.springframework.boot
插件),而将spring boot BOM作为平台导入:

// Child build file (alternative)
dependencies {
  // Use 'platform' for making the versions in the BOM a recommendation only, and 'enforcedPlatform' for making them a requirement.
  // Note that you need the version of the BOM, so I recommend putting it in a property.
  implementation enforcedPlatform("org.springframework.boot:spring-boot-dependencies:2.3.0.RELEASE")

  // Here you can leave out the version
  implementation 'org.springframework.boot:spring-boot-starter'
}
我通常选择后者,因为这允许我使用正常的Gradle语义。但这主要只是偏好

(对于构建脚本,请注意一点:
compile
配置已被弃用。它在
compile'org.springframework.boot:spring boot starter'
行中使用。您可能只是从某个地方复制/粘贴了它,但应该用
实现
替换它)

// Child build file (alternative)
dependencies {
  // Use 'platform' for making the versions in the BOM a recommendation only, and 'enforcedPlatform' for making them a requirement.
  // Note that you need the version of the BOM, so I recommend putting it in a property.
  implementation enforcedPlatform("org.springframework.boot:spring-boot-dependencies:2.3.0.RELEASE")

  // Here you can leave out the version
  implementation 'org.springframework.boot:spring-boot-starter'
}