Spring源码IDEA编译

Spring源码学习的第一步,就是源码的构建编译,本文主要介绍了如何在IDEA中构建源码,仅供学习参考



文章目录

  • Spring源码IDEA编译
  • 前言
  • 一、下载Spring源码包
  • 二、导入IDEA步骤
  • 1.修改build.gradle文件
  • 2.导入源码
  • 总结



前言

Spring源码学习是个持久漫长的过程,不过第一步就是要能在集成环境中进行顺利的构建编译,刚构建的时候很多人可能会遇到各种问题,不过需要一点耐心,终会迈出最坚实的一步。在此,分享我的步骤。
我的环境版本如下:

  1. IDEA 2019.2.3
  2. JDK1.8
  3. spring-framework-5.2.0.RELEASE

以下是本篇文章正文内容,具体构建流程如下

一、下载Spring源码包

Spring源码下载地址官网地址

idea springboot编译慢 idea编译spring源码_maven

进入上面页面后,点击红色箭头地方可以直接跳转到GitHub下载,Spring源码已托管到GitHub上,选择一个版本进行下载,我这里下载的是spring-framework-5.2.0.RELEASE

idea springboot编译慢 idea编译spring源码_java_02

直接download到本地,解压即可。按照Spring官网构建给出的建议

Within your locally cloned spring-framework working directory:

Precompile spring-oxm with ./gradlew :spring-oxm:compileTestJava
Import into IntelliJ (File -> New -> Project from Existing Sources -> Navigate to directory -> Select build.gradle)
When prompted exclude the spring-aspects module (or after the import via File-> Project Structure -> Modules)
Code away

进入Spring源码解压的目录,打开cmd,输入gradlew :spring-oxm:compileTestJava(注意,这里不要加./),如果没有安装Gradle,默认会下载安装,你也可以手动停掉,然后去Gradle官网下载提示的版本即可,并配好环境变量,我在构建的过程中,直接默认的安装,中间几次出现错误,可能是因为网络原因,多尝试几次,然后就能看到build successful!

二、导入IDEA步骤

1.修改build.gradle文件

buildscript {
	repositories{
		maven{url 'http://maven.aliyun.com/nexus/content/groups/public/'} 
		maven{url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
	}
	dependencies {
		classpath 'org.asciidoctor:asciidoctorj-pdf:1.5.0-alpha.16'
		classpath 'io.spring.asciidoctor:spring-asciidoctor-extensions:0.1.3.RELEASE'
	}
}

打开build.gradle文件,添加阿里云镜像地址,红色标注的地方

idea springboot编译慢 idea编译spring源码_maven_03

并在此文件中搜索repositories关键字,添加

idea springboot编译慢 idea编译spring源码_maven_04

2.导入源码

选择File-New-Project from Existing Sources…选择源码文件夹,然后点击OK

idea springboot编译慢 idea编译spring源码_spring_05

选择Gradle进行构建,构建快的情况下1min,慢的话,需要7-8分钟,需耐心等待,如果中间有错误,可能是有的包下载超时,可以单独下载下来后放入目录即可。

idea springboot编译慢 idea编译spring源码_maven_06

然后依据Spring官网给出的建议,我们还需要排除spring-aspects模块,直接Unload即可

idea springboot编译慢 idea编译spring源码_官网_07


重新再次Gradle构建即可,最后我们进行测试是否构建正确,自己写一个模块,然后依赖Spring的组件

idea springboot编译慢 idea编译spring源码_官网_08

引入Spring-Context组件进行测试验证

idea springboot编译慢 idea编译spring源码_maven_09

控制台输出没有错误,且正确输出,证明源码构建完成,可以继续开启Spring源码阅读的下一个阶段学习。

总结

本文介绍了如何在IDEA中构建Spring源码,Spring源码的构建是阅读Spring源码关键的一步,可能会遇到各种各样大同小异的问题,多尝试几次,多找找解决方案,不要轻言放弃,毕竟Spring源码可持续学习的内容很精很好,长期吸收,必能受益!以上就是今天要分享的内容。