編譯Spring
為了深入了解Spring-Framework不狮,閱讀項目源碼路召,第一步是編譯源碼续室。
代碼地址如下 https://github.com/spring-projects/spring-framework
準(zhǔn)備編譯環(huán)境
本人的工具和環(huán)境如下:
類型 | 描述 |
---|---|
設(shè)備 | Dell筆記本(i5-6550HQ 8G 256SSD) |
操作系統(tǒng) | Window 10 家庭中文版 64位(10.0 16299) |
IDE(開發(fā)集成環(huán)境) | IntelliJ IDEA Ultimate2017.2 |
JDK(Java 開發(fā)套件) | 1.8.0_144 |
Git | 2.10.2 |
Gradle | 4.6 |
查看效果如下
C:\Users\shalk>gradle -v
------------------------------------------------------------
Gradle 4.6
------------------------------------------------------------
Build time: 2018-02-28 13:36:36 UTC
Revision: 8fa6ce7945b640e6168488e4417f9bb96e4ab46c
Groovy: 2.4.12
Ant: Apache Ant(TM) version 1.9.9 compiled on February 2 2017
JVM: 1.8.0_144 (Oracle Corporation 25.144-b01)
OS: Windows 10 10.0 amd64
C:\Users\shalk>git version
git version 2.10.2.windows.1
C:\Users\shalk>java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)
IDEA,JDK挺狰,Git for windows,gradle 都去各自的官網(wǎng)下載丰泊,安裝簡單薯定,稍微有一些配置。
配置方面瞳购,
JDK配置環(huán)境變量(windows配置環(huán)境變量的方法在系統(tǒng)屬性->高級->環(huán)境變量)
CLASSPATH=,;C:\bin\Java\jdk1.8.0_144\lib\tools.jar
JAVA_HOME=C:\bin\Java\jdk1.8.0_144
PATH=其他;C:\bin\Java\jdk1.8.0_60\bin;其他;
Gradle 配置環(huán)境變量
PATH=其他;D:\bin\gradle\gradle-4.6\bin;其他;
Gradle全局配置文件修改学赛,路徑為C:\User\用戶名\.gradle\init.gradle
(目錄可以自己創(chuàng)建,文件自己創(chuàng)建)变丧,目的是為了使用國內(nèi)鏡像
allprojects{
repositories {
def REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public/'
all { ArtifactRepository repo ->
if(repo instanceof MavenArtifactRepository){
def url = repo.url.toString()
if (url.startsWith('https://repo1.maven.org/maven2') || url.startsWith('https://jcenter.bintray.com/')) {
project.logger.lifecycle "Repository ${repo.url} replaced by $REPOSITORY_URL."
remove repo
}
}
}
maven {
url REPOSITORY_URL
}
}
}
Git 貌似會自動配置環(huán)境變量缠捌,如果沒有需要自己添加。
IDEA 配置(如果第一次使用)谊却,配置JDK哑芹,配置Gradle_Home.
另外還有一個重要環(huán)節(jié)是,網(wǎng)絡(luò)需要可以自由訪問google碴萧。
編譯過程
按照官網(wǎng)的步驟(https://github.com/spring-projects/spring-framework/wiki/Build-from-Source)以及IDEA的文檔(https://github.com/spring-projects/spring-framework/blob/master/import-into-idea.md) 并不能完全成功,需要額外的調(diào)整虎谢。
先用命令行下載源碼
git clone https://github.com/spring-projects/spring-framework.git
編譯 spring-oxm
./gradlew :spring-oxm:compileTestJava
打開idea曹质,
選中 build.gradle羽德,點擊OK几莽。
這里選中 Use auto-import
; Create separate module per source set
; Use local gradle
宅静。
(最后使用default gradle wrapper 和local gradle 其實沒有什么區(qū)別姨夹,既然下載了gradle,這里選用local gradle)
導(dǎo)入之后IDEA洒忧,會進(jìn)行一系列的代碼解析和依賴下載够颠,等待處理完成榄鉴。
(雖然文檔建議要源碼把spring-aspects 排除掉,但是這個BUG已經(jīng)修復(fù)剃诅,不需要排除這個模塊驶忌。)
接下來做一些調(diào)整:
調(diào)整1
解決的問題:避免 gradle dokka失敗
修改文件gradle.properties (給Gradle配置代理)
version=5.1.0.BUILD-SNAPSHOT
為
version=5.1.0.BUILD-SNAPSHOT
systemProp.http.proxyHost=localhost
systemProp.http.proxyPort=1080
systemProp.https.proxyHost=localhost
systemProp.https.proxyPort=1080
# 需要驗證時
# systemProp.https.proxyUser=userid
# systemProp.https.proxyPassword=password
# 直接連接而不走代理設(shè)置
# systemProp.https.nonProxyHosts=localhost
為了確保萬無一失付魔,給IDEA配置代理
調(diào)整2:
修改spring-framework\gradle\docs.gradle
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")
}?.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))
}
assert xsdFile != null
into (shortName) {
from xsdFile.path
}
}
}
}
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")
}?.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
}
}
}
}
由于windows路徑的問題翻屈,這里修改之后妻坝,可以讓gradle schemaZip產(chǎn)生zip文件惊窖,避免gradle distZip失敗厘贼。
最后在IDEA中執(zhí)行 gradle build
執(zhí)行結(jié)果如下:
11:42:30: Executing external task 'build'...
:buildSrc:compileJava NO-SOURCE
:buildSrc:compileGroovy UP-TO-DATE
:buildSrc:processResources UP-TO-DATE
:buildSrc:classes UP-TO-DATE
:buildSrc:jar UP-TO-DATE
:buildSrc:assemble UP-TO-DATE
:buildSrc:compileTestJava NO-SOURCE
:buildSrc:compileTestGroovy NO-SOURCE
:buildSrc:processTestResources NO-SOURCE
:buildSrc:testClasses UP-TO-DATE
:buildSrc:test NO-SOURCE
:buildSrc:check UP-TO-DATE
:buildSrc:build UP-TO-DATE
Repository https://jcenter.bintray.com/ replaced by http://maven.aliyun.com/nexus/content/groups/public/.
:spring-core:cglibRepackJar UP-TO-DATE
:spring-core:objenesisRepackJar UP-TO-DATE
:spring-jcl:compileKotlin UP-TO-DATE
:spring-jcl:compileJava UP-TO-DATE
(略)
:spring-websocket:check UP-TO-DATE
:spring-websocket:build UP-TO-DATE
Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
See https://docs.gradle.org/4.6/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 1m 59s
239 actionable tasks: 4 executed, 235 up-to-date
11:44:30: External task execution finished 'build'.
每個模塊的構(gòu)建結(jié)果都在build目錄下嘴秸,jar包都在 build/lib下面赁遗。
補充
以下是構(gòu)建release版本遇到的問題
由于又用另外一臺電腦編譯了一遍,由于之前是用master分支(snapshot)進(jìn)行編譯岩四,為了后續(xù)的分析的穩(wěn)定剖煌,我使用了v5.0.5-release的tag,checkout出了一個release的分支耕姊。遇到了一些新問題茉兰。
問題1. 導(dǎo)入項目之后,idea無法正常解析gradle項目规脸,出現(xiàn) Unindexed remote maven repositories found的bug莫鸭。
解決: IDEA 2017.1的版本遇到的 ,升級到了IDEA 2018.1解決被因。
問題2. spring-beans.gradle 的 30行報錯
解決: 參考master分支和 當(dāng)前分支的差異梨与,修改如下
description = "Spring Beans"
apply plugin: "groovy"
dependencies {
compile(project(':spring-core'))
optional("javax.inject:javax.inject:1")
optional("org.yaml:snakeyaml:1.20")
optional("org.codehaus.groovy:groovy-all:${groovyVersion}")
optional("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
optional("org.jetbrains.kotlin:kotlin-stdlib:${kotlinVersion}")
testCompile("org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}")
}
// This modules does joint compilation for Java and Groovy code,
// with the compileGroovy task.
sourceSets {
main.groovy.srcDirs += "src/main/java"
main.java.srcDirs = []
}
compileGroovy {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
// This module also builds Kotlin code and the compileKotlin task
// naturally depends on compileJava.
// We need to redefine dependencies to break task cycles.
compileGroovy.dependsOn = compileGroovy.taskDependencies.values - 'compileJava'
compileKotlin.dependsOn(compileGroovy)
compileKotlin.classpath += files(compileGroovy.destinationDir)
修改成
description = "Spring Beans"
apply plugin: "groovy"
dependencies {
compile(project(':spring-core'))
optional("javax.inject:javax.inject:1")
optional("org.yaml:snakeyaml:1.20")
optional("org.codehaus.groovy:groovy-all:${groovyVersion}")
optional("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
optional("org.jetbrains.kotlin:kotlin-stdlib:${kotlinVersion}")
testCompile("org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}")
}
// This modules does joint compilation for Java and Groovy code,
// with the compileGroovy task.
sourceSets {
main.groovy.srcDirs += "src/main/java"
main.java.srcDirs = []
}
compileGroovy {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
// This module also builds Kotlin code and the compileKotlin task
// naturally depends on compileJava.
// We need to redefine dependencies to break task cycles.
def deps = compileGroovy.taskDependencies.immutableValues + compileGroovy.taskDependencies.mutableValues
compileGroovy.dependsOn = deps - 'compileJava'
compileKotlin.dependsOn(compileGroovy)
compileKotlin.classpath += files(compileGroovy.destinationDir)
問題3蛋欣,asciidoctor 任務(wù)報錯,錯誤提示 (SystemCallError) Unknown error 123 - FindFirstFile
解決: 參考 https://github.com/jruby/jruby/issues/3957 這個問題是 asciidoctor-pdf 模塊調(diào)用了jruby到踏,在windows10 文件路徑之類的兼容性問題,里面討論是在某些版本解決了楣富,經(jīng)過一翻折騰伴榔,調(diào)整build.gradle
的 buildscript
的依賴,也沒有解決塘安。
辦法1: 由于release版本會構(gòu)建pdf文檔援奢,修改gradle/doc.gradle
修改91-94
// only ouput PDF documentation for non-SNAPSHOT builds
if(!project.getVersion().toString().contains("BUILD-SNAPSHOT")) {
// 注釋此行 backends += "pdf"
}
辦法2: 我瞎試出來的,修改build.gradle
的 buildscript
的依賴
buildscript {
repositories {
maven { url "https://repo.spring.io/plugins-release" }
}
dependencies {
classpath("io.spring.gradle:propdeps-plugin:0.0.8")
classpath("io.spring.gradle:docbook-reference-plugin:0.3.1")
classpath("org.asciidoctor:asciidoctorj-pdf:1.5.0-alpha.16")
classpath("org.asciidoctor:asciidoctorj-epub3:1.5.0-alpha.7")
}
}
修改成
buildscript {
repositories {
maven { url "https://repo.spring.io/plugins-release" }
}
dependencies {
classpath("io.spring.gradle:propdeps-plugin:0.0.8")
classpath("io.spring.gradle:docbook-reference-plugin:0.3.1")
classpath("org.asciidoctor:asciidoctorj-pdf:1.5.0-alpha.14")
classpath("org.asciidoctor:asciidoctorj-epub3:1.5.0-alpha.7")
}
}
我非常懷疑切黔,asciidoctorj-pdf在alpha.16又把這個bug又引入了纬霞。
參考
https://github.com/spring-projects/spring-framework/wiki/Build-from-Source
https://stackoverflow.com/questions/34916981/build-spring-framework-source-code-encounter-an-error
https://blog.csdn.net/wei987654wei/article/details/50635026