java-How to solve 'plugin org.springframework.boot was not found in any of the following sources exception' when building springboot applications with gradle

Problem

When we build spring boot applications with gradle, sometimes we get this error :

Build file '/Users/bswen/private/bw/bswen-github/bswen-project/spring-boot-23/build.gradle' line: 2

Plugin [id: 'org.springframework.boot', version: '2.3.2.RELEASE'] was not found in any of the following sources:


* What went wrong:
Plugin [id: 'org.springframework.boot', version: '2.3.2.RELEASE'] was not found in any of the following sources:

- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'org.springframework.boot:org.springframework.boot.gradle.plugin:2.3.2.RELEASE')
  Searched in the following repositories:
    Gradle Central Plugin Repository

Environments

The environments are as follows:

  • java version: jdk 1.8
  • spring boot version: 2.3.2.RELEASE

The build.gradle

The build.gradle of this spring boot application is as follows:

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

group 'com.bswen'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenLocal()
    maven { url 'https://maven.aliyun.com/repository/google/' }
    maven { url 'https://maven.aliyun.com/repository/public/' }
    maven { url 'https://maven.aliyun.com/repository/spring/' }
    maven { url 'https://maven.aliyun.com/repository/gradle-plugin/' }
    maven { url 'https://maven.aliyun.com/repository/spring-plugin/' }

    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

How to solve the problem

This error is caused by the incorrectly configured build.gradle in the project. You should add buildscript block to the build.gradle, this buildscript block is required by the build.gradle script own.

According to this post (by Peter Niederwieser):

The buildScript block determines which plugins, task classes, and other classes are available for use in the rest of the build script. Without a buildScript block, you can use everything that ships with Gradle out-of-the-box. If you additionally want to use third-party plugins, task classes, or other classes (in the build script!), you have to specify the corresponding dependencies in the buildScript block

You must use a buildScript block because Gradle needs this information in order to understand the rest of the build script. That’s why you have to provide this information in a separate channel (the buildScript block). Technically speaking, Gradle needs this information in order to compile and evaluate the rest of the build script. What happens under the hood is that Gradle separates the build script into two scripts (the buildScript block and everything else) so that it can process them separately

and this comment by Ashish:

  • The global level dependencies and repositories sections list dependencies that required for building your source and running your source etc.
  • The buildscript is for the build.gradle file itself. So, this would contain dependencies for say creating RPMs, Dockerfile, and any other dependencies for running the tasks in all the dependent build.gradle

please do as follows:

open the build.gradle ,add the lines:

buildscript {
    repositories {
        mavenLocal()
        maven { url 'https://maven.aliyun.com/repository/google/' }
        maven { url 'https://maven.aliyun.com/repository/public/' }
        maven { url 'https://maven.aliyun.com/repository/spring/' }
        maven { url 'https://maven.aliyun.com/repository/gradle-plugin/' }
        maven { url 'https://maven.aliyun.com/repository/spring-plugin/' }

        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.3.2.RELEASE")
    }
} 

After this change, the whole build.gradle file is as follows:

buildscript {
    repositories {
        mavenLocal()
        maven { url 'https://maven.aliyun.com/repository/google/' }
        maven { url 'https://maven.aliyun.com/repository/public/' }
        maven { url 'https://maven.aliyun.com/repository/spring/' }
        maven { url 'https://maven.aliyun.com/repository/gradle-plugin/' }
        maven { url 'https://maven.aliyun.com/repository/spring-plugin/' }

        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.3.2.RELEASE")
    }
}

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

group 'com.bswen'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenLocal()
    maven { url 'https://maven.aliyun.com/repository/google/' }
    maven { url 'https://maven.aliyun.com/repository/public/' }
    maven { url 'https://maven.aliyun.com/repository/spring/' }
    maven { url 'https://maven.aliyun.com/repository/gradle-plugin/' }
    maven { url 'https://maven.aliyun.com/repository/spring-plugin/' }

    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

The code base of this example is uploaded to github.com, you can click this web url to download the code.

References

You can view some references as follows: