构建Spring源码阅读环境

1. 基础环境

名称说明
JDK 1.8.0_144JDK版本
IntelliJ IDEA 2020.1IDEA版本
Gradle-4.10.3Gradle版本
spring-framework-5.1.xSpring源码版本

2. 准备前提

  • JDK环境
  • IntelliJ IDEA环境
  • Gradle环境

2.1 搭建JDK环境

参考:https://blog.csdn.net/qq_43625140/article/details/108871133

2.2 搭建IDEA环境

参考:https://blog.csdn.net/qq_43625140/article/details/108691749

2.3 搭建Gradle环境

  • Spring源码基于Gradle构建,需要搭建Gradle环境,这里选用【gradle-4.10.3-all.zip】进行搭建,下载地址如下:

Gradle下载:https://services.gradle.org/distributions

  • 下载好gradle后直接解压缩即可,这里解压目录为【D:\DevelopmentTool\gradle\gradle-4.10.3】

  • 配置gradle环境变量,新建GRADLE_HOME变量,配置Gradle解压目录
    在这里插入图片描述

  • 在Path变量中添加【%GRADLE_HOME%\bin】

在这里插入图片描述

  • cmd命令行测试gradle是否安装成功,输入命令【gradle -version】出现gradle版本如下:

在这里插入图片描述

3. 源码搭建

  • 从GitHub下载Spring源码,这里选择从分支【5.1.x】下载zip包进行解压,下载地址如下:

Spring仓库:https://github.com/spring-projects/spring-framework

  • 下载完成后进行解压缩导入IDEA

  • 修改gradle/wrapper/gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
# 使用自己的gradle
distributionUrl=file:///D:/DevelopmentTool/gradle/gradle-4.10.3-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
  • 注释gradle/docs.gradle文件中如下部分:

因为Spring源码基于Linux环境发布GitHub仓库,在Windows环境下需要做出部分修改

//dokka {
//	dependsOn {
//		tasks.getByName("api")
//	}
//	doFirst {
//		classpath = subprojects.collect { project -> project.jar.outputs.files.getFiles() }.flatten()
//		classpath += files(subprojects.collect { it.sourceSets.main.compileClasspath })
//
//	}
//	moduleName = "spring-framework"
//	outputFormat = "html"
//	outputDirectory = "$buildDir/docs/kdoc"
//
//	sourceDirs = files(subprojects.collect { project ->
//		def kotlinDirs = project.sourceSets.main.kotlin.srcDirs.collect()
//		kotlinDirs -= project.sourceSets.main.java.srcDirs
//	})
//	externalDocumentationLink {
//		url = new URL("https://docs.spring.io/spring-framework/docs/$version/javadoc-api/")
//		packageListUrl = new File(buildDir, "api/package-list").toURI().toURL()
//	}
//	externalDocumentationLink {
//		url = new URL("https://projectreactor.io/docs/core/release/api/")
//	}
//	externalDocumentationLink {
//		url = new URL("https://www.reactive-streams.org/reactive-streams-1.0.1-javadoc/")
//	}
//}
//asciidoctor {
//	// only output PDF documentation for non-SNAPSHOT builds
//	if (!project.getVersion().toString().contains("BUILD-SNAPSHOT")) {
//		dependsOn 'asciidoctorPdf'
//	}
//	sources {
//		include '*.adoc'
//	}
//	resources {
//		from(sourceDir) {
//			include 'images/*', 'stylesheets/*', 'tocbot-3.0.2/*'
//		}
//	}
//	logDocuments = true
//	backends = ["html5"]
//	options doctype: 'book', eruby: 'erubis'
//	attributes  'icons': 'font',
//			'idprefix': '',
//			'idseparator': '-',
//			docinfo: '',
//			revnumber: project.version,
//			sectanchors: '',
//			sectnums: '',
//			'source-highlighter': 'coderay@', // TODO switch to 'rouge' once supported by the html5 backend
//			stylesdir: 'stylesheets/',
//			stylesheet: 'main.css',
//			'spring-version': project.version
//}
//task schemaZip(type: Zip) {
//	group = "Distribution"
//	baseName = "spring-framework"
//	classifier = "schema"
//	description = "Builds -${classifier} archive containing all " +
//			"XSDs for deployment at https://springframework.org/schema."
//	duplicatesStrategy 'exclude'
//	moduleProjects.each { subproject ->
//		def Properties schemas = new Properties();
//
//		subproject.sourceSets.main.resources.find {
//			(it.path.endsWith("META-INF/spring.schemas") || it.path.endsWith("META-INF\\spring.schemas"))
//		}?.withInputStream { schemas.load(it) }
//
//		for (def key : schemas.keySet()) {
//			def shortName = key.replaceAll(/http.*schema.(.*).spring-.*/, '$1')
//			assert shortName != key
//			File xsdFile = subproject.sourceSets.main.resources.find {
//				(it.path.endsWith(schemas.get(key)) || it.path.endsWith(schemas.get(key).replaceAll('\\/','\\\\')))
//			}
//			assert xsdFile != null
//			into (shortName) {
//				from xsdFile.path
//			}
//		}
//	}
//}
  • 在gradle/docs.gradle文件中添加如下部分:
task schemaZip(type: Zip) {
	group = "Distribution"
	baseName = "spring-framework"
	classifier = "schema"
	description = "Builds -${classifier} archive containing all " +
			"XSDs for deployment at https://springframework.org/schema."
	duplicatesStrategy 'exclude'
	moduleProjects.each { subproject ->
		def Properties schemas = new Properties();

		subproject.sourceSets.main.resources.find {
			it.path.endsWith("META-INF\\spring.schemas")
		}?.withInputStream { schemas.load(it) }

		for (def key : schemas.keySet()) {
			def shortName = key.replaceAll(/http.*schema.(.*).spring-.*/, '$1')
			assert shortName != key
			File xsdFile = subproject.sourceSets.main.resources.find {
				it.path.endsWith(schemas.get(key).replaceAll('\\/','\\\\'))
			}
			assert xsdFile != null
			into (shortName) {
				from xsdFile.path
			}
		}
	}
}
  • 修改build.gradle文件,修改内容如下:

Spring需要很多依赖,设置阿里云镜像环境便于加快依赖的下载。

repositories {
    maven { url "https://maven.aliyun.com/nexus/content/groups/public/" }
    maven { url "https://maven.aliyun.com/nexus/content/repositories/jcenter" }
    mavenCentral()
    maven { url "https://repo.spring.io/libs-spring-framework-build" }
    maven { url "https://repo.spring.io/milestone" }
    maven { url "https://repo.spring.io/plugins-release" }
}
  • 根据【import-into-idea.md】文件的描述,先编译spring-oxm模块

在这里插入图片描述

  • 构建全部模块

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值