Spring 源码环境搭建—— IDEA 版

Spring 源码环境搭建—— IDEA 版

  • 说明
  • 环境搭建步骤
    • 下载源码
    • 安装 Gradle
    • 在 IDEA 中导入 Spring 项目
  • 可能出现的异常与解决方案
  • 可能出现的单元测试失败问题
  • 参考博客

说明

  • Spring 版本
    • Spring 源码环境搭建—— IDEA 版_第1张图片
  • gradle 版本
    • Spring 源码环境搭建—— IDEA 版_第2张图片

环境搭建步骤

下载源码

  • 从 GitHub 下载源码到本地指定目录。(略)

安装 Gradle

  • 下载 Gradle 安装包,解压并配置环境变量。(略)

在 IDEA 中导入 Spring 项目

  • IDEA 导入说明文档import-into-idea.md
  • The following has been tested against IntelliJ IDEA 2016.2.2
    
    ## Steps
    
    _Within your locally cloned spring-framework working directory:_
    
    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)
    3. When prompted exclude the `spring-aspects` module (or after the import via File-> Project Structure -> Modules)
    4. Code away
    
    ## Known issues
    
    1. `spring-core` and `spring-oxm` should be pre-compiled due to repackaged dependencies.
    See `*RepackJar` tasks in the build and https://youtrack.jetbrains.com/issue/IDEA-160605).
    2. `spring-aspects` does not compile due to references to aspect types unknown to
    IntelliJ IDEA. See https://youtrack.jetbrains.com/issue/IDEA-64446 for details. In the meantime, the
    'spring-aspects' can be excluded from the project to avoid compilation errors.
    3. While JUnit tests pass from the command line with Gradle, some may fail when run from
    IntelliJ IDEA. Resolving this is a work in progress. If attempting to run all JUnit tests from within
    IntelliJ IDEA, you will likely need to set the following VM options to avoid out of memory errors:
        -XX:MaxPermSize=2048m -Xmx2048m -XX:MaxHeapSize=2048m
    4. If you invoke "Rebuild Project" in the IDE, you'll have to generate some test
    resources of the `spring-oxm` module again (`./gradlew :spring-oxm:compileTestJava`)    
    
    
    ## Tips
    
    In any case, please do not check in your own generated .iml, .ipr, or .iws files.
    You'll notice these files are already intentionally in .gitignore. The same policy goes for eclipse metadata.
    
    ## FAQ
    
    Q. What about IntelliJ IDEA's own [Gradle support](https://confluence.jetbrains.net/display/IDEADEV/Gradle+integration)?
    
    A. Keep an eye on https://youtrack.jetbrains.com/issue/IDEA-53476
    
  • 预编译 spring-oxm
    • 在 spring 项目根目录执行命令:./gradlew :spring-oxm:compileTestJava
    • Spring 源码环境搭建—— IDEA 版_第3张图片
  • 导入项目
    • File -> New -> Project from Existing Sources -> 找到 Spring 项目路径 -> 选中 build.gradle
    • Spring 源码环境搭建—— IDEA 版_第4张图片
    • Spring 源码环境搭建—— IDEA 版_第5张图片
  • 排除不参与编译的 module
    • spring-aspects 不参与编译是因为 IDEA 不能识别 aspect 类型,会引起编译错误。 具体查看: https://youtrack.jetbrains.com/issue/IDEA-64446
    • File -> Project Structure -> Modules
    • Spring 源码环境搭建—— IDEA 版_第6张图片
  • 构建项目:
    • 第一次同步时,由于需要下载的文件非常多,耗时略长(博主花了 20 分钟左右)
    • Spring 源码环境搭建—— IDEA 版_第7张图片

可能出现的异常与解决方案

  • 异常:Cause: org.jetbrains.plugins.gradle.tooling.util.ModuleComponentIdentifierImpl.getModuleIdentifier()Lorg/gradle/api/artifacts/ModuleIdentifier;
  • 解决方案:更换 Gradle 版本。这个异常是 IDEA 和 Gradle 版本不兼容引起的。博主的 IDEA是 2018.1 版本,使用当前最新的 gradle(5.4.1) 版本时会报这个异常,因此博主把 gradle 版本替换为 4.6 版本。

可能出现的单元测试失败问题

  • 构建成功,但是运行 spring-beans 下某个单元测试报错:
    • Spring 源码环境搭建—— IDEA 版_第8张图片
  • 解决方法
    • 对 spring(root)项目执行 clean 、 build 任务
      • rebuild 之前,需要重新执行预编译:./gradlew :spring-oxm:compileTestJava

参考博客

  • https://blog.csdn.net/change987654321/article/details/86649310
  • https://blog.csdn.net/u011976388/article/details/80356808
  • https://blog.csdn.net/evan_leung/article/details/81714604

你可能感兴趣的:(IDEA,Spring)