How to resolve 'plugin org.springframework.boot was not found in any of the following sources exception' when building spring boot 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 resolve 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.
According to this post (by Peter Niederwieser):
and this comment by Ashish:
So 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 the above 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'}
More about BuildScript
The buildscript block in a build.gradle file is used to configure the build script itself. It allows you to define dependencies and repositories that are needed for the build script to run. This is different from the dependencies and repositories that are needed for the project being built.
Here is an example of a buildscript block:
buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:4.1.0' }}
In this example:
- The repositories block specifies where to find the dependencies needed for the build script. In this case, it is using Maven Central.
- The dependencies block specifies the dependencies needed for the build script. Here, it includes the Android Gradle plugin.
This configuration is necessary for setting up the environment in which the build script will execute.
The code base of this example is uploaded to github.com
, you can click this web url to download the code.
Final Words + More Resources
My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!