Spring5源码 - 构建源码环境

开发环境

IDEA 2020.2.1 + OpenJDK8U-jdk_x64_windows_hotspot_8u265b01 + gradle-6.5.1-bin.zip

遵循官方指导文档

git clone 源码

源码地址: https://github.com/spring-projects/spring-framework

copy地址,待会要用
这里我将放到了 D:\workspace
打开 git bash ,执行如下命令 git clone https://github.com/spring-projects/spring-framework.git


等一会即可,如下所示

官方指导手册

gradle调整 [可选]

gradle离线安装

打开 D:\workspace\spring-framework\gradle\wrapper\gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.6-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

gradle会去 distributionUrl对应的地址下载gradle压缩包,如果网络不好的话,可以提前现在好一个离线包



修改 distributionUrl,如下

distributionUrl=file\:///D\:/Soft/gradle-6.5.1-bin.zip

gradle中央仓库地址调整

国内环境的话,建议调整一下gradle的中央仓库。

找到 build.gradle


打开 285行 添加阿里云地址

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

执行 gradlew.bat

windows环境下 gradlew.bat 就是我们需要执行的脚本

第一次要下载好多依赖,速度取决于你的网速,如果失败的话,多执行几次,基本都是可以成功的。


导入到IEDA


根据提示来操作即可

Precompile spring-oxm with ./gradlew :spring-oxm:compileTestJava
Import into IntelliJ (File -> New -> Project from Existing Sources -> Navigate to directory -> Select build.gradle)
When prompted exclude the spring-aspects module (or after the import via File-> Project Structure -> Modules)
Code away

gradlew :spring-oxm:compileTestJava

第一步 gradlew :spring-oxm:compileTestJava


导入到IDEA

选择你拉取的spring源码,方式选择build.gradle


等待index完成,需要下载jar包 ,不是很快(我用了好几次,耗时好几个小时),如果期间出错,多试几次。


点击刷新按钮,如上所示,基本可以认定为构建成功。

编译和 运行Testst设置为 IntelliJ IDEA (建议)

编译完成后建议 将编译和 运行Testst设置为 IntelliJ IDEA , 默认的Gradle特别慢。


image.png
image.png
C:/Users/artisan/.gradle/wrapper/dists/gradle-6.5.1-bin/b4shxvjcpfsjxpjuxxiaa7cyh/gradle-6.5.1

这个地方选OPEN JDK11 会有问题,请见问题记录

测试验证

新建子模块

image.png

选择 gradle


随便建立一个子module的名称

添加依赖

compile(project(":spring-context"))
[图片上传中...(image.png-bbb819-1602556907727-0)]

编写代码 加载Bean测试


image.png
package com.artisan;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.artisan")
public class AppConfig {
}
package com.artisan;

import org.springframework.stereotype.Component;

@Component
public class ArtisanService {
}
package com.artisan;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class ArtisanTest {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
        ArtisanService bean = ac.getBean(ArtisanService.class);
        System.out.println(bean.getClass().getName());
    }
}

运行测试

image.png

至此,spring5.1.x的源码编译导入idea就算完成了。

Gradle 下载的依赖包存储的位置

  • Windows系统默认下载到:C:\Users(用户名).gradle\caches\modules-2\files-2.1
  • Mac系统默认下载到:/Users/(用户名)/.gradle/caches/modules-2/files-2.1

问题

D:\workspace\spring-framework\spring-core\src\main\java\org\springframework\core\metrics\jfr\FlightRecorderStartupEvent.java:19:15
java: 程序包jdk.jfr不存在

最开始使用的OpenJDK jdk-11.0.8.10-hotspot 更换JDK为 OpenJDK8U-jdk_x64_windows_hotspot_8u265b01

【1】gradle JVM


image.png

【2】 项目编译环境 的JDK


image.png
image.png

重新编译,即可解决。

你可能感兴趣的:(Spring5源码 - 构建源码环境)