首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Gradle中获取依赖型版本父项目

如何在Gradle中获取依赖型版本父项目
EN

Stack Overflow用户
提问于 2020-06-15 09:08:55
回答 1查看 1.9K关注 0票数 1

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

错误信息是

任务执行失败:子项目:

‘>无法解析配置的所有文件“子项目:编译compileClasspath”.>找不到org.springframework.boot:spring-boot-starter:.要求:项目:父-项目

这是父项目build.gradle文件:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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'
  }
}

请帮忙,谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-15 11:30:59

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

由于应用插件在默认情况下也会禁用普通jar任务,因此可以创建一个bootJar,因此需要对库进行更改:

代码语言:javascript
复制
// 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导入为一个平台:

代码语言:javascript
复制
// 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'中使用。您可能只是从某个地方复制/粘贴它,但是您应该用implementation替换它。)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62384903

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档