编译spring-framework源码


这篇文章来源是源于最近在学习spring源码,就从github上下载了源码,进行导入到idea,期间遇到很多问题,故作为记录
编译期间遇到很多问题,最终总结出此文档,希望帮助到有需要的人,如果有用,记得评论区回复

步骤

  1. 下载源码,spring5最新源码

  2. 修改gradle配置

  3. 导入IDEA

  4. 新建自己的module

工具

  • git客户端

  • jdk8

  • idea2019

  • gradle5.6.4 地址 https://gradle.org/releases/ 选择 binary-only版本即可,可以自行百度配置gradle环境变量

下载源码

  • spring-framework在github源码地址:

    https://github.com/spring-projects/spring-framework

  • 使用git客户端可以把源码下载到本地硬盘

    git clone https://github.com/spring-projects/spring-framework.git

  • 遇到问题
  1. github非国内地址,且无国内镜像,下载很慢,只有十几K,国内也有类似github的网站,如码云,下载会快很多

提供一个码云地址


    https://gitee.com/wuchenliang/spring-framework.git


    git clone https://gitee.com/wuchenliang/spring-framework.git

修改spring源码 gradle配置

  1. 临时去除 spring-aspects 模块

  2. 修改 maven 仓库地址

在源码跟目录

  • ./settings.gradle 第1行

pluginManagement {

repositories {

//添加阿里云仓库,提高编译速度

maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }

maven { url 'https://maven.aliyun.com/repository/spring' }

//网上很多文章是注释掉一下仓库的,但是注释掉会遇到有些jar包在阿里镜像不存在,仍然编译失败,因此只是增加阿里镜像地址

gradlePluginPortal()

maven { url 'https://repo.spring.io/plugins-release' }

}

}

apply from: "$rootDir/gradle/build-cache-settings.gradle"

include "spring-aop"

// 去除 aspects 模块

//include "spring-aspects"

include "spring-beans"

include "spring-context"

  • ./build.gradle 第 281 行

repositories {

              //添加阿里云仓库,提高编译速度

maven { url 'https://maven.aliyun.com/repository/central' }

maven { url 'https://maven.aliyun.com/repository/spring' }

maven { url "https://repo.spring.io/snapshot" }

//网上很多文章是注释掉一下仓库的,但是注释掉会遇到有些jar包在阿里镜像不存在,仍然编译失败,因此只是增加阿里镜像地址

mavenCentral()

maven { url "https://repo.spring.io/libs-spring-framework-build" }

}

  • buildSrc/build.gradle 第 5 行

repositories {

maven { url 'https://maven.aliyun.com/repository/central' }

maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }

mavenCentral()

gradlePluginPortal()

}

源码导入IDEA


设置项目环境


spring官方编译指导


// 先编译 spring-oxm

1. Precompile `spring-oxm` with `./gradlew :spring-oxm:compileTestJava`

2. Import into IntelliJ (File -> New -> Project from Existing Sources -> Navigate to directory -> Select build.gradle)

// 排除 spring-aspects,编译完spring-oxm与spring-core后将注释打开,重新编译整个工程

3. When prompted exclude the `spring-aspects` module (or after the import via File-> Project Structure -> Modules)

// spring-core 与 spring-oxm

需要首先编译

4. `spring-core ` and `spring-oxm` should be pre-compiled due to repackaged dependencies.

开始编译

编译spring-oxm

  • 选择命令行操作或者gradle工作群点击命令都可以,执行module的测试代码,帮我们下载依赖
  • 如下图表示编译成功


编译spring-core

  • 与 编译spring-oxm 操作相同

编译 spring-aspects

  • 修改./settings.gradle 将我们注释的 spring-aspects 释放
  • 与 编译spring-oxm 操作相同,执行测试脚本进行编译

编译 spring-context

  • 与 编译spring-oxm 操作相同
  • 这个模块也是我们常用模块,因此都编译下

其他模块,有需要可以都编译下,操作相同

编译成功的工程展示

FAQ

编译报错,提示jar包或maven仓库超时

  • 是由于没有修改maven仓库地址为阿里云导致,国外仓库,国内访问会出现超时

编译太慢

  • 可以重新看下操作,设置下gradle配置,提高编译效率

如果参考此文档还是有问题,可以评论区留言

下次增加个module试下

你可能感兴趣的:(编译spring-framework源码)