一、環境相關
- 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源碼環境搭建完成。