Spring源码搭建

余生长醉 提交于 2021-02-02 16:32:34

第一步:下载Spring源码

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

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

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

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.3-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-5.6.3-bin.zip 文件,配置Gradle环境变量:

GRADLE_HOME=D:\Program Files\Apache\Gradle\gradle-5.6.3
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, 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://www.eclipse.org/ajdt/downloads/) (_AJDT_) if you need to work with the `spring-aspects` project.
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项目所有依赖的工具以及依赖包,因此消耗的时间较长,如果网络不好,还可能会出现命令执行失败的情况,这时我们多执行几次。直到出现下面的成功提示,这时我们第一步的构建就完成了。
D:\works\wywuzh@163.com\learning\spring-projects\v5.2.1\spring-framework-5.2.1.RELEASE>gradlew.bat
Starting a Gradle Daemon (subsequent builds will be faster)                                                             

Welcome to Gradle 5.6.3.

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

To see a list of available tasks, run gradlew tasks

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

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

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

BUILD SUCCESSFUL in 8s
1 actionable task: 1 executed
  1. 启动eclipse,将Java环境切换到JDK 8: 1 安装Kotlin Plugin for Eclipse、Eclipse Groovy Development Tools插件。 2 切换Groovy 2.5(Preferences -> Groovy -> Compiler -> Switch to 2.5...)。 3 将Eclipse中的禁止引用(访问规则)从Error更改为Warning(Preferences -> Java -> Compiler -> Errors/Warnings -> Deprecated and restricted API -> Forbidden reference (access rule))

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

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

  4. 将源码导入到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的方式

图解步骤:

参考网址:

  1. https://blog.csdn.net/u013553529/article/details/55011602
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!