Spring 源码编译

1. 下载源码

下载地址:
https://github.com/spring-projects/spring-framework

springboot编译时间太长的原因 springboot源码编译_spring


建议下载最新的 RELEASE 版本。

我这里下载的是:
https://github.com/spring-projects/spring-framework/tree/v5.2.11.RELEASE

解压压缩包。

2.修改配置

进入目录:

D:\ideaworkspace\spring-framework-5.2.11.RELEASE

修改 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" }
		}

3.编译

在源码文件夹目录下打开控制台输入编译命令:

springboot编译时间太长的原因 springboot源码编译_spring_02

gradlew :spring-oxm:compileTestJava

如果是首次编译的话他会自动下载配置好的 gradle-5.6.4-bin.zip 包速度会比较慢。

这里建议网速不好的同学可以自己下

3.1 网速不好 gradle 下载慢解决方案

直接 copy

D:\ideaworkspace\spring-framework-5.2.11.RELEASE\gradle\wrapper\gradle-wrapper.properties

gradle-wrapper.properties 配置文件夹下的下载地址出来下载 OR 官网下载

distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-bin.zip

在你的 gradle 安装包下新建 .gardle 配置环境变量
PS : 如果你的是 MAC 系统,同样的在命令行中配置好对应的 GRADLE_HOME 和 PATH 即可

springboot编译时间太长的原因 springboot源码编译_springboot编译时间太长的原因_03


PATH 中添加:

%GRADLE_HOME%\bin

验证是否配置成功:gradle -v

springboot编译时间太长的原因 springboot源码编译_Spring_04


配置全局阿里镜像:

.gradle 目录下新建 init.gradle 文件并配置

allprojects{
    repositories {
        def ALIYUN_REPOSITORY_URL = 'https://maven.aliyun.com/nexus/content/groups/public'
        def ALIYUN_JCENTER_URL = 'https://maven.aliyun.com/nexus/content/repositories/jcenter'
        all { ArtifactRepository repo ->
            if(repo instanceof MavenArtifactRepository){
                def url = repo.url.toString()
                if (url.startsWith('https://repo1.maven.org/maven2')) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL."
                    remove repo
                }
                if (url.startsWith('https://jcenter.bintray.com/')) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_JCENTER_URL."
                    remove repo
                }
            }
        }
        maven {
            url ALIYUN_REPOSITORY_URL
            url ALIYUN_JCENTER_URL
        }
    }
}

在源码项目目录下运行 gradle clean 接下来就是考验网速的时候了(该来的终究逃不过),耐心等待。出现 BUILD SUCCESSFUL 说明编译成功,下面的 git 可以忽略。

springboot编译时间太长的原因 springboot源码编译_maven_05

使用 IDEA 打开项目

file -> open -> open your spring project

在项目设置中设置你的 gradle 地址

等待 build

springboot编译时间太长的原因 springboot源码编译_springboot编译时间太长的原因_06


如果你网速极好,上面的流程会很顺利,所以建议找个网速好的地方搞,不然会很麻烦……构建成功

springboot编译时间太长的原因 springboot源码编译_Spring_07

新建模块

springboot编译时间太长的原因 springboot源码编译_源码编译_08


选择 Gradle 模块 next 输入名称 Finish

springboot编译时间太长的原因 springboot源码编译_maven_09


在你新建的包模块 build.gradle 下添加依赖:

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

随意创建一个 bean

springboot编译时间太长的原因 springboot源码编译_spring_10

import org.springframework.stereotype.Service;

@Service
public class TestServiceImpl {

	public void helloWord(){
		System.out.println("hello spring");
	}
}

创建一个容器并启动

springboot编译时间太长的原因 springboot源码编译_spring_11

import com.dhls.TestServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan("com.dhls")
public class MainStat {

	public static void main(String[] args) {
		ApplicationContext applicationContext=new AnnotationConfigApplicationContext(MainStat.class);
		TestServiceImpl testService=applicationContext.getBean(TestServiceImpl.class);
		testService.helloWord();
	}
}

OK!! 到这里 Spring 源码就编译运行完成了,到 Spring 的海洋中畅游吧!