spring项目根目录有gradlew.bat 文件,可以直接点击gradlew.bat
文件进行编译,但推荐你cmd命令行输入gradlew.bat
查看编译状态
spring官网支持gradle 编译,首先从github上拉取自己需要的spring分支https://github.com/spring-projects/spring-framework(我的spring-framework 分支版本是:v5.1.12.RELEASE ) ,建议使用git clone 拉取。
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
BUILD FAILED in 8m 55s
Build scan background action failed.
org.gradle.process.internal.ExecException: Process 'command 'git'' finished with non-zero exit value 128
at org.gradle.process.internal.DefaultExecHandle$ExecResultImpl.assertNormalExitValue(DefaultExecHandle.java:417)
at org.gradle.process.internal.DefaultExecAction.execute(DefaultExecAction.java:38)
at org.gradle.process.internal.DefaultExecActionFactory.exec(DefaultExecActionFactory.java:168)
at io.spring.ge.conventions.gradle.WorkingDirectoryProcessOperations.exec(WorkingDirectoryProcessOperations.java:45)
at io.spring.ge.conventions.gradle.ProcessOperationsProcessRunner.run(ProcessOperationsProcessRunner.java:41)
at io.spring.ge.conventions.core.BuildScanConventions.run(BuildScanConventions.java:166)
at io.spring.ge.conventions.core.BuildScanConventions.addGitMetadata(BuildScanConventions.java:113)
at io.spring.ge.conventions.gradle.GradleConfigurableBuildScan.lambda$background$0(GradleConfigurableBuildScan.java:104)
at com.gradle.enterprise.gradleplugin.internal.extension.b$3.run(SourceFile:101)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
git clone 后不再报该错误。
选择编译需要的gradle版本,判断依据项目目录内的gradle-wrapper.properties配置。(图片为已编译完成的项目结构)
注: 这里是为了说明如何确认spring 分支gradle版本 ,可忽略分支版本差异
主要看distributionUrl 值,distributionUrl=https\://services.gradle.org/distributions/gradle-6.5.1-bin.zip
因此需要下载6.5.1的版本。
gradle和spring 版本一定要对的上,否则gradlew.bat 执行失败:Caused by: org.gradle.internal.resource.transport.http.HttpErrorStatusCodeException: Could not HEAD 'https://repo.spring.io/plugins-release/io/spring/gradle-enterprise-conventions/io.spring.gradle-enterprise-conventions.gradle.plugin/0.0.2/io.spring.gradle-enterprise-conventions.gradle.plugin-0.0.2.jar'. Received status code 401 from server: Unauthorized
命令行 执行gradlew.bat 看详细报错命令:gradlew.bat --stacktrace
Downloading https://services.gradle.org/distributions/gradle-6.8.3-bin.zip
..........10%..........20%..........30%...........40%..........50%..........60%..........70%...........80%..........90%..........100%
Welcome to Gradle 6.8.3!
Here are the highlights of this release:
- Faster Kotlin DSL script compilation
- Vendor selection for Java toolchains
- Convenient execution of tasks in composite builds
- Consistent dependency resolution
For more details see https://docs.gradle.org/6.8.3/release-notes.html
Starting a Gradle Daemon (subsequent builds will be faster)
FAILURE: Build failed with an exception.
* Where:
Build file 'C:\Study\spring\spring-framework\build.gradle' line: 305
* What went wrong:
Could not compile build file 'C:\Study\spring\spring-framework\build.gradle'.
> startup failed:
build file 'C:\Study\spring\spring-framework\build.gradle': 305: unexpected token: } @ line 305, column 1.
}
^
1 error
* 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
BUILD FAILED in 1m 52s
A build scan was not published as you have not authenticated with server 'ge.spring.io'.
修改前:
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
// 下载gradle-6.5.1-bin 版本
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
修改配置文件
distributionBase=GRADLE_USER_HOME
// 执行gradlew.bat编译命令后 生成的文件存放路径
distributionPath=wrapper/dists
// 改成自己下载到本地的gradle压缩包地址
distributionUrl=file\:///C\:/SofeFile/gradle-6.5.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl
修改为自己已下载的gradle压包地址,后面执行编译命令 会从C:/SofeFile/gradle-6.5.1-bin.zip 下载gradle
C\:/SofeFile/gradle-6.5.1-bin.zip
下载gradle-6.5.1版本,只包含binary的版本。C:\Users\\.gradle\wrapper\dists
目录中。C:\Users\\.gradle\wrapper\dists\gradle-3.1-bin\\
,gradle-6.5.1-bin目录是根据下载的gradle的文件名来定的,
目录名是根据distribution url路径字符串计算md5值得来的,具体参考PathAssembler.java中的rootDirName()和getHash(),PathAssembler.java的位置见本文最后的参考路径)例如me:用户名为哈哈
C:\Users\哈哈\.gradle\wrapper\dists
,
目录名为98dayp9c8wecb8izhag7k5iak
C:\Users\哈哈\.gradle\wrapper\dists\gradle-6.5.1-bin\98dayp9c8wecb8izhag7k5iak
执行gradlew.bat 后,会从file\:///C\:/SofeFile/gradle-6.5.1-bin.zip
下载gradle 到C:\Users\哈哈\.gradle\wrapper\dists\gradle-6.5.1-bin\98dayp9c8wecb8izhag7k5iak
目录,并解压
选择正确的版本后
执行gradlew.bat
后,扫描 自动下载完gradle之后会通过gradle去编译spring源码;这里就涉及到一个gradle的中央仓库的问题了,编译过程中会下载大量的依赖;如果你采用默认的依赖那么这个编译过程一定特别漫长;而且会失败;于是我们需要修改中央仓库为国内的阿里云;这样就会快很多;
修改build.gradle 文件
repositories {
mavenCentral()
maven { url "https://repo.spring.io/libs-spring-framework-build" }
maven { url "https://repo.spring.io/milestone" } // Reactor
// 添加下面两个
maven {url 'https://maven.aliyun.com/nexus/content/groups/public/'} //阿里云
maven {url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}
}
plugins {
id "com.gradle.enterprise" version "3.2"
// 注解后,就不会再进行身份验证了
// id "io.spring.gradle-enterprise-conventions" version "0.0.2"
}
A build scan was not published as you have not authenticated with server ‘ge.spring.io’. 报错
所有的操作都执行后,就可以直接编译spring源码啦,同上 cmd命令行输入 gradlew.bat 。
篇幅太长 后面的内容在下一篇编译spring源码(二)
参考网址:
https://blog.csdn.net/qq_46119030/article/details/114229263