7

Spring 源码阅读环境的搭建

 3 years ago
source link: http://www.cnblogs.com/liuzhihang/p/spring-source-build.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

前言

本文记录了 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. zIbAf2m.png!mobile
    2. 63uqeyJ.png!mobile
    3. 等待 IDEA 加载完成即可。

注: 也可以指定 clone 的分支

git clone -b 5.2.x  https://github.com/spring-projects/spring-framework.git

或者先 fork 到自己的仓库,然后再 clone。

zqqquuR.png!mobile

这里我是 fork 到我的仓库,然后再 clone 的。

当前 master 分支代表的版本为 5.3.2-SNAPSHOT。

执行测试

  • 在项目右键创建 module

3u2aIbi.png!mobile

  • 选择 Gradle Java

vmEjUv6.png!mobile

  • 创建 module

MRf67bY.png!mobile

  • 在 build.gradle 中添加配置
compile(project(":spring-context"))

uimYZri.png!mobile

  • 创建测试类并测试

63yEBj6.png!mobile

其中 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	
    ... 其他省略

Rfa2Ab.png!mobile

看意思是没有 git 配置,那就添加上吧!

  1. 这时候想着添加 git

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

依然报错

fatal: Needed a single revision

jYzY3aU.png!mobile

  1. 查询问题

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

建议使用

$ git clone [email protected]:spring-projects/spring-framework.git

MJ3aYve.png!mobile

意思就是 zip 发行版主要是用来共享源代码,但不一定用于构建它。

  1. 最后我选择了使用 clone 的方式,直接 clone 下来,然后 build 通过。

缺少 cglib 和 objenesis 包

Kotlin: warnings found and -Weeror specified

Eva6Vbq.png!mobile

没有 spring-cglib-repackspring-objenesis-repack

RBVN7fR.png!mobile

执行这两个即可。

找不到包 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

快捷键: ⌘ + ;

RvENFzZ.png!mobile

相关资料

  1. Spring 仓库: https://github.com/spring-projects/spring-framework
  2. Spring 构建文档: https://github.com/spring-projects/spring-framework/wiki/Build-from-Source

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK