编译Spring
为了深入了解Spring-Framework,阅读项目源码,第一步是编译源码。
代码地址如下 https://github.com/spring-projects/spring-framework
准备编译环境
本人的工具和环境如下:
类型 | 描述 |
---|---|
设备 | Dell笔记本(i5-6550HQ 8G 256SSD) |
操作系统 | Window 10 家庭中文版 64位(10.0 16299) |
IDE(开发集成环境) | IntelliJ IDEA Ultimate2017.2 |
JDK(Java 开发套件) | 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 都去各自的官网下载,安装简单,稍微有一些配置。
配置方面,
JDK配置环境变量(windows配置环境变量的方法在系统属性->高级->环境变量)
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 配置环境变量
PATH=其他;D:\bin\gradle\gradle-4.6\bin;其他;
Gradle全局配置文件修改,路径为C:\User\用户名\.gradle\init.gradle
(目录可以自己创建,文件自己创建),目的是为了使用国内镜像
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 貌似会自动配置环境变量,如果没有需要自己添加。
IDEA 配置(如果第一次使用),配置JDK,配置Gradle_Home.
另外还有一个重要环节是,网络需要可以自由访问google。
编译过程
按照官网的步骤(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) 并不能完全成功,需要额外的调整。
先用命令行下载源码
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 其实没有什么区别,既然下载了gradle,这里选用local gradle)
导入之后IDEA,会进行一系列的代码解析和依赖下载,等待处理完成。
(虽然文档建议要源码把spring-aspects 排除掉,但是这个BUG已经修复,不需要排除这个模块。)
接下来做一些调整:
调整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
# 直接连接而不走代理设置
# systemProp.https.nonProxyHosts=localhost
为了确保万无一失,给IDEA配置代理
调整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产生zip文件,避免gradle distZip失败。
最后在IDEA中执行 gradle build
执行结果如下:
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'.
每个模块的构建结果都在build目录下,jar包都在 build/lib下面。
补充
以下是构建release版本遇到的问题
由于又用另外一台电脑编译了一遍,由于之前是用master分支(snapshot)进行编译,为了后续的分析的稳定,我使用了v5.0.5-release的tag,checkout出了一个release的分支。遇到了一些新问题。
问题1. 导入项目之后,idea无法正常解析gradle项目,出现 Unindexed remote maven repositories found的bug。
解决: IDEA 2017.1的版本遇到的 ,升级到了IDEA 2018.1解决。
问题2. spring-beans.gradle 的 30行报错
解决: 参考master分支和 当前分支的差异,修改如下
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 任务报错,错误提示 (SystemCallError) Unknown error 123 - FindFirstFile
解决: 参考 https://github.com/jruby/jruby/issues/3957 这个问题是 asciidoctor-pdf 模块调用了jruby,在windows10 文件路径之类的兼容性问题,里面讨论是在某些版本解决了,经过一翻折腾,调整build.gradle
的 buildscript
的依赖,也没有解决。
办法1: 由于release版本会构建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