【问题标题】:Spring boot failing to resolve spring-boot-dependencies using gradle kotlin dslSpring Boot 无法使用 gradle kotlin dsl 解决 spring-boot-dependencies
【发布时间】:2019-07-29 19:13:03
【问题描述】:

build.gradle.kts 文件如下所示:

buildscript {
    val springBootVersion by extra("2.1.3.RELEASE")

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion")
    }
}

plugins {
    java
    id("org.springframework.boot") version "2.1.3.RELEASE"
}

apply(plugin = "io.spring.dependency-management")

错误信息是这样的:

* What went wrong:
Execution failed for task ':compileJava'.
> Could not resolve all dependencies for configuration ':detachedConfiguration1'.
> Cannot resolve external dependency org.springframework.boot:spring-boot-dependencies:2.1.3.RELEASE because no repositories are defined.
 Required by:
     project :

gradle 版本是:

$ ./gradlew --version

------------------------------------------------------------
Gradle 5.2.1
------------------------------------------------------------

Build time:   2019-02-08 19:00:10 UTC
Revision:     f02764e074c32ee8851a4e1877dd1fea8ffb7183

Kotlin DSL:   1.1.3
Kotlin:       1.3.20
Groovy:       2.5.4
Ant:          Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM:          1.8.0_131 (Oracle Corporation 25.131-b11)
OS:           Linux 4.18.7-100.fc27.x86_64 amd64

groovy dsl 似乎工作正常。任何帮助将不胜感激。

【问题讨论】:

    标签: spring-boot gradle gradle-kotlin-dsl kotlin-dsl


    【解决方案1】:

    删除无用的buildscript部分,并指定构建内部的存储库:

    plugins {
        java
        id("org.springframework.boot") version "2.1.3.RELEASE"
    }
    
    apply(plugin = "io.spring.dependency-management")
    
    repositories {
        mavenCentral()
    }
    

    【讨论】:

      【解决方案2】:

      我必须在 allProjects 部分添加 mavenCentral:

      allprojects {
          repositories {
              mavenCentral()
          }
      }
      

      This post 关于 buildscript vs allprojects 可以帮助澄清。

      【讨论】: