Spring Boot源码编译

目录

前言

环境准备

1.下载Spring Boot源码

2.编译源码

3.解决编译的问题

3.1下载依赖失败

3.2 ge.spring.io没有授权

3.3执行spring-boot-maven-plugin的intTest报错

3.4注释测试模块

4.将项目导入idea

5.新建源码学习项目


前言

        Spring Boot现在作为一个热门的框架,甚至成就了一种开发生态,如果想要更好地掌握该框架,对其源码的学习是必不可少的。然而该怎么搭建其源码的阅读环境,以及如何调试跟踪其源码的执行,就是本篇文章的重点介绍内容,希能够对大家有所帮助。

环境准备

1.安装JDK(1.8+)

2.安装Maven

3.安装Gradle

4.安装Git

5.安装idea

1.下载Spring Boot源码

        本文以2.5.6版本为例,下载地址 Release v2.5.6 · spring-projects/spring-boot · GitHub

2.编译源码

1.进入下载的项目路径
cd /Users/luoshining/devspace/openprojects/spring-boot-2.5.6
2.执行编译命令
./gradlew bulid

3.解决编译的问题

        每个人编译源码时可能会遇到各种各样的问题,我将自己碰到的问题及解决方法分享下。

3.1下载依赖失败

* What went wrong:
Execution failed for task ':spring-boot-project:spring-boot-cli:compileJava'.
> Could not resolve all files for configuration ':spring-boot-project:spring-boot-cli:compileClasspath'.
   > Could not download groovy-templates-3.0.9.jar (org.codehaus.groovy:groovy-templates:3.0.9): Skipped due to earlier error
   > Could not download jline-2.11.jar (jline:jline:2.11)
      > Could not get resource 'https://repo.maven.apache.org/maven2/jline/jline/2.11/jline-2.11.jar'.
         > Could not GET 'https://repo.maven.apache.org/maven2/jline/jline/2.11/jline-2.11.jar'.
            > Read timed out
   > Could not download maven-resolver-transport-file-1.6.1.jar (org.apache.maven.resolver:maven-resolver-transport-file:1.6.1): Skipped due to earlier error
   > Could not download maven-settings-builder-3.6.3.jar (org.apache.maven:maven-settings-builder:3.6.3)
      > Could not get resource 'https://repo.maven.apache.org/maven2/org/apache/maven/maven-settings-builder/3.6.3/maven-settings-builder-3.6.3.jar'.
         > Could not GET 'https://repo.maven.apache.org/maven2/org/apache/maven/

解决办法

配置阿里的镜像地址,打开根目录下的build.gradle,找到allprojects,修改如下

allprojects {
	group "org.springframework.boot"

	repositories {
		mavenCentral()
		// if (version.contains('-')) {
		// 	maven { url "https://repo.spring.io/milestone" }
		// }
		// if (version.endsWith('-SNAPSHOT')) {
		// 	maven { url "https://repo.spring.io/snapshot" }
		// }
		def ALIYUN_REPOSITORY_URL = 'https://maven.aliyun.com/repository/public/'
    def ALIYUN_JCENTER_URL = 'https://maven.aliyun.com/repository/jcenter/'
    def ALIYUN_GOOGLE_URL = 'https://maven.aliyun.com/repository/google/'
    def ALIYUN_GRADLE_PLUGIN_URL = 'https://maven.aliyun.com/repository/gradle-plugin/'
        
    maven { url ALIYUN_REPOSITORY_URL }
    maven { url ALIYUN_JCENTER_URL }
    maven { url ALIYUN_GOOGLE_URL }
    maven { url ALIYUN_GRADLE_PLUGIN_URL }

	}

	configurations.all {
		resolutionStrategy.cacheChangingModulesFor 0, "minutes"
	}
}

3.2 ge.spring.io没有授权

A build scan was not published as you have not authenticated with server 'ge.spring.io'.

解决办法

打开项目根目录的settings.build,注释掉plugins的io.spring.ge.conventions,如下所示

plugins {
	id "com.gradle.enterprise" version "3.6.1"
	//id "io.spring.ge.conventions" version "0.0.8"
}

3.3执行spring-boot-maven-plugin的intTest报错

原因还不清楚,可能跟maven的版本有关吧,我使用的是mavne版本3.8.3,jdk是openjdk11

> Task :spring-boot-project:spring-boot-tools:spring-boot-maven-plugin:intTest FAILED

Found test failures in 1 test task:

:spring-boot-project:spring-boot-tools:spring-boot-maven-plugin:intTest
    org.springframework.boot.maven.BuildInfoIntegrationTests > buildInfoPropertiesAreGenerated(MavenBuild)[1]
    org.springframework.boot.maven.BuildInfoIntegrationTests > buildInfoPropertiesAreGenerated(MavenBuild)[2]
    org.springframework.boot.maven.BuildInfoIntegrationTests > buildInfoPropertiesAreGenerated(MavenBuild)[3]
    org.springframework.boot.maven.BuildInfoIntegrationTests > buildInfoPropertiesAreGenerated(MavenBuild)[4]
    org.springframework.boot.maven.BuildInfoIntegrationTests > buildInfoPropertiesAreGeneratedToCustomOutputLocation(MavenBuild)[1]
    org.springframework.boot.maven.BuildInfoIntegrationTests > buildInfoPropertiesAreGeneratedToCustomOutputLocation(MavenBuild)[2]

解决办法,因为使用gradle编译和插件管理,maven的插件依赖暂时注释掉不使用即可

打开settings.build,将项目中的spring-boot-maven-plugin和spring-boot-docs(为啥也注释它,因为其依赖spring-boot-maven-plugin)注释掉,如下图所示

3.4注释测试模块

如果想编译快点,可以将一些测试模块注释掉,例如这些

include "spring-boot-project:spring-boot-test-autoconfigure"
include "spring-boot-tests:spring-boot-deployment-tests"
include "spring-boot-tests:spring-boot-integration-tests:spring-boot-configuration-processor-tests"
include "spring-boot-tests:spring-boot-integration-tests:spring-boot-launch-script-tests"
include "spring-boot-tests:spring-boot-integration-tests:spring-boot-loader-tests"
include "spring-boot-tests:spring-boot-integration-tests:spring-boot-server-tests"

其中这两个测试模块不要注释,有些模块需要使用

include "spring-boot-project:spring-boot-test"
include "spring-boot-project:spring-boot-test-autoconfigure"

编译前后大小文件比较

从github下载回单的压缩包13.9M,编译后2.02G

4.将项目导入idea

导入完成

5.新建源码学习项目

加入web模块

通过该项目调试springboot源码

 接下来就可以在主流程的main方法打断点,然后一步步追踪其源码的执行过程了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值