Spring源码 如何用Gradle编译Spring源码并导入IDEA

准备工作

  原本以为不就是编译个源码么,想不到还是折腾了挺久的,记录一下吧。

  1.IDEA版本 2017.1.2

  2.gradle版本 4.5

  3.Spring framework 版本 5.0.X

安装gradle

  因为spring源码是gradle项目,第一步就需要下载安装gradle

  这里下载4.5版本的

  我刚开始没注意,下载了最新版的,竟然不能用,后来才知道gradle版本需要跟IDE也就是IDEA里面自带的gradle插件版本相匹配,我这里需要用4.5以上的,如果遇到了版本问题,解决思路就是哪个旧就升级哪个,我后面升级了IDEA...2017太旧了,玩不了

  言归正传,gradle下载地址: https://gradle.org/releases/ 

  选到4.5 complete下载

  下载后像配置maven一样配置gradle,将gradle解压出来放到合适的位置,配置环境变量,我是mac,直接贴一下,看懂就行:

export GRADLE_HOME=/Users/garfield/softwares/gradle-4.5
export GRADLE_USER_HOME=/Users/garfield/.gradle
export PATH=$PATH:$GRADLE_HOME/bin

  配置完后,命令试一下:

sts-MacBook-Pro:IntelliJIdea2019.3 garfield$ gradle -v

------------------------------------------------------------
Gradle 4.5
------------------------------------------------------------

Build time:   2018-01-24 17:04:52 UTC
Revision:     77d0ec90636f43669dc794ca17ef80dd65457bec

Groovy:       2.4.12
Ant:          Apache Ant(TM) version 1.9.9 compiled on February 2 2017
JVM:          1.8.0_144 (Oracle Corporation 25.144-b01)
OS:           Mac OS X 10.12.6 x86_64

检出源码

  checkout from git 地址: https://github.com/spring-projects/spring-framework.git

  检下来以后把版本切到5.0.x

  也可以直接下载源码zip包,然后导入,一样的。

  

 IDEA配置gradle

  【IntelliJ idea】-【Preference】-【Build Execution Deployment】-【Build Tools】-【gradle】 如下配置:

  

编译

   配置完后打开源码目录,找到import-into-idea.md 这是提供的编译说明如下:

## 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`)    

  可以看到他要我们干两件事,如下执行两个命令:

./gradlew :spring-oxm:compileTestJava
./gradlew :spring-core:compileTestJava

  做完这个后就可以开始编译了,运行build,这时候可能遇到如下几个问题,也是本文真正想记录的:

问题

问题一

Error:No such property: GradleVersion for class: JetGradlePlugin

  这个问题比较简单,就是版本不匹配,根据自己idea的版本下载正确的gradle版本,这里2017可以不能匹配4.10以上的,要注意。

问题二

Error:(30, 0) No such property: values for class: org.gradle.api.internal.tasks.DefaultTaskDependency
Possible solutions: values
Open File(spring-beans\spring-beans.gradle )

 点击:Open File 然后注释掉

//compileGroovy.dependsOn = compileGroovy.taskDependencies.values - ‘compileJava’

问题三 

Circular dependency between the following tasks:
:spring-beans:compileGroovy
\--- :spring-beans:compileJava
\--- :spring-beans:compileKotlin
\--- :spring-beans:compileGroovy (*)

  该问题则注释掉上一个问题的下面一行,也就是spring-beans.gradle文件的倒数第二行

//compileGroovy.dependsOn = compileGroovy.taskDependencies.values - "compileJava"
//compileKotlin.dependsOn(compileGroovy)
compileKotlin.classpath += files(compileGroovy.destinationDir)

问题四

  这个问题需要描述一下,理论上到这里就算成功了,但是我的idea一直报一个错

Error:Unable to make the module: spring-framework, related gradle configuration was not found. Please, re-import the Gradle project and try again.

  这个错误按照网上朋友的说法需要点击刷新按钮

   

  这个方法确实可以 解决编译问题,但是当我想rebuild或者新建项目运行时,他又冒出来了,是的,阴魂不散,这个问题经过探究怀疑,是特么我的IDEA版本问题,说到底还是gradle插件的问题,于是忍无可忍,我将idea跟新到2019 解决了这个问题。 

新建工程

  【file】-【new】-【module】

  

   如果遇到新建的工程里面没有src目录,则需要骚操作,

  在build.gradle 文件中添加如下代码

task "create-dirs" << {
 sourceSets*.java.srcDirs*.each {
  it.mkdirs()
 }
 sourcScts*.resources.srcDirs*.each{
  it.midirs()
 }
}

  添加完后,build 然后点击他:

  

   然后就可以看到src了,编写代码吧

猜你喜欢

转载自www.cnblogs.com/garfieldcgf/p/12591157.html