步骤概述
把大象装进冰箱分三步:
第一,打开冰箱 下载源码,安装idea、git、JDK21、JDK17
第二,把大象放进去 配置相关环境
第三,关上冰箱 运行项目
配置环境的1000个步骤
新版idea 2023.2版本
安装JDK21,配置环境变量
下载源码,可以下载zip或者ssh拉取
https://github.com/spring-projects/spring-framework
下载gradle,配置仓库,配置系统环境变量
下载spring 6.1.1的对应版本 gradle 8.4,下载编译后的包就好
下载好了,在本地的gradle文件夹的init.d目录下,新建文件init.gradle
(代码引用Gradle 学习 ----Gradle 入门_init.gradle_不穿铠甲的穿山甲的博客-CSDN博客,感谢原作者)
allprojects {
repositories {
mavenLocal()
maven { name "Alibaba" ; url "https://maven.aliyun.com/repository/public" }
maven { name "Bstek" ; url "https://nexus.bsdn.org/content/groups/public/" }
mavenCentral()
}
buildscript {
repositories {
maven { name "Alibaba" ; url 'https://maven.aliyun.com/repository/public' }
maven { name "Bstek" ; url 'https://nexus.bsdn.org/content/groups/public/' }
maven { name "M2" ; url 'https://plugins.gradle.org/m2/' }
}
}
}
下载idea插件:kotlin、aspectJ
编译gradle Test包
在spring framework源码文件夹下,打开git bash面板,输入
./gradlew :spring-oxm:compileTestJava
可以看执行过程中遇到哪些报错,如果是下载包失败,可以手动下载安装好了再尝试。
如我遇到了下载超时,可以看到是下载log4j包超时了,可以检查下gradle的仓库地址是否改为国内了。建议,可以搭个梯子。因为像jdk超时还好,可以自己安装。但下载包超时,哪怕你下载到了源包,也很难放到应该存储的路径下。
$ ./gradlew :spring-oxm:compileTestJava
Starting a Gradle Daemon, 1 incompatible Daemon could not be reused, use --status for details
> Task :spring-jcl:compileJava FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':spring-jcl:compileJava'.
> Could not resolve all files for configuration ':spring-jcl:compileClasspath'.
> Could not download log4j-api-2.21.1.jar (org.apache.logging.log4j:log4j-api:2.21.1)
> Could not get resource 'https://repo.maven.apache.org/maven2/org/apache/logging/log4j/log4j-api/2.21.1/log4j-api-2.21.1.jar'.
> Read timed out
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Get more help at https://help.gradle.org.
Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
For more on this, please refer to https://docs.gradle.org/8.4/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
BUILD FAILED in 6m 27s
17 actionable tasks: 15 executed, 1 from cache, 1 up-to-date
错误2
编译成功!!!
大家需要多点耐心,我搞了一下午终于编译成功了
设置idea环境
设置gradle的JDK为21版
设置项目JDK为17版
Sources、Paths、Dependencies的JDK版本都检查一下
修改项目gradle仓库为阿里云
maven { url "https://maven.aliyun.com/repository/google" }
刷新项目的gradle包
在build.gradle文件刷新引用(如果文件页面找不到图标,可以在右侧gradle窗口,点刷新)
然后可以看到引用刷新成功了,右侧gradle窗口也显示出了项目,但还有报错
刷新引用报错Execution failed for task ':myTestMoudle:checkstyleMain'.
那就把报错的task,放到git里单独执行。打开项目所在文件的git窗口,执行命令
./gradlew :myTestMoudle:checkstyleMain
如果报错,这里也可以看到详细的报错内容,再针对性解决。
下载包失败
如果遇到了某个包拉取不到的情况,可以手动下载并导入到项目。也有修改gradle仓库的方法,但没必要。我们的目的是阅读源码,无需在这些细节浪费时间。