第一、環境、工具準備
- JDK版本:1.8.0_161
- GRADLE版本:4.9
- Spring源碼版本:v5.0.8.RELEASE
- IntelliJ IDEA版本:IntelliJ IDEA 2018.2.1 (Ultimate Edition)
- 系統:WINDOWS 10
第二、下載源碼
下載源碼可以直接到Spring源碼倉庫 下載v5.0.8.RELEASE版本的zip包在本地解壓,也可以通過fork方式把它fork到自己倉庫,方便後續更新維護。
下載到本地解壓如下:
上圖用紅框圈出來的幾個文件要重點關注下:-
- build.gradle 放了一些依賴配置,有點像maven的pom文件
- gradle.properties 存放當前Spring的版本,可以對比下看看是不是5.0.8版本
- gradlew.bat 放的是項目的構建腳本,不過在本章用不上,本章是使用IDEA去構建
- import-into-idea.md 這個是Spring導入IDEA的說明文件,可以根據裏面的步驟去導入
第三、導入IDEA
1.點擊File->Open打開項目
下載的jar包會存放在上圖配置的路徑:
2.修改docs.gradle文件
打開docs.gradle文件
3.註釋掉dokka和asciidoctor兩個配置項:
這兩個任務主要是用來生成文檔使用,對主流程並沒有任何影響
4.替換docs.gradle文件中的task schemaZip配置
在gradle的構建腳本,只針對Linux系統做了適配,需要把task schemaZip
替換成下面代碼,因爲原有代碼的路徑符號“/”
是針對Linux的,windows使用會有問題,在windows環境需要替換成"\\"
,否則會出現如下異常:
* What went wrong:
Failed to capture snapshot of input files for task ':distZip' property 'rootSpec$1$3' during up-to-date check.
> Failed to create MD5 hash for file 'D:\repository\spring-framework-5.0.8.RELEASE\build\distributions\spring-framework-5.0.8.RELEASE-schema.zip'.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1m 43s
254 actionable tasks: 3 executed, 251 up-to-date
參考如下代碼,註釋部分是原有的代碼:
//task schemaZip(type: Zip) {
// group = "Distribution"
// baseName = "spring-framework"
// classifier = "schema"
// description = "Builds -${classifier} archive containing all " +
// "XSDs for deployment at http://springframework.org/schema."
// duplicatesStrategy 'exclude'
// moduleProjects.each { subproject ->
// def Properties schemas = new Properties();
//
// subproject.sourceSets.main.resources.find {
// it.path.endsWith("META-INF/spring.schemas")
// }?.withInputStream { schemas.load(it) }
//
// for (def key : schemas.keySet()) {
// def shortName = key.replaceAll(/http.*schema.(.*).spring-.*/, '$1')
// assert shortName != key
// File xsdFile = subproject.sourceSets.main.resources.find {
// it.path.endsWith(schemas.get(key))
// }
// assert xsdFile != null
// into (shortName) {
// from xsdFile.path
// }
// }
// }
//}
task schemaZip(type: Zip) {
group = "Distribution"
baseName = "spring-framework"
classifier = "schema"
description = "Builds -${classifier} archive containing all " +
"XSDs for deployment at http://springframework.org/schema."
duplicatesStrategy 'exclude'
moduleProjects.each { subproject ->
def Properties schemas = new Properties();
subproject.sourceSets.main.resources.find {
it.path.endsWith("META-INF\\spring.schemas")
}?.withInputStream { schemas.load(it) }
for (def key : schemas.keySet()) {
def shortName = key.replaceAll(/http.*schema.(.*).spring-.*/, '$1')
assert shortName != key
File xsdFile = subproject.sourceSets.main.resources.find {
it.path.endsWith(schemas.get(key).replaceAll('\\/','\\\\'))
}
assert xsdFile != null
into (shortName) {
from xsdFile.path
}
}
}
}
5.根據import-into-idea.md文檔步驟去構建
根據import-into-idea.md文檔裏面介紹,在IDEA比較簡單,對這兩個包分別按下圖操作即可:
6.終極構建Spring項目
最後一步,對整個Spring項目構建,這個時候它會自動下載依賴包,如果有異常,會在控制檯拋出並停止操作。
構建成功在控制檯可以看到下圖提示
第四、執行單元測試
即使整個項目沒有error或者紅點,也不代表項目已經成功構建好了,我們還需要執行個單元測試試下,如果能順利執行單元測試,那證明該項目已經構建成功,這裏我用到的是DispatcherServletTests
這個類,直接運行查看結果:
很明顯,上面單元測試類的所有單測執行成功,證明該項目已經構建完畢,可以開始開啓Spring源碼分析旅程啦!!!
第五、可能遇到的坑點回顧
- 構建成功沒報錯,但運行單元測試有異常 ,提示類找不到
- 方案1:嘗試點擊右邊菜單欄projects刷新項目,然後對Spring(Root)模塊先clean再build一次
- 方案2:針對找不到類的模塊重新build一次
- 方案3:點擊Spring(root)模塊下的Tasks->other->compileTestJava 執行一遍,針對test類編譯一次即可
- 構建失敗,缺少依賴包
- 方案1:對Spring(Root)模塊先clean再build一次(建議打開VPN,有可能存在某些包下載不成功導致)
- 方案2:嘗試針對當前模塊執行
buildDependents
- 構建失敗,執行gradle task失敗
- 方案1:可能是當前運行環境版本不支持問題,如果不重要的問題,可以註釋掉,保證構建正常
- 構建失敗,編碼異常
- 方案1:可以通過idea工具設置:Editor-File encodings 全部設置爲UTF-8