原创

spring源码编译踩坑

事情的起因是电脑重装系统,导致原先的环境都不适用,于是乎从新下载spring源码然后编译它,之前提到怎么去编译,但是现在使用那种方法却不能编译成功!于是再记录下踩坑。

之前的踩坑文章:spring-framework 5.x源代码编译

好多一大部分原因都是jar包下载不下来的问题,这里也把它提到第一位来解决。

解决编码问题

spring源码默认的编码是GBK,因此,我们如果在源码里写中文运行的时候就会报错。

更换Gradle的maven仓库(阿里云镜像)

参考文章:Spring5.1源码编译
配置好Gradle的环境变量后,找到安装Gradle的安装目录找到 init.d , 在下面创建文件名称为init.gradle 的文件,如下图:

然后将下面的内容复制进去

allprojects{
   repositories {
       def REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public/'
       all { ArtifactRepository repo ->
           if(repo instanceof MavenArtifactRepository){
               def url = repo.url.toString()
               if (url.startsWith('https://repo1.maven.org/maven2') || url.startsWith('https://jcenter.bintray.com/')) {
                   project.logger.lifecycle "Repository ${repo.url} replaced by $REPOSITORY_URL."
                   remove repo
               }
           }
       }
       maven {
           url REPOSITORY_URL
       }
   }
}

注意:将这里改了就不用修改build.gradle里的仓库地址了

错误一

* What went wrong:
A problem occurred evaluating project ':spring-beans'.
> No such property: values for class: org.gradle.api.internal.tasks.DefaultTaskDependency
  Possible solutions: value

如下图所示

参考文章:记录使用gradle编译spring源码时发生的错误

错误二

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

或者是这个错误

spring-beans\src\main\kotlin\org\springframework\beans\factory\BeanFactoryExtensions.kt: (35, 30): Unresolved reference: BeanFactory

修改spring-beans.spring-beans.gradle文件
修改最后三行为如下:

def deps = compileGroovy.taskDependencies.immutableValues + compileGroovy.taskDependencies.mutableValues
compileGroovy.dependsOn = deps - "compileJava"
compileKotlin.dependsOn(compileGroovy)
compileKotlin.classpath += files(compileGroovy.destinationDir)

参考文章:spring5.0.x源码编译完后,spring-beans项目运行compileTestJava报循环依赖错误,怎么解决?

错误三

> Task :spring-oxm:genJaxb
[ant:javac] : warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds

> Task :spring-beans:compileGroovy FAILED
D:\work_space\spring-framework\spring-beans\src\main\java\org\springframework\beans\support\PropertyComparator.java:138: 错误: 不兼容的类型: PropertyComparator<CAP
#1>无法转换为Comparator<? super CAP#1>
                        source.sort(new PropertyComparator<>(sortDefinition));
                                    ^
  其中, CAP#1是新类型变量:
    CAP#1从?的捕获扩展Object
注: 某些消息已经过简化; 请使用 -Xdiags:verbose 重新编译以获得完整输出
1 个错误
startup failed:
Compilation failed; see the compiler error output for details.

1 error

Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. Windows RegCreateKeyEx(...) returned error code 5.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':spring-beans:compileGroovy'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.4.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 29s
28 actionable tasks: 17 executed, 11 from cache

解决方法:
点击错误类,会直接跳转到该方法,然后直接返回值List泛型去掉,如下图

参考文章:spring源码编译,报错无法转换为Comparator

其他设置

我这里用的是JDK1.8


修改成我们配置的gradle

错误四

Could not find org.jetbrains.dokka:integration:0.9.15

导包继续时无法找到该包,所以我们要修改该包版本号,解决办法如下:
1 修改build.gradle文件,将id “org.jetbrains.dokka” version “0.9.15” 修改为id “org.jetbrains.dokka” version “0.9.17”
2 repositories中添加maven { url “https://plugins.gradle.org/m2/“ },问题解决了,可以下载新版本的jar包
我能想到的也就这么多了,希望大家都能编译成功!

正文到此结束(点击广告是对作者最大的支持)