准备环境:
- 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通过这里来指定distributionBase
和distributionPath
:两个属性组合在一起,指定distributionUrl
属性下载下来的文件的解压后的存放位置。distributionPath
是distributionBase
指定的目录下的子目录。zipStoreBase和zipStorePath
:两个属性组合在一起,指定distributionUrl
属性下载下来的文件的存放位置。zipStorePath
是zipStoreBase
指定的目录下的子目录。
zipStoreBase
和distributionBase
有两种取值:GRADLE_USER_HOME
和PROJECT
。
GRADLE_USER_HOME
:表示用户目录。在windows下是%USERPROFILE%/.gradle
,例如C:\Users\<user_name>\.gradle\
。在linux下是$HOME/.gradle
,例如~/.gradle
。当然我们也可以通过环境变量来指定GRADLE_USER_HOME
的目录。PROJECT
:PROJECT
表示工程的当前目录,即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!
总结起来,就是如下操作:
- 从命令行执行
./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.
-
启动eclipse,将Java环境切换到JDK 8:
-
安装Kotlin Plugin for Eclipse、Eclipse Groovy Development Tools插件。
-
切换Groovy 2.5(Preferences -> Groovy -> Compiler -> Switch to 2.5…)。
-
将Eclipse中的禁止引用(访问规则)从Error更改为Warning(Preferences -> Java -> Compiler -> Errors/Warnings -> Deprecated and restricted API -> Forbidden reference (access rule))
-
从命令行构建“spring-oxm”:
./gradlew :spring-oxm:check
-
从命令行运行
./gradlew eclipseBuildship
-
将源码导入到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。
源码构建步骤:
-
从命令行执行
./gradlew
(cmd命令行执行gradlew.bat
)。与eclipse的方式类似,如果中途出现失败的情况,就多执行几次该命令,直到成功为止。 -
用
./gradlew :spring-oxm:compileTestJava
命令预编译spring-oxm
-
导入到 IntelliJ IDEA(File -> New -> Project from Existing Sources -> Navigate to directory -> Select build.gradle)。注意这里导入的方式是
File -> New -> Project from Existing Sources
,并不是通过open
的方式。
图解步骤:
出现 CoroutinesUtils.java:74: 警告: [deprecation] AccessibleObject中的isAccessible()已过时
,解决方案:在 org.springframework.core.CoroutinesUtils.invokeSuspendingFunction(Method method, Object target, Object... args)
方法上加 @SuppressWarnings("deprecation")
注解即可。
参考网址:
- https://blog.csdn.net/u013553529/article/details/55011602
- https://cloud.tencent.com/developer/article/2020790
- https://www.cnblogs.com/qubo520/p/16044387.html