编译spring5.2.x源码

编译spring源码是学习spring的第一步,spring源码是用gradle编译的,在编译时会遇到一些问题,在此记录一下。

下载spring源码

第一步需要下载spring的源码,由于官方托管在github,国内访问github不通畅,这里就直接使用gitee的镜像仓库,在国内比较快,地址:https://gitee.com/mirrors/spring-framework

我们可以直接fork出一份到自己的仓库中,然后克隆到自己本地。
这里我使用5.2.x分支作为源码阅读分支。

下载gradle并安装

gradle使用5.6.4版本。安装方式参考百度,和maven基本一致。
gradle在国内访问比较慢,我放到百度云盘上了。

链接:https://pan.baidu.com/s/1_vVFJBep1p6nM8EilG-RYg
提取码:zfh1

修改Spring源码包下的gradle相关配置文件

修改gradle/wrapper/gradle-wrapper.properties 文件

该文件distributionUrl值默认是从官网上下载gradle,我们下载过了,这里直接配置成本地地址

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
#distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-bin.zip
distributionUrl=file:///F:/soft/gradle-5.6.4-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

修改根目录下build.gradle

中央仓库配置为阿里云的,下载依赖更快

repositories {
	maven { url "https://maven.aliyun.com/nexus/content/groups/public" }
	maven { url "https://maven.aliyun.com/nexus/content/repositories/jcenter" }
	mavenCentral()
	maven { url "https://repo.spring.io/libs-spring-framework-build" }
}

修改buildSrc/build.gradle配置

同样配置阿里云中央仓库

repositories {
	maven{ url 'https://maven.aliyun.com/nexus/content/groups/public/'}
	maven{ url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}
	mavenCentral()
	gradlePluginPortal()
}

使用Gradle编译Spring环境

执行gradlew.bat

image.png

执行gradlew :spring-oxm:compileTestJava

image.png

执行gradlew :spring-core:compileTestJava

image.png

F:\dev\my\spring-framework>gradle.bat
Starting a Gradle Daemon (subsequent builds will be faster)

> Task :help

Welcome to Gradle 5.6.4.

To run a build, run gradle <task> ...

To see a list of available tasks, run gradle tasks

To see a list of command-line options, run gradle --help

To see more detail about a task, run gradle help --task <task>

For troubleshooting, visit https://help.gradle.org

BUILD SUCCESSFUL in 41s
1 actionable task: 1 executed
F:\dev\my\spring-framework>gradlew :spring-oxm:compileTestJava
Downloading file:/F:/soft/gradle-5.6.4-all.zip
........................................................................................................................
> Task :spring-oxm:genJaxb
[ant:javac] : warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds

BUILD SUCCESSFUL in 47s
40 actionable tasks: 24 executed, 16 from cache
F:\dev\my\spring-framework>gradlew :spring-core:compileTestJava

BUILD SUCCESSFUL in 6s
17 actionable tasks: 2 from cache, 15 up-to-date

idea配置

上面执行完之后,可以把源码导入idea中,我们需要在idea中配置下gradle

image.png

配置完之后,将整个项目进行编译
image.png

创建测试模块,测试可用性

创建一个新的模块mytest,如下所示:
image.png

build.gradle文件更改,加入spring模块依赖:

dependencies {
    testImplementation(platform("org.junit:junit-bom:5.9.1"))
    testImplementation("org.junit.jupiter:junit-jupiter")
    compile(project(":spring-beans"))
    compile(project(":spring-core"))
    compile(project(":spring-context"))
    compile(project(":spring-aop"))
}

新建配置文件:spring-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

  <bean id="myTestBean" class="org.springframework.test.TestBean"/>

</beans>

创建java类:TestBean

package org.springframework.test;

import org.springframework.stereotype.Component;

@Component
public class TestBean {
	private String name = "me";

	public TestBean(String name) {
		this.name = name;
	}

	public TestBean() {
	}

	@Override
	public String toString() {
		return "MyTestBean{" +
				"name='" + name + '\'' +
				'}';
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

执行测试:

package org.springframework.test;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
	public static void main(String[] args) {
		System.out.println("Hello world!");

		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("org.springframework.test");
		TestBean bean = context.getBean(TestBean.class);
		System.out.println("anno - " + bean);


		ClassPathXmlApplicationContext context1 = new ClassPathXmlApplicationContext("classpath:spring-config.xml");
		Object myTestBean = context1.getBean("myTestBean");
		System.out.println("xml - " + myTestBean);
	}
}

可能遇到问题:

  1. Plugin with id 'java-test-fixtures' not found.
    使用gradle固定版本

  2. No tests found for given includes: [**/Tests.class, **/Test.class]......
    进入 Gradle 设置页面:File -> Settings -> Build,Execution,Deployment -> Build Tools -> Gradle
    修改:将 Gradle Projects 面板中的 Run tests using 选项值改为 IntelliJ IDEA

参考:

  1. 在windows10使用Gradle编译Spring源码
    https://blog.csdn.net/qq_34738512/article/details/129747004
  2. 【极速下载】gradle各版本快速下载地址大全
    https://blog.csdn.net/ii950606/article/details/109105402
posted @ 2023-11-10 14:15  mingshan  阅读(63)  评论(0编辑  收藏  举报