Spring 源碼閱讀環境的搭建


前言

本文記錄了 Spring 源碼環境的搭建方式,以及踩過的那些坑!​當前版本:5.3.2-SNAPSHOT。

環境准備

  1. Git
  2. JDK
    1. master 分支需要 JDK 11
    2. 5.2.x 分支, JDK8 即可
  3. Gradle 6.5.1
  4. IDEA 最新 (2020.2.3)

Spring 源碼倉庫地址:https://github.com/spring-projects/spring-framework

下載源碼

  1. clone 源碼
git clone  https://github.com/spring-projects/spring-framework.git
  1. 使用 IDEA 打開

    1. 等待 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 構建的。)

  1. 報錯如下:
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 配置,那就添加上吧!

  1. 這時候想着添加 git

VCS -> Enable Version Control Integration... -> 右上角 Reload All Gradle Projects

依然報錯

fatal: Needed a single revision

  1. 查詢問題

issues 地址:https://github.com/spring-projects/spring-framework/issues/24467

建議使用

$ git clone git@github.com:spring-projects/spring-framework.git

意思就是 zip 發行版主要是用來共享源代碼,但不一定用於構建它。

  1. 最后我選擇了使用 clone 的方式,直接 clone 下來,然后 build 通過。

缺少 cglib 和 objenesis 包

Kotlin: warnings found and -Weeror specified

沒有 spring-cglib-repackspring-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

快捷鍵:⌘ + ;

相關資料

  1. Spring 倉庫:https://github.com/spring-projects/spring-framework
  2. Spring 構建文檔:https://github.com/spring-projects/spring-framework/wiki/Build-from-Source

歷史文章


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM