spring源码构建,保证一次成功
目录
文章目录
环境准备
1.代码准备 : spring-framework 5.0.x版本
下载地址:https://github.com/spring-projects/spring-framework
2.jdk版本: jdk 1.8.0_181(环境变量自行配置,建议用rapidee配置方便)
下载地址:https://www.oracle.com/java/technologies/javase/8u-relnotes.html
3.gradle版本:gradle-4.4.1-bin.zip
(此版本的gradle在D:\mavenproject\mySpring\spring-framework\gradle\wrapper下的gradle-wrapper.properties 查看) ,配置中是distributionUrl=https://services.gradle.org/distributions/gradle-4.4.1-bin.zip
此处我们用 gradle-4.4.1-all.zip ,方便出问题,用源码进行调试
下载地址 https://gradle.org/releases/
4.idea版本:2020.2
1. github下载spring源码
https://github.com/spring-projects/spring-framework
I.fork到本地仓库
II.拉取代码
之后从自己的github下载即可,这样做的目的是为了自己学习提交代码到自己的远程仓库,因为直接github的spring-framework提交的代码会被审批之后才能提交,没有金刚钻,就不要往上提了,不利于个人学习
2.配置
I 将gradle 下载下来后,放到
D:\mavenproject\gradle-4.4.1-all.zip目录下 ,修改D:\mavenproject\mySpring\spring-framework\gradle\wrapper下的
gradle-wrapper.properties ,修改后的配置如下:
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
#distributionUrl=https\://services.gradle.org/distributions/gradle-4.4.1-bin.zip
distributionUrl=file:///D:\env\gradle-4.4.1-bin.zip
这样每次打开项目就不会从官网下载了,直接读取本地gradle
II 在D:\mavenproject\mySpring\spring-framework的build.gradle</ 文件中添加阿里云镜像,添加后的文件如下
buildscript {
repositories {
maven { url "http://maven.aliyun.com/nexus/content/groups/public/"}
maven{ url "http://maven.aliyun.com/nexus/content/repositories/jcenter"}
maven { url "https://repo.spring.io/plugins-release" }
}
dependencies {
classpath("io.spring.gradle:propdeps-plugin:0.0.9.RELEASE")
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")
}
}
此文件的中间位置下面还一处仓库配置,搜索repositories即可
repositories {
maven { url "http://maven.aliyun.com/nexus/content/groups/public/"}
maven{ url "http://maven.aliyun.com/nexus/content/repositories/jcenter"}
maven { url "https://repo.spring.io/libs-release" }
}
||| 解压gradle-4.4.1-all.zip 并配置环境变量
修改idea的新项目配置 ctrl+alt+s 的gradle配置
注意:此处需指定正确的路径,要不然会报错:
Could not create parent directory for lock file C:\Program Files\Android\Android Studio\gradle\gradle-3.2\bin\wrapper\dists\gradle-3.3-all\55gk2rcmfc6p2dg9u9ohc3hw9\gradle-3.3-all.zip.lck
https://stackoverflow.com/questions/45527316/could-not-create-parent-directory-for-lock-file/48979955
ctrl+alt+shift+s
需指定模块的依赖jdk8
下载依赖
以上配置完成后,即可刷新依赖,下载项目依赖
下载完成即可
到此处为止,依赖下载完成!
验证
找到ApplicatonContext ctrl+alt+shift+u
出现类图,则成功
编译
前置动作
找到gradle目录下的docs.gradle 文件,修改以下几点:
1. 注释掉 dokka
//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/")
// }
//}
2.注释掉 asciidoctor
//asciidoctor {
// sources {
// include '*.adoc'
// }
// resources {
// from(sourceDir) {
// include 'images/*', 'stylesheets/*', 'tocbot-3.0.2/*'
// }
// }
// logDocuments = true
// backends = ["html5"]
// // only ouput PDF documentation for non-SNAPSHOT builds
// if(!project.getVersion().toString().contains("BUILD-SNAPSHOT")) {
// backends += "pdf"
// }
// 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
//
//}
3. 修改task schemaZip
windows操作系统,需要把正斜杠替换成反斜杠,修改后的配置如下:
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'
//当前系统是否是windows的标志
def isWindows = System.properties['os.name'].toUpperCase().contains('WINDOWS')
//不同的操作系统,表示子目录的符号是不同的
def schemaPath = isWindows ? "META-INF\\spring.schemas" : "META-INF/spring.schemas"
moduleProjects.each { subproject ->
def Properties schemas = new Properties();
subproject.sourceSets.main.resources.find {
it.path.endsWith(schemaPath)
}?.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 {
//如果是windows环境,就要对路径中的分隔符做替换
isWindows ? it.path.endsWith(schemas.get(key).replaceAll('\\/','\\\\')) : it.path.endsWith(schemas.get(key))
}
assert xsdFile != null
into (shortName) {
from xsdFile.path
}
}
}
}
找到import-into-idea.md 按照步骤进行操作
I.Precompile spring-oxm
执行编译的过程中,可能会出现如下问题,可能会出现多次,此处应该是网络问题,到指定资源抢占问题,不用关心,重新执行即可
构建了多次,终于成功了!
II 去掉spring-sapects 模块
III 编译下spring-core
成功!就恭喜你啦!
IV.执行整个项目的编译
若不跳过单元测试,可能会有问题,我试过,不跳过单元测试确实存在问题,这里我就直接跳过单元测试执行了
在代码的目录,打开cmd执行如下命令
如果网络够好的话,而且你又比较幸运的话,一次就成功了,否则,你得尝试多次,但是肯定会成功!
测试
新建module
spring-mytest
在spring-mytest的build.gradle中添加如下依赖
dependencies {
compile(project(":spring-context"))
compile(project(":spring-core"))
compile(project(":spring-beans"))
compile(project(":spring-aop"))
testCompile group: 'junit', name: 'junit', version: '4.12'
}
刷新依赖即可
I.新建User.java
package com.study;
public class User {
private String name;
private Integer age;
public User() {
}
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
II 新建SysConfig
package com.study;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
public class SysConfig {
@Bean
public User user(){
return new User("lq",12);
}
}
III 新建测试类TestMy
package com.study;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestMy {
public static void main(String[] args) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SysConfig.class);
User user = (User)applicationContext.getBean("user");
System.out.println(user.toString());
}
}
运行输出
至此 spring源码编译测试成功,可以学习啦!