IntelliJ IDEA导入Spring源码

工欲善其事,必先利其器,研究spring源码,从导入源码开始吧。本文记录自己 Windows下 IDEA 导入源码折腾的过程。

内容说明:

本文内容同微信公众号【凡登】,关注不迷路,学习上高速,欢迎关注共同学习。

spring源码构建参见 官方文档 https://github.com/spring-projects/spring-framework/wiki/Build-from-Source

1、下载spring源码

安装git,Spring源码现在在github托管,spring官网找到github小图标,点击进入,复制spring源码地址,使用git下载,

spring源码 git地址https://github.com/spring-projects/spring-framework.git

图示:

2、下载gradle、安装配置gradle环境变量

To build you will need Git and JDK 8 update 60 or later. Be sure that your JAVA_HOME environment variable points to the jdk1.8.0 folder extracted from the JDK download.

安装前确保javahome在jdk1.8.0.60以上版本,

2.1、找到合适gradle版本

windows解压zip包,打开spring-framework-5.0.x\gradle\wrapper\gradle-wrapper.properties查看源码使用的gradle的版本号

distributionUrl=https\://services.gradle.org/distributions/gradle-4.4.1-bin.zip

2.2、下载安装gradle,  

gradle下载地址,安装配置gradle环境变量, 使用gradle -v测试是否安装成功

查看是否安装成功

3、编译spring源码

3.1、解压spring源码包,进入当前目录,shift + 鼠标右键选择(在此处打开命令窗口) ,键入 gradlew build 开始构建,

加速构建,可在源码包根目录build.gradle中更新如下配置参考地址,可加快构建效率

repositories {
    maven { url 'http://maven.aliyun.com/nexus/content/repositories/google' }
    maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' } 
    maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
    maven { url 'http://repo.springsource.org/plugins-release'}
}

新增以下配置(没有以下配置,构建过程超级慢……),定义全局的国内镜像,注意该配置不能在plugins { }之前

allprojects {
	repositories {
		maven { url 'http://maven.aliyun.com/nexus/content/repositories/google' }
		maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
		maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
		maven { url 'http://repo.springsource.org/plugins-release'}
	}
}

项目第一次构建,加载相关依赖包,比较慢,构建过程中会出现Read timed out ,提示构建失败,只需再次构建即可

3.2、将spring jar安装到本地maven仓库  命令:gradlew install -x javadoc

-x javadoc跳过文档

4、导入源码到intell idea

4.1、打开IDEA,File->New->Project From Existing Sources

4.2、选择下载的本地spring源码

4.3、配置gradle 操作,配置后点击OK开始导入,过程有点长,如果配置国内镜(参考3.1)像会好很多,

5、配置jdk1.8, spring高版本中使用了1.8新特性,为了避免报错,需要配置

5.1、File--->Setting

5.2、File--->>Project Structure

6、开始spring源码旅行

 7、可能出现的问题

7.1、gradle版本与scan版本问题

异常1:
An exception occurred applying plugin request [id: 'com.gradle.build-scan', version: '1.8']
> Failed to apply plugin [id 'com.gradle.build-scan']
   > This version of Gradle requires version 2.0.2 of the build scan plugin or later.
     Please see https://gradle.com/scans/help/gradle-incompatible-plugin-version for more information.

异常2:

No such property: values for class: org.gradle.api.internal.tasks.DefaultTaskDependency


解决:参考Gradle Enterprise version compatibility | Gradle Enterprise Docs 找到合适的版本,并在gradle.properties中替换如:

plugins {
	id "com.gradle.build-scan" version "1.8" // 替换版本号
	id "io.spring.dependency-management" version "1.0.7.RELEASE" apply false
	id "org.jetbrains.kotlin.jvm" version "1.2.71" apply false
	id "org.jetbrains.dokka" version "0.9.18"
	id "org.asciidoctor.convert" version "1.5.6"
}

7.2、导入已经编译后的spring源码到idea中,出现异常

idea 内置的gradle版本过低,没有与源码中对应的gradle版本,降低源码中的gradle.properties中的班

Error:No such property: GradleVersion for class: JetGradlePlugin

解决方法:参考2.1

7.3、导入源码到idea中,出现jar下载不到,下载超时

Error:Could not find io.spring.gradle:propdeps-plugin:0.0.9.RELEASE.
Searched in the following locations:
    https://plugins.gradle.org/m2/io/spring/gradle/propdeps-plugin/0.0.9.RELEASE/propdeps-plugin-0.0.9.RELEASE.pom
    https://plugins.gradle.org/m2/io/spring/gradle/propdeps-plugin/0.0.9.RELEASE/propdeps-plugin-0.0.9.RELEASE.jar
    http://maven.aliyun.com/nexus/content/repositories/google/io/spring/gradle/propdeps-plugin/0.0.9.RELEASE/propdeps-plugin-0.0.9.RELEASE.pom
    http://maven.aliyun.com/nexus/content/repositories/google/io/spring/gradle/propdeps-plugin/0.0.9.RELEASE/propdeps-plugin-0.0.9.RELEASE.jar
    http://maven.aliyun.com/nexus/content/groups/public/io/spring/gradle/propdeps-plugin/0.0.9.RELEASE/propdeps-plugin-0.0.9.RELEASE.pom
    http://maven.aliyun.com/nexus/content/groups/public/io/spring/gradle/propdeps-plugin/0.0.9.RELEASE/propdeps-plugin-0.0.9.RELEASE.jar
    http://maven.aliyun.com/nexus/content/repositories/jcenter/io/spring/gradle/propdeps-plugin/0.0.9.RELEASE/propdeps-plugin-0.0.9.RELEASE.pom
    http://maven.aliyun.com/nexus/content/repositories/jcenter/io/spring/gradle/propdeps-plugin/0.0.9.RELEASE/propdeps-plugin-0.0.9.RELEASE.jar
Required by:
    project :

解决:参考3.1

  • 8
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值