手把手教你如何搭建Spring本地编译环境

大家好,我是极客涛,不知道小伙伴有没有和我一样的情况,在阅读Spring源码时,只通过静态的代码阅读很难有更深刻的理解,所以建议通过写测试类进行debug的方式,对核心的代码进行运行时的状态调试,这样可以更容易理解,本文介绍了如何搭建Spring的本地编译环境。笔者亲试,只要按照步骤来一定可以顺利搭建完成。

使用版本:
gradle-6.4.1-all.zip (官网地址)
Spring-framework-master (官网地址)

一、安装gradle

1.下载解压gradle-6.4.1-all.zip

在这里插入图片描述

2.配置 gradle 的环境变量
vim ~/.bash_profile

export M2_HOME=/Users/wangxt/workspace/software/apache-maven-3.9.2
export GRADLE_HOME=/Users/wangxt/workspace/software/gradle-6.4.1
export PATH=$PATH:$M2_HOME/bin:$GRADLE_HOME/bin

source ~/.bash_profile
3.验证安装结果
gradle -v

------------------------------------------------------------
Gradle 6.2.2
------------------------------------------------------------

Build time:   2020-03-04 08:49:31 UTC
Revision:     7d0bf6dcb46c143bcc3b7a0fa40a8e5ca28e5856

Kotlin:       1.3.61
Groovy:       2.5.8
Ant:          Apache Ant(TM) version 1.10.7 compiled on September 1 2019
JVM:          1.8.0_371 (Oracle Corporation 25.371-b11)
OS:           Mac OS X 13.3 x86_64
4.安装 ./gradlew
gradle wrapper

二、修改配置

1.解压spring-framework-master.zip

在这里插入图片描述

2.修改 settings.gradle 文件,添加阿里云仓库地址
maven { url 'https://maven.aliyun.com/repository/public' }

加完效果

pluginManagement {
	repositories {
		maven { url 'https://maven.aliyun.com/repository/public'}
		gradlePluginPortal()
		maven { url 'https://repo.spring.io/plugins-release' }
	}
}
3.修改 gradle.properties 文件

将 org.gradle.jvmargs的值修改为 -Xmx2048M; 并添加 org.gradle.daemon=true

version=5.3.0-SNAPSHOT
org.gradle.jvmargs=-Xmx2048M
org.gradle.caching=true
org.gradle.parallel=true
org.gradle.daemon=true
4.修改 build.gradle 文件,添加阿里云仓库地址

检索关键词“repositories”

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

加完效果

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" }
			maven { url "https://repo.spring.io/snapshot" } // Reactor
			maven { url "https://oss.jfrog.org/artifactory/oss-snapshot-local" } // RSocket
}
5.修改 setting.gradle ,删除第11行

删完效果

pluginManagement {
	repositories {
		maven { url 'https://maven.aliyun.com/repository/public'}
		gradlePluginPortal()
		maven { url 'https://repo.spring.io/plugins-release' }
	}
}

plugins {
	id "com.gradle.enterprise" version "3.2"
}
6.修改 build.gradle 文件

删除版本号中的SNAPSHOT

imports {
			mavenBom "com.fasterxml.jackson:jackson-bom:2.11.0"
			mavenBom "io.netty:netty-bom:4.1.50.Final"
			mavenBom "io.projectreactor:reactor-bom:2020.0.0-SNAPSHOT"
			mavenBom "io.rsocket:rsocket-bom:1.0.1"
			mavenBom "org.eclipse.jetty:jetty-bom:9.4.29.v20200521"
			mavenBom "org.jetbrains.kotlin:kotlin-bom:1.3.72"
			mavenBom "org.jetbrains.kotlinx:kotlinx-coroutines-bom:1.3.5"
			mavenBom "org.junit:junit-bom:5.6.2"
}
7.修改源码

修改**spring-framework-master\buildSrc\src\main\java\org\springframework\build\compile\CompilerConventionsPlugin.java 文件,删除“-Werror”**参数

三、编译源码

编译时间一般几分钟,只要没有报异常,等着就行

# 编译oxm模块
./gradlew :spring-oxm:compileTestJava
# 编译core模块
./gradlew :spring-core:compileTestJava

四、导入项目

1.使用idea导入 spring-framework-master 项目
2.修改构建工具为 Gradle
  • 修改 Gradle user home 为解压路径
  • 修改 Distribution 为 Local installation, 路径为解压路径
  • 修改 jdk 包
3.创建测试模块,测试编译是否有问题

3.1创建模块
在这里插入图片描述

3.2 修改 build.gradle 配置文件

plugins {
    id("java")
}

group = "org.springframework"
version = "5.3.0-SNAPSHOT"

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

dependencies {
    compile(project(":spring-context"))
    testCompile group: 'junit', name: 'junit', version: '4.12'
}
4.添加测试代码

4.1创建测试bean

public class TestBean {

	private String name = "hello, 你好";

	public String getName() {
		return name;
	}

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

4.2 添加xml配置文件

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

	<bean id="testBean" class="org.springframework.wangxt.TestBean"/>
</beans>

4.2 运行测试代码

public class Main {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
		String name = classPathXmlApplicationContext.getBean(TestBean.class).getName();
		System.out.println(name);
	}
}

运行结果

> Task :spring-wangxt-test:Main.main()
hello, 你好
  • 13
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值