一、环境相关
- IDE:IntelliJ IDEA 2018.1 (Ultimate Edition)
- JDK版本:1.8.0_111
- gradle版本:2.14.1
- spring版本:spring-framework-4.3.9.RELEASE
二、gradle环境安装
由于spring-framework-4.3.9.RELEAS的源码由gradle构建,因此需要有gradle环境。有两种方式可供选择:
-
方式一:gradlew.bat脚本自动构建gradle环境。
这种较为简单,也不会出现gradle版本兼容性问题。
执行spring源码根目录下的gradlew.bat脚本,会根据spring项目下的gradle/wrapper/gradle-wrapper.properties配置文件自动构建gradle环境。
♦注
:这里执行gradlew.bat脚本可以交给idea来做,在将spring导入idea的时候可以选择自动构建gradle环境的方式,这点后面会提到。 -
方式二:用户自己安装的gradle环境。
这种方式相对第一种来说要麻烦一点,而且根据gradle版本的不同,会出现兼容性问题。
▼ gradle安装步骤如下:
1、下载gradle
gradle官网下载地址:https://gradle.org/releases/
选择合适的版本下载即可。
2、配置gradle系统环境变量
3、查看gradle版本
在命令窗口中输入gradle -v返回版本信息表示配置成功,如下图所示:
三、spring源码环境搭建
1、下载spring源码
- spring源码下载地址:https://github.com/spring-projects/spring-framework
下载页面如下所示:
使用git拉取或者直接下载后用IDE导入均可,下载版本和导入方式自行选择。这里笔者选择下载后用idea导入。
2、导入spring源码
将下载的压缩包解压,Import Project源码项目。
选择要导入的源码。
选择Gradle。
Use default gradle wapper对应上面第二节gradle环境安装中的方式一,自动构建环境。直接导入到idea即可,不需要用户再去手动执行gradlew.bat脚本。Use local gradle distribution对应第二种方式,需要用户自己搭建gradle环境,Gradle home为gradle安装目录。这里我们选择第一种方式。
导入成功。
3、编译spring源码
编译源码。
编译源码的过程中会出现一些问题,下面依次解决。
▼ 编译源码过程中的一些问题及解决方法
-
Failed to capture snapshot of input files for task 'distZip' during up-to-date check.
异常信息:
FAILURE: Build failed with an exception.
* What went wrong:
Failed to capture snapshot of input files for task ‘distZip’ during up-to-date check.
> java.io.FileNotFoundException: D:\idea_workspace_demos\spring-framework-4.3.9.RELEASE\build\distributions\spring-framework-4.3.9.RELEASE-schema.zip (系统找不到指定的文件。)
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
:distZip
BUILD FAILED
>>> 解决方法:
找到spring根目录下build.gradle文件中的schemaZip
任务代码:
task schemaZip(type: Zip) {
group = "Distribution"
baseName = "spring-framework"
classifier = "schema"
description = "Builds -${classifier} archive containing all " +
"XSDs for deployment at http://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") // [^1]: ===> 待修改
}?.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)) // (2) ===> 待修改
}
assert xsdFile != null
into (shortName) {
from xsdFile.path
}
}
}
}
将上述代码中(1)、(2)两处的代码分别修改为
it.path.endsWith("META-INF\\spring.schemas")
it.path.endsWith(schemas.get(key).replaceAll('\\/','\\\\'))
-
Failed to capture snapshot of input files for task 'distZip' during up-to-date check.
异常信息:
org.springframework.ui.jasperreports.JasperReportsUtilsTests > renderAsCsvWithDataSource FAILED
java.lang.AssertionError at JasperReportsUtilsTests.java:201
org.springframework.ui.jasperreports.JasperReportsUtilsTests > renderAsCsvWithCollection FAILED
java.lang.AssertionError at JasperReportsUtilsTests.java:201
org.springframework.ui.jasperreports.JasperReportsUtilsTests > renderAsXlsWithDataSource FAILED
org.junit.ComparisonFailure at JasperReportsUtilsTests.java:226
org.springframework.ui.jasperreports.JasperReportsUtilsTests > renderAsXlsWithCollection FAILED
org.junit.ComparisonFailure at JasperReportsUtilsTests.java:226
org.springframework.ui.jasperreports.JasperReportsUtilsTests > renderAsCsvWithExporterParameters FAILED
java.lang.AssertionError at JasperReportsUtilsTests.java:201
org.springframework.ui.jasperreports.JasperReportsUtilsTests > renderAsXlsWithExporterParameters FAILED
org.junit.ComparisonFailure at JasperReportsUtilsTests.java:226
360 tests completed, 6 failed, 10 skipped
:spring-context-support:test FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ‘:spring-context-support:test’.
> There were failing tests. See the report at: file:///D:/idea_workspace_demos/spring-framework-4.3.9.RELEASE/spring-context-support/build/reports/tests/index.html
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
>>> 解决方法:
报错的类JasperReportsUtilsTests.java是spring-context-support模块的单元测试类,直接删除。单独对spring-context-support模块clean后再编译。还有其他单元测试报错也进行相同操作即可。
4、测试
在源码编译成功后,我们尝试新建一个模块来测试。
选择gradle模块。
填写实例名称。
完成创建。
创建一个简单的测试类:
Test类:
package com.hy;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
/**
* @Author: hy
* @Date: 2019/10/19
* @Description:
*/
@Configuration
public class Test {
public static void main(String[] args) {
AnnotationConfigApplicationContext aca = new AnnotationConfigApplicationContext(Test.class);
Test test = (Test)aca.getBean("test");
System.out.println(test);
}
}
新建的hy模块根目录下的build.gradle:
group 'org.springframework'
version '4.3.9.RELEASE'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile(project(":spring-core"))
compile(project(":spring-beans"))
compile(project(":spring-context"))
testCompile group: 'junit', name: 'junit', version: '4.12'
}
运行main方法。
发现编译报错,找不到类:
import org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader;
该类属于spring-beans-groovy
模块,被spring-context
模块引用。
解决办法:
找到spring项目根目录下的build.gradle文件中spring-context模块的位置:project("spring-context")
,添加对spring-beans-groovy模块的依赖:
compile(project(":spring-beans-groovy"))
如图所示:
再次运行,成功输出结果。
至此,spring源码环境搭建完成。