gradle 无法应用插件 [id 'org.springframework.boot']

Doc*_*rDo 5 java ubuntu gradle spring-boot

我正在尝试运行 spring-boot 项目。我的 gradle 有一些问题。

gradle build 工作正常,但我无法运行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
Run Code Online (Sandbox Code Playgroud)

我的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")
}
Run Code Online (Sandbox Code Playgroud)

gradle build 工作正常,没有错误。

Bjø*_*ter 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。

并且,不要在依赖项中compile使用。implementation前者已被弃用(包括testCompile)。