文章目次
- spring源码构建,包管一次成功
- 目次
- 环境准备
- 1. github下载spring源码
- 2.配置
- I 将gradle 下载下来后,放到
- II 在D:\mavenproject\mySpring\spring-framework的**build.gradle**
环境准备
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_HOMEdistributionPath=wrapper/distszipStoreBase=GRADLE_USER_HOMEzipStorePath=wrapper/dists#distributionUrl=https\://services.gradle.org/distributions/gradle-4.4.1-bin.zipdistributionUrl=file:///D:\env\gradle-4.4.1-bin.zip这样每次打开项目就不会从官网下载了,直接读取当地gradle
II 在D:\mavenproject\mySpring\spring-framework的build.gradle 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@ComponentScanpublic 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源码编译测试成功,可以学习啦! |