可以选择自己的版本,官网现在给的版本1.8的好像就只要8u241了(日期2020.2.24)
jdk8u241
配置环境变量此处不再赘述自己百度
我的建议是,先下载Spring-framework5.1(下载5.的操作一样)
github地址: (慢)
码云地址: (快)
根据源码确定gradle环境
打开你下载的目录\spring-framework-5.1.x\gradle\wrapper\gradle-wrapper.properties
:
由此确定我的gradle版本,但是你可以把这里的版本改一下,比如你的版本是gradle-4.9.1-bin.zip:
那你就修改:distributionUrl=https\://services.gradle.org/distributions/gradle-4.9.1-bin.zip
看见一行和第四行的信息了吗?distributionBase=GRADLE_USER_HOME
和zipStoreBase=GRADLE_USER_HOME
, 我看好多教程上这个环境变量都没有配置,但是这里一定要配置GRADLE_USER_HOME
就是等会要下载的依赖文件的位置,文件的具体位置在GRADLE_USER_HOME/wrapper/dists/
确定gradle版本之后那就下载吧:
官网(慢):
下载地址(快):
根据版本确定你自己下载的,官网上的版本比较全,找不到去官网找,我的就是在官网上找到的gradle-4.10.3-bin.zip
下载解压
GRADLE_HOME
C:\gradle-4.10.3 ## 修改为自己的路径
GRADLE_USER_HOME
C:\gradle-4.10.3\.gradle
%GRADLE_HOME%\bin
%GRADLE_USER_HOME%\bin
查看环境变量是否更改成功
win+R
->cmd-
>输入gradle -v
:显示如下信息
allprojects{
repositories {
def REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public/'
all { ArtifactRepository repo ->
def url = repo.url.toString()
if ((repo instanceof MavenArtifactRepository) && (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
}
}
}
先不急导入,简单配置源码,让版本保持一致并且下载的速度更快。
gradle clean
运行,直到看到BUILD SUCESSFUL
查看便签1
编辑源码根目录下的build.gradle
修改文件
spring-framework-5.1.x\gradle\docs.gradle
* What went wrong:
Failed to capture snapshot of input files for task ':distZip' property 'rootSpec$1$3' during up-to-date check.
> Failed to create MD5 hash for file 'D:\repository\spring-framework-5.0.8.RELEASE\build\distributions\spring-framework-5.0.8.RELEASE-schema.zip'.
* 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 43s
254 actionable tasks: 3 executed, 251 up-to-date
参考如下代码,注释部分是原有的代码:
//task schemaZip(type: Zip) {
// group = "Distribution"
// baseName = "spring-framework"
// classifier = "schema"
// description = "Builds -${classifier} archive containing all " +
// "XSDs for deployment at http://springframework.org/schema."
// duplicatesStrategy 'exclude'
// moduleProjects.each { subproject ->
// def Properties schemas = new Properties();
//
// subproject.sourceSets.main.resources.find {
// it.path.endsWith("META-INF/spring.schemas")
// }?.withInputStream { schemas.load(it) }
//
// for (def key : schemas.keySet()) {
// def shortName = key.replaceAll(/http.*schema.(.*).spring-.*/, '$1')
// assert shortName != key
// File xsdFile = subproject.sourceSets.main.resources.find {
// it.path.endsWith(schemas.get(key))
// }
// assert xsdFile != null
// into (shortName) {
// from xsdFile.path
// }
// }
// }
//}
task schemaZip(type: Zip) {
group = "Distribution"
baseName = "spring-framework"
classifier = "schema"
description = "Builds -${classifier} archive containing all " +
"XSDs for deployment at http://springframework.org/schema."
duplicatesStrategy 'exclude'
moduleProjects.each { subproject ->
def Properties schemas = new Properties();
subproject.sourceSets.main.resources.find {
it.path.endsWith("META-INF\\spring.schemas")
}?.withInputStream { schemas.load(it) }
for (def key : schemas.keySet()) {
def shortName = key.replaceAll(/http.*schema.(.*).spring-.*/, '$1')
assert shortName != key
File xsdFile = subproject.sourceSets.main.resources.find {
it.path.endsWith(schemas.get(key).replaceAll('\\/','\\\\'))
}
assert xsdFile != null
into (shortName) {
from xsdFile.path
}
}
}
}
gradle.properties
,修改如下:org.gradle.daemon=true
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
org.gradle.parallel=true
org.gradle.configureondemand=true
若是你在gradle可能配置错了,导致不能从阿里云远程仓库下载,没关系,我们这里在本项目下配置远程仓库链接,原则是:本地项目配置>用户环境配置
根目录下的build.gradle
:
添加信息
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
File -> New -> Project from Existing Sources -> 找到选择spring-framework源码 -> 选择build.gradle
idea配置gradle: File->setting->gradle
便签1:gradle -v
选择JDK
选择对应的JDK
现在可以下载依赖了
下载依赖,等待时间取决你的网速。下载完成标志:
依赖下载完了根据根目录下的介绍import-into-idea.md
Precompile spring-oxm with ./gradlew :spring-oxm:compileTestJava
Precompile spring-oxm with ./gradlew :spring-oxm:compileTestJava
使用cmd到spring源码下:执行gradlew :spring-oxm:compileTestJava
。这样其实出现的错误不好解决,因为版本信息不好确定的。When prompted exclude the spring-aspects module (or after the import via File-> Project Structure -> Modules)
这个步骤简而言之就是去掉该moudle, 鼠标右键spring-aspects,操作如下:没有删除该文件,只是从Spring项目中移除这个模块。在源码项目new一个moudle,类型为gradle的管理的java项目即可
选择对应的JDK版本(8u241)
项目创建完成后
该项目下build.gradle添加compile project(":spring-context")
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile project(":spring-context")
}
UserDao.java
package com.wjh.dao;
import org.springframework.stereotype.Repository;
@Repository("dao")
public class UserDao {
public void pringInfo(){
System.out.println("User dao");
}
}
SpringConfig.java
package com.wjh.anno;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com")
public class SpringConfig {
}
Test.java
package com.wjh.test;
import com.wjh.anno.SpringConfig;
import com.wjh.dao.UserDao;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Test {
public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
UserDao dao = (UserDao) annotationConfigApplicationContext.getBean("dao");
dao.pringInfo();
}
}
运行Test.java,出现下图即证明spring5.1.x项目完美部署成功!
网上有的教程要求:spring-bean
项目下的spring-beans.gradle
配置下需要注释
这里不需要,若注释掉,会报错误
Circular dependency between the following tasks:
:spring-beans:compileGroovy
\--- :spring-beans:compileJava
\--- :spring-beans:compileKotlin
\--- :spring-beans:compileGroovy (*)
Spring5.x 源码环境搭建
源码学习:IDEA成功导入、编译、调试SPRING5源码项目
spring源码阅读环境(几分钟下载包)
使用idea和gradle编译spring5源码
idea2019.2最快捷编译spring5.0.x源码
IntelliJ IDEA 统一设置编码为utf-8编码