前言
本文記錄了 Spring 源碼環境的搭建方式,以及踩過的那些坑!當前版本:5.3.2-SNAPSHOT。
環境准備
- Git
- JDK
- master 分支需要 JDK 11
- 5.2.x 分支, JDK8 即可
- Gradle 6.5.1
- IDEA 最新 (2020.2.3)
Spring 源碼倉庫地址:https://github.com/spring-projects/spring-framework
下載源碼
- clone 源碼
git clone https://github.com/spring-projects/spring-framework.git
-
使用 IDEA 打開
- 等待 IDEA 加載完成即可。
注: 也可以指定 clone 的分支
git clone -b 5.2.x https://github.com/spring-projects/spring-framework.git
或者先 fork 到自己的倉庫,然后再 clone。
這里我是 fork 到我的倉庫,然后再 clone 的。
當前 master 分支代表的版本為 5.3.2-SNAPSHOT。
執行測試
- 在項目右鍵創建
module
- 選擇
Gradle
Java
- 創建 module
- 在 build.gradle 中添加配置
compile(project(":spring-context"))
- 創建測試類並測試
其中 UserComponent
添加了 @Component
注解, 程序正常執行則一切 OK。可以開始愉快的調試代碼了。
問題總結
編譯失敗
有小伙伴直接下載 zip 包,可能遇到以下問題:(非常不建議直接下載 zip 包構建,想知道原因可以繼續看,最后我也沒有構建成功,而是直接通過 clone 構建的。)
- 報錯如下:
fatal: not a git repository (or any of the parent directories): .git
BUILD SUCCESSFUL in 14s
Build scan background action failed.
org.gradle.process.internal.ExecException: Process 'command 'git'' finished with non-zero exit value 128
... 其他省略
看意思是沒有 git 配置,那就添加上吧!
- 這時候想着添加 git
VCS
-> Enable Version Control Integration...
-> 右上角 Reload All Gradle Projects
依然報錯
fatal: Needed a single revision
- 查詢問題
issues 地址:https://github.com/spring-projects/spring-framework/issues/24467
建議使用
$ git clone git@github.com:spring-projects/spring-framework.git
意思就是 zip 發行版主要是用來共享源代碼,但不一定用於構建它。
- 最后我選擇了使用 clone 的方式,直接 clone 下來,然后 build 通過。
缺少 cglib 和 objenesis 包
Kotlin: warnings found and -Weeror specified
沒有 spring-cglib-repack
和 spring-objenesis-repack
包
執行這兩個即可。
找不到包 jdk.jfr
import jdk.jfr.Category;
import jdk.jfr.Description;
import jdk.jfr.Event;
import jdk.jfr.Label;
JDK 升級為 11。因為我本地使用的是 JDK8,發現報錯,jfr 包需要升級 JDK 11 才有。
如果不生效,可以通過:
IDEA -> File
-> Project Structure
-> Project
檢查下是否修改為 JDK 11
快捷鍵:⌘ + ;
相關資料
- Spring 倉庫:https://github.com/spring-projects/spring-framework
- Spring 構建文檔:https://github.com/spring-projects/spring-framework/wiki/Build-from-Source