源码学习:IDEA成功导入、编译、调试Spring5源码项目

目录

 

成功编译(不代表就完全成功,需要测试成功才是最终成功)

错误信息汇总:

1、编译流程

1.1、环境搭建:JDK1.8.0_211、Gradle4.9、IDEA2019.1.2

1.2、将源码导入IDEA

1.3、修改文件

第一步:注释dokka和asciidoctor 

第二步:修改schemaZip的两处位置,看注释行

1.4、预编译

2、测试模块


成功编译(不代表就完全成功,需要测试成功才是最终成功)

测试成功

看到项目调试成功,那就可以开启我们的源码学习之路啦!!!!

错误信息汇总:

1、TASK API:这个任务出现error,大都是javadoc出现问题,确定为JDK版本不对

正确做法是换为我们要求的JDK版本即可!

2、成功编译后,运行自己的测试项目报错:找不到InstrumentationSavingAgent

正确做法是spring-context.gradle修改 optional(project(":spring-instrument"))为 compile(project(":spring-instrument")),然后重新编译一下spring-context(compileTestJava+build),完美解决!


1、编译流程

1.1、环境搭建:JDK1.8.0_211、Gradle4.9、IDEA2019.1.2

JDK:jdk1.8.0_211(推荐这个版本)链接

Gradle:gradle4.9链接

Gradle的GRADLE_HOME环境配置和Java配置JAVA_HOME是一样的

记得在Path中配置%GRADLE_HOME%\bin

IDEA:IDEA 2019.1.2

1.2、将源码导入IDEA

在IDEA中File->Open将源码的文件夹导入

1、点击auto-import

2、点击使用本地的gradle

3、JVM环境设置为你的jdk1.8.0_211,也就是列表里的JAVA_HOME选项

4、在这里面配置vm选项

点击

设置VM为-XX:MaxMetaspaceSize=2048m -Xmx2048m -XX:MaxHeapSize=1024m

点击ok即可

1.3、修改文件

第一步:注释dokka和asciidoctor 

/*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/")
	}
}

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

}*/

第二步:修改schemaZip的两处位置,看注释行

task schemaZip(type: Zip) {
	group = "Distribution"
	baseName = "spring-framework"
	classifier = "schema"
	description = "Builds -${classifier} archive containing all " +
			"XSDs for deployment at https://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 {
                //此处将Linux路径符号替换为Windows路径符号
				it.path.endsWith(schemas.get(key).replaceAll('\\/','\\\\'))
			}
			assert xsdFile != null
			into (shortName) {
				from xsdFile.path
			}
		}
	}
}

1.4、预编译

第一步:打开源码文件夹中的

spring-oxm和spring-core需要预先编译

第二步:预编译

打开gradle面板,找到各个源码模块,然后双击compileTestJava

第三步:整体编译

等待即可,大概20-40分钟

2、测试模块

在源码项目new一个moudle,类型为gradle的管理的java项目即可

自己使用注解测试一下即可

UserDao.java

package com.lpf.dao;
import org.springframework.stereotype.Repository;
@Repository
public class UserDao {
	public void printInfo(){
		System.out.println("user dao");
	}
}

Test.java

package com.lpf.test;
import com.lpf.dao.UserDao;
import com.lpf.utils.AnnotationConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Test {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext ac=new
				AnnotationConfigApplicationContext(AnnotationConfig.class);
		UserDao userDao=(UserDao) ac.getBean(UserDao.class);
		userDao.printInfo();
	}
}

AnnotationConfig.java

package com.lpf.utils;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.lpf")
public class AnnotationConfig {
}

build.gradle添加compile project(":spring-context")

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile project(":spring-context")
}

运行Test.java,出现下图即证明spring5.1.x项目完美部署成功!


猜你喜欢

转载自blog.csdn.net/StrawberryMuMu/article/details/98099886