基于Gradle搭建Spring 5.3.13-release源码阅读环境的详细流程

目录
  • # 1、安装JDK
  • # 2、安装Gradle
  • # 3、Spring版本命名规则
  • # 4、下载Spring 5.3.13-release源码
  • # 5、修改Spring源码中Gradle配置
  • # 6、构建Spring源码
  • # 7、导入IDEA 点击File --> New --> Project from Existing Sources...
  • # 8、创建Spring源码debug调试模块

# 基于Gradle搭建Spring 5.5.13-release源码阅读环境

Spring版本:5.3.13-release

# 1、安装JDK

首先需要保证本地已经安装JDK1.8及以上版本。这里不做过多赘述,自行安装即可。

# 2、安装Gradle

  • Spring 5.x开始全部都采用Gradle进行编译,构建源码前需要提前安装GradleGradle官网下载地址为:Gradle官网地址
  • 我这里使用的版本是最新的gradle-7.3.1。下载链接为:Gradle-7.3.1下载地址
  • 下载完成之后解压到文件夹。
  • 配置Gradle环境变量,在环境变量的系统变量中添加如下:
$	GRADLE_HOME
$	D:\develop\IDE-Gradle\gradle-7.3.1

在系统环境变量的path中添加环境变量:

$	%GRADLE_HOME%\bin

检测环境,使用gradle -v命令在Terminal中查看:

$	gradle -v

显示如下图则代表gradle安装成功。

gradle安装目录下的init.d文件夹下新建一个init.gradle文件,加入如下配置:

  • repositories中配置获取依赖jar包的顺序:
  • 先是从本地maven仓库中获取
  • 然后mavenLocal()是获取maven本地仓库的路径,和第一条一样,但是不冲突
  • 第三条第四条分别为国内alibaba镜像仓库和国外bstek镜像仓库
  • 最后mavenCentral()是从apache提供的中央仓库中获取依赖jar
allprojects {
    repositories {
        maven { url 'file:///D:/develop/IDE-Repository'}
        mavenLocal()
        maven { name "Alibaba" ; url "https://maven.aliyun.com/repository/public" }
        maven { name "Bstek" ; url "https://nexus.bsdn.org/content/groups/public/" }
        mavenCentral()
    }

    buildscript {
        repositories {
		maven { url 'https://maven.aliyun.com/repository/google'}
        maven { url 'https://maven.aliyun.com/repository/jcenter'}
        maven { url 'https://maven.aliyun.com/nexus/content/groups/public'}
        }
}

# 3、Spring版本命名规则

版本名称 版本 版本意思
snapshot 快照版 尚不稳定,处于开发中的版本
release 稳定版 功能相对稳定的版本,可以对外发行,但是有时间限制
GA(General Availability) 正式版 可广泛使用的稳定版本
M(Milestone) 里程碑版 具有一些全新的功能货时具有里程碑意义的版本
RC(Release Candidate) 最终测试 即将作为正式版本发布的版本

# 4、下载Spring 5.3.13-release源码

Spring 3.x开始,Spring官方不在提供源码下载,所有源代码全部托管在githubSpring Github托管代码首页地址

spring framework 5.3.13-release

  • 源码访问地址:https://github.com/spring-projects/spring-framework/releases/tag/v5.3.13
  • 源码zip压缩包下载地址:https://github.com/spring-projects/spring-framework/archive/refs/tags/v5.3.13.zip
  • 源码tar.gz压缩包下载地址:https://github.com/spring-projects/spring-framework/archive/refs/tags/v5.3.13.tar.gz
  • 国内gitee托管源码访问地址:https://gitee.com/mirrors/Spring-Framework?_from=gitee_search
  • 国内getee源码zip压缩包下载地址:https://gitee.com/mirrors/Spring-Framework/repository/archive/v5.3.13

下载完成之后解压即可。

# 5、修改Spring源码中Gradle配置

修改Spring源码的Gradle构建配置,在Spring源码下修改gradle/wrapper/gradle-wrapper.properties文件如下:

修改distributionUrl为本地的gradle安装包路径,从本地拉去,加快源码构建编译的速度。

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
## distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
## 配置本地gradle, 配置之后需要配置gradle的中央仓库(阿里云maven中央仓库)
distributionUrl=file:///D:/develop/IDE-Gradle/gradle-7.3.1-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

设置阿里云镜像:

修改根目录下build.gradle文件中的repositories

    repositories {
            // 配置本地maven仓库
            mavenLocal()
            // 配置阿里云maven仓库
            maven { url "https://maven.aliyun.com/nexus/content/groups/public/" }
            maven { url "https://maven.aliyun.com/nexus/content/repositories/jcenter/" }
            // maven中央仓库
            mavenCentral()
            maven { url "https://repo.spring.io/libs-spring-framework-build" }
        }

修改根目录下settings.gradle文件中pluginManagement下的repositories

pluginManagement {
	repositories {
		// 配置阿里云 maven 中央仓库
		maven { url 'https://maven.aliyun.com/repository/public/' }
		gradlePluginPortal()
		maven { url 'https://repo.spring.io/plugins-release/' }
	}
}

修改根目录下gradle.properties文件,将jvmargs根据自己本机内存重新分配内存大小,我这里分配了10G的内存:

version=5.3.13
org.gradle.jvmargs=-Xmx10240m
org.gradle.caching=true
org.gradle.parallel=true
kotlin.stdlib.default.dependency=false

# 6、构建Spring源码

使用Terminal进入解压后Spring源码所在的文件目录下,预编译spring-oxm模块:

$	./gradlew :spring-oxm:compileTestJava

如某个jar包没下载成功等,只需要重新执行./gradlew :spring-oxm:compileTestJava再次进行预编译就行了。

构建完成之后修改根目录下setting.gradle文件,注释掉spring-aspects

....
include "spring-aop"
// 移除aspects
// include "spring-aspects"
include "spring-beans"
include "spring-context"
....

# 7、导入IDEA 点击File --> New --> Project from Existing Sources...

然后选择Spring源码所在的目录

点击ok后选择使用gradle导入然后点击Finish

导入之后IDEA会自动进行构建,终止自动构建,此时还需要进行一些设置,Ctrl+Alt+S快捷键打开IDEA设置。

点击File | Settings | Build, Execution, Deployment | Compiler,修改构建时堆内存大小(根据自己的电脑内存自行分配即可,我这里里是分配了5G):

点击File | Settings | Build, Execution, Deployment | Build Tools | Gradle,配置IDEAGradle配置:

Ctrl+Alt+Shift+S快捷键打开Project Structure。修改工程的SDK,分别将ProjectModulesSDKs中的JDK设置为本地安装的JDK(版本为1.8及以上)。

Ctrl+Alt+Shift+S快捷键打开Project Structure。在Modules中排除spring-aspects

Alt+F12快捷键打开Terminal,使用gradlew.bat命令进行编译

$	gradlew.bat

编译成功后会出现BUILD SUCCESSFUL的提示。如果有报错根据报错信息进行处理,多编译几次即可。

编译成功后整个工程如下图所示:

使用shift+shift快捷键输入ApplicationContext类,使用Ctrl+Shift+Alt+U如果能够成功打开类图,也证明Spring源码构建成功:

# 8、创建Spring源码debug调试模块

使用gradle创建一新的debug模块。

创建完成之后将新建模块的build.gradle修改为:新建的模块名称。如我新建的模块叫spring-context-debug,则将其修改为:spring-context-debug.gradle并在其中配置:

plugins {
    id 'java'
}

group 'org.springframework'
version '5.3.13'
repositories {
    // 配置本地 maven 仓库
    mavenLocal()
    // 配置阿里云 maven 仓库
    maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
    maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter' }
    // 配置 maven 中央仓库
    mavenCentral()
dependencies {
    // 测试需要依赖
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
    // 导入 spring-context模块, 包含bean工厂
    implementation(project(':spring-context'))
    // 导入 spring-instrument 模块, 此模块为 spring-context 模块编译所必须的
    implementation(project('::spring-instrument'))
//    compile已经被gradle 7.x 弃用, 爷也是醉了
//    compile(project(":spring-context"))
test {
    useJUnitPlatform()

为新建模块添加spring-contextspring-instrument依赖。配置完成之后刷新gradle。如下图所示,gradle中新建模块的runtimeClasspath中已经新增project spring-conetxtproject spring-instrument代表以来成功。

创建一个TestBean实现DebugBean

DebugBean.java

package com.kapcb.ccc.model;

/**
 * <a>Title: Bean </a>
 * <a>Author: Kapcb <a>
 * <a>Description: Bean <a>
 *
 * @author Kapcb
 * @version 1.0
 * @date 2021/12/11 23:21
 * @since 1.0
 */
public interface DebugBean {
	/**
	 * say method
	 */
	void say();
}

TestBean.java

package com.kapcb.ccc.model;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
 * <a>Title: TestBean </a>
 * <a>Author: Kapcb <a>
 * <a>Description: TestBean <a>
 *
 * @author Kapcb
 * @version 1.0
 * @date 2021/12/11 23:21
 * @since 1.0
 */
public class TestBean implements DebugBean {
	protected final Log log = LogFactory.getLog(getClass());
	private String username;
	public TestBean() {
		log.info("调用TestBean无参构造器");
	}
	@Override
	public void say() {
		log.info("Hi, I'm " + this.username);
	public void setUsername(String username) {
		log.info("调用TestBean中的setUsername方法注入属性");
		this.username = username;
}

添加测试代码,使用注解模式声明一个Bean

package com.kapcb.ccc.configuration;

import com.kapcb.ccc.model.DebugBean;
import com.kapcb.ccc.model.TestBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
/**
 * <a>Title: AnnotationTestConfiguration </a>
 * <a>Author: Kapcb <a>
 * <a>Description: AnnotationTestConfiguration <a>
 *
 * @author Kapcb
 * @date 2021/12/15 16:06
 */
@Configuration
public class AnnotationTestConfiguration {
	@Bean("testBean")
	@Scope("singleton")
	public DebugBean debugBean() {
		TestBean testBean = new TestBean();
		testBean.setUsername("Kapcb(Annotation)");
		return testBean;
	}
}

添加测试代码:

package com.kapcb.ccc;

import com.kapcb.ccc.configuration.AnnotationTestConfiguration;
import com.kapcb.ccc.model.DebugBean;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
 * <a>Title: AnnotationApplication </a>
 * <a>Author: Kapcb <a>
 * <a>Description: AnnotationApplication <a>
 *
 * @author Kapcb
 * @date 2021/12/15 16:05
 */
public class AnnotationApplication {
	public static void main(String[] args) {
		BeanFactory beanFactory = new AnnotationConfigApplicationContext(AnnotationTestConfiguration.class);
		DebugBean testBean = beanFactory.getBean("testBean", DebugBean.class);
		testBean.say();
	}
}

使用debug模式运行,如果debug模式启动main方法报错,尝试修改:File | Settings | Build, Execution, Deployment | Debugger | Data Views | Kotlin在Disable coroutine agent前勾选后保存。

启动成功后控制台会输出如下日志:

Connected to the target VM, address: '127.0.0.1:55594', transport: 'socket'
一月 06, 2022 2:56:42 下午 com.kapcb.ccc.model.TestBean <init>
信息: 调用TestBean无参构造器
一月 06, 2022 2:56:42 下午 com.kapcb.ccc.model.TestBean setUsername
信息: 调用TestBean中的setUsername方法注入属性
一月 06, 2022 2:56:42 下午 com.kapcb.ccc.model.TestBean say
信息: Hi, I'm Kapcb(Annotation)
Disconnected from the target VM, address: '127.0.0.1:55594', transport: 'socket'

GitHub源码地址https://github.com/kapbc/kapcb-spring-source/tree/master/Spring-Framework-v5.3.13

到此这篇关于基于Gradle搭建Spring 5.3.13-release源码阅读环境的文章就介绍到这了,更多相关Gradle搭建Spring源码阅读环境内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • 使用idea和gradle编译spring5源码的方法步骤

    写在前面:spring 应该对于每个从事java开发的大兄弟们来说应该都不陌生的,作为一个从业两年多的小开发仔,个人觉得,每天都在面对spring,确从来没有编译过spring源码,有点不太合适.最近在研究spring源码的时候,突然想起编译一下spring源码,网上应该也有很多大神编译过spring源码,在这里我把我再编译过程的遇到的坑,来跟大家分享下. 版本工具:spring5.0.x,gradle4,9,jdk1.8_131,kotlin1.2.51,groovy2.4.15,Intell

  • springboot-2.3.x最新版源码阅读环境搭建(基于gradle构建)

    一.前言 跟很多小伙伴聊天,发现一个严重的问题,很多小伙伴横向发展的貌似很不错,很多技术都能说出一二,但是如果在某个技术上深挖一下就不行了,问啥啥不会.就拿springboot来说,很多同学止步于springboot的应用,再往深处就一问三不知了,那么如何破局呢?smart哥认为最好的办法就是直捣黄龙,要把一个技术理解透了,听别人讲一万遍原理,不如自己撕一遍源码. 要阅读源码那就首先得先搭建源码阅读环境,那么本篇文章就来介绍下Spring Boot的源码环境搭建. 鉴于spring团队已经全面抛

  • Spring-boot 2.3.x源码基于Gradle编译过程详解

    spring Boot源码编译 1. git上下拉最新版的spring Boot 下载:git clone git@github.com:spring-projects/spring-boot.git,建议下载release版本,不会出现奇奇怪怪的错误 2.修改下载源, gradle\wrapper中的配置文件 gradle-wrapper.properties distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists #d

  • 基于Gradle搭建Spring 5.3.13-release源码阅读环境的详细流程

    目录 # 1.安装JDK # 2.安装Gradle # 3.Spring版本命名规则 # 4.下载Spring 5.3.13-release源码 # 5.修改Spring源码中Gradle配置 # 6.构建Spring源码 # 7.导入IDEA 点击File --> New --> Project from Existing Sources... # 8.创建Spring源码debug调试模块 # 基于Gradle搭建Spring 5.5.13-release源码阅读环境 Spring版本:5

  • 教你使用IDEA搭建spring源码阅读环境的详细步骤

    目录 第一步.准备gradle环境 第二步.下载spring源码 第一步.准备gradle环境 1.去官网下载gradle https://gradle.org/releases/ 2.将其解压缩,创建repository文件夹 和init.d文件夹 创建init.gradle文件 输入文本信息,主要是配置阿里云镜像仓库地址,和maven的类似 gradle.projectsLoaded { rootProject.allprojects { buildscript { repositories

  • VSCode 搭建 x264 源码调试环境的详细步骤

    目录 1.下载 x264 2. 使用上一节介绍的方法为 x264 生成支持 debug 的 x264.exe 3. 在 VSCode 中打开 x264 源码文件夹 4. 创建并配置 launch.json 4.1 创建 launch.json 4.2 配置 launch.json 的 gdb.exe 路径 4.3 配置 launch.json 的 x264.exe 路径 5. 创建并配置 tasks.json 5.1 创建 tasks.json 5.2 配置 tasks.json 的 gcc.e

  • Eureka源码阅读之环境搭建及工程结构

    目录 1. 源码阅读环境搭建 1.1 源码下载: 2. 工程结构速览 3. 调试须知 1. 源码阅读环境搭建 ide:IntelliJ IDEA 2020.1 包管理:gradle eureka版本:1.10.11 Spring Cloud : 2020.0.2 Spring Boot :2.4.4 1.1 源码下载: 下载完源码之后,需要更改一下几个地方: build.gradle增加阿里云镜像仓库,将如下插件版本改一下,否则导入idea会报错: maven { url 'https://ma

  • Pytorch搭建YoloV4目标检测平台实现源码

    目录 什么是YOLOV4 YOLOV4结构解析 1.主干特征提取网络Backbone 2.特征金字塔 3.YoloHead利用获得到的特征进行预测 4.预测结果的解码 5.在原图上进行绘制 YOLOV4的训练 1.YOLOV4的改进训练技巧 a).Mosaic数据增强 b).Label Smoothing平滑 c).CIOU d).学习率余弦退火衰减 2.loss组成 a).计算loss所需参数 b).y_pre是什么 c).y_true是什么. d).loss的计算过程 训练自己的YoloV4

  • Pytorch搭建yolo3目标检测平台实现源码

    目录 yolo3实现思路 一.预测部分 1.主题网络darknet53介绍 2.从特征获取预测结果 3.预测结果的解码 4.在原图上进行绘制 二.训练部分 1.计算loss所需参数 2.pred是什么 3.target是什么. 4.loss的计算过程 训练自己的YoloV3模型 一.数据集的准备 二.数据集的处理 三.开始网络训练 四.训练结果预测 yolo3实现思路 一起来看看yolo3的Pytorch实现吧,顺便训练一下自己的数据. 源码下载 一.预测部分 1.主题网络darknet53介绍

  • Spring菜鸟教你看源码冲面试

    Spring 类的初始化和实例化的不同 IOC 探究spring的IOC容器 DefaultListableBeanFactory是最终实现类,在代码中可以找到HashMap的影子:IOC容器就是用HashMap装的Bean; public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements ConfigurableListableBeanFactory, BeanDefin

  • Mybatis一级缓存和结合Spring Framework后失效的源码探究

    1.在下面的案例中,执行两次查询控制台只会输出一次 SQL 查询: mybatis-config.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"

  • 基于C语言实现随机点名器(附源码)

    突发奇想写了个随机点名器…以供使用 随机点名器 main函数 #include "myList.h" #define FILENAME "stu.txt" void menu();//画面界面; void userOptions(Node* headNode);//用户选项 int main(void) { SetConsoleTitle(L"随机抽查系统"); Node* List = createrList(); readInfoFromFi

随机推荐