Gradle构建失败,应用插件[id 'org.springframework.boot']失败。

5

我想运行一个spring-boot项目。但是在使用gradle时遇到了一些问题。

gradle的构建工作正常,但我无法运行gradlew

无法运行命令: ./gradlew build &&java -jar build/libs/gs-spring-boot-docker-0.1.0.jar

以下是错误信息:

Failed to apply plugin [id 'org.springframework.boot']
Spring Boot plugin requires Gradle 4.10 or later. The current version is Gradle 4.9

我的Gradle版本是6.0

我的Gradle文件

buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.2.4.RELEASE")
        classpath('com.google.cloud.tools.jib:com.google.cloud.tools.jib.gradle.plugin:1.8.0')
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'com.google.cloud.tools.jib'


bootJar {
    baseName = 'gs-spring-boot-docker'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    implementation('org.springframework.boot:spring-boot-starter-data-jpa')
    implementation('org.springframework.boot:spring-boot-starter-web')
    implementation('org.postgresql:postgresql')
    testCompile("org.springframework.boot:spring-boot-starter-test")
}

Gradle构建正常,没有错误。


该项目是否使用gradle-wrapper?如果是,那么gradle-wrapper是否使用gradle-version 4.9? - Turing85
@Turing85 我不知道如何检查它。我尝试了 $ gradle wrapper --version 它显示 gradle 6 - DoctorDo
进入项目的根目录,执行 ./gradlew --version(Windows 上为 gradlew.bat --version)。 - Turing85
1个回答

4
Gradle包装器的整个用途是在项目中使用固定版本的Gradle,这可以确保您不会意外使用与项目支持的版本不兼容的版本。另一个好处是,如果您还没有正确的版本,它会自动下载正确的版本。
当您键入gradle(没有'w')时,您正在调用手动下载的分发版,该分发版放置在您的路径上。这将完全跳过包装器部分。在您的情况下,您显然已经下载了版本6并更新了要使用该版本的项目。
但是,您没有更新包装器脚本,而您应该做的是更新包装器脚本。如果您查看gradle/wrapper/gradle-wrapper.properties,您应该会看到它设置为4.9,这不再与您的项目兼容。
要更新它,您需要运行以下命令两次:
gradlew wrapper --gradle-version 6.1.1 --distribution-type all (假设您想要6.1.1版本,在撰写此文时为最新版本)
第一次运行它,它基本上只是更改gradle-wrapper.properties中的版本(例如更改为6.1.1)。如果这失败,因为包装程序与项目相比太旧,请使用文本编辑器手动更改文件。
第二次运行它时,Gradle将使用该新版本(例如6.1.1)启动,并在需要时更新包装器脚本本身。
此外,在开发期间,如果您想要启动Spring Boot应用程序,请只运行gradlew bootRun。无需手动构建jar并调用java。
还有,在您的依赖项中使用implementation而不是compile。前者已被弃用(包括testCompile)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接