Spring Boot 2.1.x 及以前版本的源码中会有一个这样的工程,貌似可以直接用来调试,但今天讲的是 2.2.x 中如何编译及调试,因为本人是下载的2.2.6版本。

IDEA 编译调试 Spring Boot 2.2.x 源码_xml

1.访问 https://github.com/spring-projects/spring-boot,选择分支,然后以压缩包方式下载并解压(git方式会很慢)。

IDEA 编译调试 Spring Boot 2.2.x 源码_xml_02

2.IDEA导入源码,只导入spring-boot-project就可以了,如下图:

IDEA 编译调试 Spring Boot 2.2.x 源码_xml_03

导入后下载依赖可能需要一定时间,有个小技巧,可以在maven安装目录下的settings.xml中的​​<mirrors></mirrors>​​加一段配置:

<mirror>
<id>alimaven</id><!--阿里maven-->
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url><!--阿里maven的地址-->
<mirrorOf>central</mirrorOf>
</mirror>

加了这段配置后依赖下载贼快。。。

3.选中 spring-boot-project,新建一个module,如下图,然后输入相关信息,一直 Next 直至 Finish。

IDEA 编译调试 Spring Boot 2.2.x 源码_xml_04

4.pom.xml中引入相关maven依赖,如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-parent</artifactId>
<!-- <version>${revision}</version> -->
<version>2.2.6.RELEASE</version>
<relativePath>../spring-boot-parent</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>francis</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
</plugins>
</build>
</project>

5.依次创建package、class,如下图:

IDEA 编译调试 Spring Boot 2.2.x 源码_spring_05

6.启动项目,如果启动过程中子项目test目录下单测报错,可直接删除test目录。

7.访问接口,看到下图说明成功,接着可以在源码中各种断点调试了。。。

IDEA 编译调试 Spring Boot 2.2.x 源码_maven_06