背景:为了构建Spring源码阅读环境,网上查询了各种姿势,gradle版本从4.9试到7.5,idea版本从2022.3.2试到2020.1版本,最终构建成功.
本次构建成功的版本为5.2.6和5.2.8
- 环境
在gradle的init.d目录下的创建init.gradle,内容如下:
gradle.projectsLoaded {
rootProject.allprojects {
buildscript {
repositories {
def JCENTER_URL = 'https://maven.aliyun.com/repository/jcenter'
def GOOGLE_URL = 'https://maven.aliyun.com/repository/google'
def NEXUS_URL = 'http://maven.aliyun.com/nexus/content/repositories/jcenter'
all { ArtifactRepository repo ->
if (repo instanceof MavenArtifactRepository) {
def url = repo.url.toString()
if (url.startsWith('https://jcenter.bintray.com/')) {
project.logger.lifecycle "Repository ${repo.url} replaced by $JCENTER_URL."
println("buildscript ${repo.url} replaced by $JCENTER_URL.")
remove repo
}
else if (url.startsWith('https://dl.google.com/dl/android/maven2/')) {
project.logger.lifecycle "Repository ${repo.url} replaced by $GOOGLE_URL."
println("buildscript ${repo.url} replaced by $GOOGLE_URL.")
remove repo
}
else if (url.startsWith('https://repo1.maven.org/maven2')) {
project.logger.lifecycle "Repository ${repo.url} replaced by $REPOSITORY_URL."
println("buildscript ${repo.url} replaced by $REPOSITORY_URL.")
remove repo
}
}
}
jcenter {
url JCENTER_URL
}
google {
url GOOGLE_URL
}
maven {
url NEXUS_URL
}
}
}
repositories {
def JCENTER_URL = 'https://maven.aliyun.com/repository/jcenter'
def GOOGLE_URL = 'https://maven.aliyun.com/repository/google'
def NEXUS_URL = 'http://maven.aliyun.com/nexus/content/repositories/jcenter'
all { ArtifactRepository repo ->
if (repo instanceof MavenArtifactRepository) {
def url = repo.url.toString()
if (url.startsWith('https://jcenter.bintray.com/')) {
project.logger.lifecycle "Repository ${repo.url} replaced by $JCENTER_URL."
println("buildscript ${repo.url} replaced by $JCENTER_URL.")
remove repo
}
else if (url.startsWith('https://dl.google.com/dl/android/maven2/')) {
project.logger.lifecycle "Repository ${repo.url} replaced by $GOOGLE_URL."
println("buildscript ${repo.url} replaced by $GOOGLE_URL.")
remove repo
}
else if (url.startsWith('https://repo1.maven.org/maven2')) {
project.logger.lifecycle "Repository ${repo.url} replaced by $REPOSITORY_URL."
println("buildscript ${repo.url} replaced by $REPOSITORY_URL.")
remove repo
}
}
}
jcenter {
url JCENTER_URL
}
google {
url GOOGLE_URL
}
maven {
url NEXUS_URL
}
}
}
}
- 在GitHub下载Spring源码压缩包ZIP并解压,Spring版本:5.2.8或者5.2.6
- 把源码包进行解压,解压之后切勿立马导入idea,先进行文件修改
- 修改 build.gradle
maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter' }
- 修改 gradle.properties
version=5.2.6RELEASE
## Gradle编译时,会下载很多东西,占用内存较大,建议适当调大
org.gradle.jvmargs=-Xmx2048M
## 开启Gradle缓存
org.gradle.caching=true
## 开启Gradle并行编译
org.gradle.parallel=true
## 开启Gradle守护进程模式
org.gradle.daemon=true
# Gradle 5.x就不用配置了 6.x配置一下
# kotlin.stdlib.default.dependency=false
- 修改 settings.gradle
maven { url "https://maven.aliyun.com/repository/public"}
- 打开 idea,设置gradle 和kotiln,千万别导入源码、千万别导入源码、千万别导入源码
如果5.2.8和5.2.6版本如果使用kotiln版本1.3.72构建出错的可以尝试将其升级为1.4版本,本人多次失败之后,将其升级到1.4就构建成功了,此处最关键
8. 导入源码
10.等待下包构建完成之后就结束了
总结一下:最折腾的地方在于gradle、idea、kotiln的版本关系,同样设置配置,但在不同版本的idea中就有不同的结果。千奇百怪。。。。。
最终附上两个版本成功之后图