【Spring源码分析(v5.3.x)】第一章:Spring源码搭建

准备环境:

  • JDK 版本:Jdk11
  • IDE 工具:IntelliJ IDEA 2023.1.4
  • 项目构建工具:Gradle 7.5.1

第一步:下载Spring源码

从github上下载Spring源码。我们可以下载release版本,也可以git clone相应的版本。这里下载的是当前最新版本:spring-framework-5.3.31

第二步:配置与源码相匹配的环境

安装jdk

spring-framework-5.3.31 要求jdk最低版本为1.8,实验发现jdk为1.8的情况下执行./gradlew :spring-oxm:compileTestJava命令会出现 import jdk.jfr.category 的问题,这是因为 jdk 8 中并没有 jdk.jfr 相关包的内容的存在,导致编译时找不到对应的类。因此 jdk 需要升级到 11 以上的版本

安装Gradle

Spring 5版本是用Gradle进行构建的,因此我们进入到gradle/wrapper目录,打开gradle-wrapper.properties文件,我们可以看到如下信息:

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

配置属性信息描述:

  • distributionUrl:要下载的gradle的地址,使用哪个版本的gradle通过这里来指定
  • distributionBasedistributionPath:两个属性组合在一起,指定distributionUrl属性下载下来的文件的解压后的存放位置。distributionPathdistributionBase指定的目录下的子目录。
  • zipStoreBase和zipStorePath:两个属性组合在一起,指定distributionUrl属性下载下来的文件的存放位置。zipStorePathzipStoreBase指定的目录下的子目录。

zipStoreBasedistributionBase有两种取值:GRADLE_USER_HOMEPROJECT

  • GRADLE_USER_HOME:表示用户目录。在windows下是%USERPROFILE%/.gradle,例如C:\Users\<user_name>\.gradle\。在linux下是$HOME/.gradle,例如~/.gradle。当然我们也可以通过环境变量来指定GRADLE_USER_HOME的目录。
  • PROJECTPROJECT表示工程的当前目录,即gradlew所在的目录。

下载 gradle-7.5.1-bin.zip 文件(如果下载太慢,也可以通过腾讯云进行下载),配置Gradle环境变量:

GRADLE_HOME=D:\Program Files\Apache\Gradle\gradle-7.5.1
GRADLE_USER_HOME=D:\Program Files\Apache\Gradle\repository

第三步:源码构建

针对eclipse和idea开发工具,官方都已经给出了源码构建文档。

eclipse开发工具:import-into-eclipse.md

文档开篇就提到了一句话:

This document will guide you through the process of importing the Spring Framework
projects into Eclipse or the Spring Tool Suite (_STS_). It is recommended that you
have a recent version of Eclipse. As a bare minimum you will need Eclipse with full Java
8 support, Eclipse Buildship, the Kotlin plugin, and the Groovy plugin.

翻译过来就是:本文将指导您完成将Spring框架项目导入Eclipse或Spring工具套件(STS)的过程。建议您使用最新版本的Eclipse。您至少需要Eclipse和完整的Java 8支持、Kotlin插件和Groovy插件。

Steps中,详细描述了如何一步步构建源码,并把构建成功的源码导入到eclipse工具中。

_When instructed to execute `./gradlew` from the command line, be sure to execute it within your locally cloned `spring-framework` working directory._

1. Ensure that Eclipse launches with JDK 8.
- For example, on Mac OS this can be configured in the `Info.plist` file located in the `Contents` folder of the installed Eclipse or STS application (e.g., the `Eclipse.app` file).
1. Install the [Kotlin Plugin for Eclipse](https://marketplace.eclipse.org/content/kotlin-plugin-eclipse) in Eclipse.
1. Install the [Eclipse Groovy Development Tools](https://github.com/groovy/groovy-eclipse/wiki) in Eclipse.
1. Switch to Groovy 2.5 (Preferences -> Groovy -> Compiler -> Switch to 2.5...) in Eclipse.
1. Change the _Forbidden reference (access rule)_ in Eclipse from Error to Warning
(Preferences -> Java -> Compiler -> Errors/Warnings -> Deprecated and restricted API -> Forbidden reference (access rule)).
1. Optionally install the [AspectJ Development Tools](https://marketplace.eclipse.org/content/aspectj-development-tools) (_AJDT_) if you need to work with the `spring-aspects` project. The AspectJ Development Tools available in the Eclipse Marketplace have been tested with these instructions using STS 4.5 (Eclipse 4.14).
1. Optionally install the [TestNG plugin](https://testng.org/doc/eclipse.html) in Eclipse if you need to execute TestNG tests in the `spring-test` module.
1. Build `spring-oxm` from the command line with `./gradlew :spring-oxm:check`.
1. To apply project specific settings, run `./gradlew eclipseBuildship` from the command line.
1. Import into Eclipse (File -> Import -> Gradle -> Existing Gradle Project -> Navigate to the locally cloned `spring-framework` directory -> Select Finish).
- If you have not installed AJDT, exclude the `spring-aspects` project from the import, if prompted, or close it after the import.
- If you run into errors during the import, you may need to set the _Java home_ for Gradle Buildship to the location of your JDK 8 installation in Eclipse (Preferences -> Gradle -> Java home).
1. If you need to execute JAXB-related tests in the `spring-oxm` project and wish to have the generated sources available, add the `build/generated-sources/jaxb` folder to the build path (right click on the `jaxb` folder and select `Build Path -> Use as Source Folder`).
- If you do not see the `build` folder in the `spring-oxm` project, ensure that the "Gradle build folder" is not filtered out from the view. This setting is available under "Filters" in the configuration of the Package Explorer (available by clicking on the small downward facing arrow in the upper right corner of the Package Explorer).
1. Code away!

总结起来,就是如下操作:

  1. 从命令行执行./gradlew(cmd命令行执行gradlew.bat)。这一步会下载Spring项目所有依赖的工具以及依赖包,因此消耗的时间较长,如果网络不好,还可能会出现命令执行失败的情况,这时我们多执行几次。直到出现下面的成功提示,这时我们第一步的构建就完成了。
$ ./gradlew
Starting a Gradle Daemon (subsequent builds will be faster)

> Task :help

Welcome to Gradle 7.5.1.

To run a build, run gradlew <task> ...

To see a list of available tasks, run gradlew tasks

To see more detail about a task, run gradlew help --task <task>

To see a list of command-line options, run gradlew --help

For more detail on using Gradle, see https://docs.gradle.org/7.5.1/userguide/command_line_interface.html

For troubleshooting, visit https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.5.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 40s
7 actionable tasks: 1 executed, 6 up-to-date

A build scan was not published as you have not authenticated with server 'ge.spring.io'.
For more information, please see https://gradle.com/help/gradle-authenticating-with-gradle-enterprise.
  1. 启动eclipse,将Java环境切换到JDK 8:

  2. 安装Kotlin Plugin for Eclipse、Eclipse Groovy Development Tools插件。

  3. 切换Groovy 2.5(Preferences -> Groovy -> Compiler -> Switch to 2.5…)。

  4. 将Eclipse中的禁止引用(访问规则)从Error更改为Warning(Preferences -> Java -> Compiler -> Errors/Warnings -> Deprecated and restricted API -> Forbidden reference (access rule))

  5. 从命令行构建“spring-oxm”:./gradlew :spring-oxm:check

  6. 从命令行运行./gradlew eclipseBuildship

  7. 将源码导入到eclipse:File -> Import -> Gradle -> Existing Gradle Project -> Navigate to the locally cloned spring-framework directory -> Select Finish。

idea开发工具:import-into-idea.md

文档开篇提到:The following has been tested against IntelliJ IDEA 2016.2.2。建议使用 IntelliJ IDEA 的版本为:2016.2.2。

源码构建步骤:

  1. 从命令行执行./gradlew(cmd命令行执行gradlew.bat)。与eclipse的方式类似,如果中途出现失败的情况,就多执行几次该命令,直到成功为止。

  2. ./gradlew :spring-oxm:compileTestJava 命令预编译 spring-oxm

  3. 导入到 IntelliJ IDEA(File -> New -> Project from Existing Sources -> Navigate to directory -> Select build.gradle)。注意这里导入的方式是File -> New -> Project from Existing Sources,并不是通过open的方式

图解步骤:
gradlew构建源码预编译spring-oxm失败
出现 CoroutinesUtils.java:74: 警告: [deprecation] AccessibleObject中的isAccessible()已过时 ,解决方案:在 org.springframework.core.CoroutinesUtils.invokeSuspendingFunction(Method method, Object target, Object... args) 方法上加 @SuppressWarnings("deprecation") 注解即可。

预编译spring-oxm成功通过new -> Project from Existing Sources方式导入通过new -> Project from Existing Sources方式导入 -> 选择文件目录导入项目 -> 项目构建工具选择Gradle
导入后下载依赖包导入成功

参考网址:

  1. https://blog.csdn.net/u013553529/article/details/55011602
  2. https://cloud.tencent.com/developer/article/2020790
  3. https://www.cnblogs.com/qubo520/p/16044387.html
  • 12
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值