01、 Spring源码阅读环境搭建

1、简单说两句

  本篇文档主要介绍如何完成Spring源代码的下载,编译,以及导入IDEA。
  因为Spring代码是放在GitHub上进行托管的,而国内的网络有时候真的一言难尽,有时候可能连GitHub都登录不上去。国内的码云是一个类似GitHub的网站,这个网站会每天从GitHub上进行一些优秀项目的代码同步,下载代码的时候可以从:https://gitee.com/mirrors/Spring-Framework 这个地址进行下载。
  Spring源码是使用Gradle进行构建的,如果没用过Gradle可以花点时间先看看这个视频:来自Gradle开发团队的Gradle入门教程

2、环境准备

2.1、Jdk

  Jdk最好使用1.8版本的jdk,不能比这低了。其他高版本的jdk也不是不可以,不过在编译的时候可能会提示你使用 -source 1.8。我这里使用的JDK版本如下:

C:\Users\denggh\Desktop> java -version
java version "1.8.0_241"
Java(TM) SE Runtime Environment (build 1.8.0_241-b07)
Java HotSpot(TM) 64-Bit Server VM (build 25.241-b07, mixed mode)

2.2、Gradle

  Gradle是一个类似Maven的代码构建工具。他在进行代码构建的时候,会进行依赖jar包的下载,而下载jar包的仓库和Maven用的是同一个仓库,基于国内的情况你们也知道下载速度是很慢的。我们使用Maven的时候会设置jar包下载的仓库是国内的镜像站点,比如阿里、华为等,所以这里也可以设置Gradle的仓库位置,不然真的编译不了,因为下载太慢了!!
  Gradle的仓库设置可以放到初始化脚本里面设置,在Gradle的官方文档中有这么一段话描述了如何进行初始化脚本的设置。

Using an init script
There are several ways to use an init script:

Specify a file on the command line. The command line option is -I or --init-script followed by the path to the script. The command line option can appear more than once, each time adding another init script. The build will fail if any of the files specified on the command line does not exist.
Put a file called init.gradle (or init.gradle.kts for Kotlin) in the USER_HOME/.gradle/ directory.
Put a file that ends with .gradle (or .init.gradle.kts for Kotlin) in the USER_HOME/.gradle/init.d/ directory.
Put a file that ends with .gradle (or .init.gradle.kts for Kotlin) in the GRADLE_HOME/init.d/ directory, in the Gradle distribution. This allows you to package up a custom Gradle distribution containing some custom build logic and plugins. You can combine this with the Gradle wrapper as a way to make custom logic available to all builds in your enterprise.
If more than one init script is found they will all be executed, in the order specified above. Scripts in a given directory are executed in alphabetical order. This allows, for example, a tool to specify an init script on the command line and the user to put one in their home directory for defining the environment and both scripts will run when Gradle is executed.

来源: https://docs.gradle.org/current/userguide/init_scripts.html#sec:configuring_projects_from_an_init_script

  这里我们可以用多种方式设置初始化脚本,例如就像文档中介绍的在用户的家目录下会有一个.gradle的文件夹,我们可以写一个以.gradle为后缀的文件放到这个目录下,也可以在这个目录下创建一个init.d的文件夹放在这个文件夹下。(说句题外话,还记得maven嘛那个软件是默认在用户家目录下创建个名叫.m2的文件夹。然后所有的相关东西都放在这个目录下,其实这个.gradle和.m2是同一个性质的东西。)
  这个.gradle的文件夹位置可以设置,具体设置的方式就是设置一个GRADLE_USER_HOME的环境变量即可,例如:

  然后我们在GRADLE_USER_HOME中创建一个init.gradle的文件夹,再在这个文件夹里面放一个init.gradle文件,文件的内容如下,指定gradle使用的仓库是国内的仓库,这样下载就会很快了。

allprojects {
    repositories {
        mavenLocal()
        maven { url "http://maven.aliyun.com/nexus/content/groups/public" }
        maven { url "http://maven.aliyun.com/nexus/content/repositories/jcenter" }
        maven { url "https://maven.repository.redhat.com/ga/" }
    }
}

3、编译Spring源码

  Spring源代码下载下来之后,进入到源码的根目录下找到 gradlew 这个文件,然后执行 gradlew build 命令即可。这个命令将会下载依赖jar包,然后执行单元测试完成Spring源码的构建。这个过程的时间取决于网速,长的要好几个小时,短的也要半个小时。


Microsoft Windows [版本 10.0.17763.1131]
(c) 2018 Microsoft Corporation。保留所有权利。
​
D:\IdeaProjects\spring-framework-v5.2.5.RELEASE>gradlew build
Downloading https://services.gradle.org/distributions/gradle-5.6.4-bin.zip
.........................................................................................
​
Welcome to Gradle 5.6.4!
​
Here are the highlights of this release:
 - Incremental Groovy compilation
 - Groovy compile avoidance
 - Test fixtures for Java projects
 - Manage plugin versions via settings script
​
For more details see https://docs.gradle.org/5.6.4/release-notes.html
​
Starting a Gradle Daemon (subsequent builds will be faster)
<-------------> 0% CONFIGURING [1m 27s]
> IDLE
> IDLE
> IDLE
> IDLE
> IDLE
> IDLE
> IDLE
> IDLE
> root project > Resolve dependencies of detachedConfiguration6 > io.spring.nohttp.gradle.plugin-0.0.4.RELEASE.pom
> IDLE
> IDLE

  上面就是执行命令的时候控制台输出,首先第5行的意思就是你这个电脑上没有下载gradle,然后他就给你下载了一个。下载他就会给你存放在 GRADLE_USER_HOME 这个目录下(默认是C:\Users<用户名>.gradle)。具体Gradle相关的知识建议看下文档最开始给的连接中的视频。
  然后第8到16行,就是说欢迎使用Gradle。第18行就是说,我要启动一个Daemon,目的是为了下次构建更快。然后后面就是一些编译构建的过程说明了,耐心等待即可。一般情况下这个过程非常长,主要耗时在下载上,所以说这个可以断断续续执行的,不是说失败后下次就得从头开始了。

BUILD SUCCESSFUL in 24m 30s
250 actionable tasks: 1 executed, 249 up-to-date

  比如说,我这边整个过程是24分30秒,GRADLE_USER_HOME 大小是682MB。

4、导入Spring源码到IDEA

  我使用的IDEA是2020.1这个版本,建议升级到比较高的版本。导入就很简单了 Open 然后选中 build.gradle 文件 Open as Project。因为已经在命令行中预先进行了编译,依赖的jar包也都下载下来了,所以导入就很快了。至此整个Spring源码我们就成功的导入到IDEA中了,后面我们就可以进行Spring源码的阅读以及测试了。

猜你喜欢

转载自www.cnblogs.com/readiay/p/12824907.html