环境准备:
1.idea2020.3
2.jdk11
3.gradlegradle-6.8.1
4.spring源码(版本:spring-framework-5.2.10.RELEASE)
一、spring源码下载
1、下载源码
Git链接:[https://github.com/spring-projects/spring-framework
Gitee链接:[https://gitee.com/mirrors/Spring-Framework.git
2、下载gradle(参考:二、配置->1、gradle配置)
gradle入门
gradle入门(二)——gradle配置及grovvy简单语法
gradle的安装
1.解压刚刚下载的gradle压缩包
2.打开电脑的环境变量,配置GRADLE_HOME ,如下图:
3.在系统变量 path中加入:%GRADLE_HOME%\bin
4.gradle也是需要本地仓库的,要配置一个本地仓库。在本地磁盘中建一个文件夹,然后在系统变量中添加一个GRADLE_USER_HOME,然后把文件夹路径添加进去就行了,如下图:
5.测试gradle是否安装完成:在cmd输入gradle -v如果出现版本号这些就算是安装成功了。
————————————————
原文链接:https://blog.csdn.net/qq_45750556/article/details/113244361
二、配置
1、gradle版本配置
因为我们自己配置了gradle,所以需要修改gradle\wrapper\gradle-wrapper.properties文件内容,让它使用我们自己配置的gradle,要不然它还是会去远程下载。
注意:本地下载的gradle版本最好和gradle-wrapper.properties中的一致
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
#distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.3-bin.zip
distributionUrl=file\:///D\:/Dev_Environment/gradle/gradle-5.6.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
将distributionUrl修改成我们自己的gradle压缩包路径,如下图:
distributionUrl=file\:///d\:/javaTools/gradle-5.6.4-bin/gradle-5.6.4/gradle-5.6.4-bin.zip
file:///D:/grade/gradle-6.5-all.zip
2、build.gradle配置
原文件是这样:
2.找到build.gradle文件并打开,注释掉一行代码(我反正没有注释编译不会成功),如下图:
同样在build.gradle文件中找到repositories部分,添加阿里云镜像,这样编译会更快,如下图:
mave n{ url "https://maven.aliyun.com/nexus/content/groups/public/" }
maven { url "https://maven.aliyun.com/nexus/content/repositories/jcenter" }
maven { url "http://repo.springsource.org/plugins-release" }
3、settings.gradle配置
找到settings.gradle文件并打开,同样找到找到repositories部分,添加下面代码
maven { url "https://maven.aliyun.com/nexus/content/groups/public/" }
或者
maven { url "https://maven.aliyun.com/repository/public" }
改变成11
完成后可以先跑 spring 自带的测试用例
参考链接:
https://blog.csdn.net/yuyue_999/article/details/109313178
https://blog.csdn.net/qq_45750556/article/details/113244361
4、idea中kotlin配置
三、预编译
可以打开源码所在文件夹,在windows cmd命令中输入:
gradlew :spring-oxm:compileTestJava
也可以在idea中编译
ps:此处代完善
四、导入项目遇见问题
1.注释build.gradle第3行
Build file 'D:\Dev_IdeaProjects\LAGOU\spring-framework-5.2.10.RELEASE\build.gradle' line: 3
Error resolving plugin [id: 'io.spring.gradle-enterprise-conventions', version: '0.0.2']
> Could not resolve all dependencies for configuration 'detachedConfiguration2'.
> Could not determine artifacts for io.spring.gradle-enterprise-conventions:io.spring.gradle-enterprise-conventions.gradle.plugin:0.0.2
> Could not get resource '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'.
> 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
2.注释build.gradle第11行
Build file 'D:\Dev_IdeaProjects\LAGOU\spring-framework-5.2.10.RELEASE\build.gradle' line: 11
An exception occurred applying plugin request [id: 'com.gradle.build-scan', version: '3.2']
> Failed to apply plugin 'com.gradle.build-scan'.
> The build scan plugin is not compatible with Gradle 6.0 and later.
Please use the Gradle Enterprise plugin instead.
3.注释掉spring-module.gradle第68行
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-E3FS4Wjl-1614606803342)(C:\Users\niuqingyi\AppData\Roaming\Typora\typora-user-images\image-20210227161046305.png)]
4.kotlin编译导致的问题
编译github的最新spring源码中发现该问题,主要是kotlin版本不一致导致的,这里要记得修改成匹配对应的版本
org.gradle.api.CircularReferenceException: Circular dependency between the following tasks:
:spring-beans:compileGroovy
\--- :spring-beans:compileJava
\--- :spring-beans:compileKotlin
\--- :spring-beans:compileGroovy (*)
五、搭建java项目问题
1、IntelliJ idea 解决 junit no tasks available 的问题
参考链接:https://blog.csdn.net/qq_15192373/article/details/103438678
2、Task ‘wrapper’ not found in project ‘😗**’.
在settings.gradle中include自己的项目,重新compileTestJava即可
3、java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
在自定义module的build.gradle中添加以下内容,重新编译compileTestJava,如果不能使用,多次重启再编译
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
testImplementation group: 'org.hamcrest', name: 'hamcrest-core', version: '1.3'
}
参考链接:https://blog.csdn.net/qq_33229669/article/details/80590770
hamcrest配置获取路径:https://mvnrepository.com/
六、搭建web项目问题
1、Error running ‘Tomcat 8.5.61’: Can’t find catalina.jar
自己把添加的tomcat文件挪地方了,在idea中添加tomcat的时候没有改,导致如上错误。
控制台乱码问题
2、tomcat启动控制台乱码问题
在idea的tomcat配置中添加utf-8配置
-Dfile.encoding=UTF-8
3、tomcat启动项目后访问链接不是http://localhost:8080/
tomcat添加war包要用上面的
3、url链接是http://localhost:8080/…_war/,很长但不是
application Context后配置为 / ,即可为http://localhost:8080/
七、测试module搭建
1、案例一
注意:有其他文件可以参考(五、打击那java项目问题)
1.点击项目名,右击鼠标新建一个module(new->model->gradle–>输入模块名称),注意选择gradle。随便命名。
2.给自己新添加的模块添加依赖:
compile(project(":spring-context"))
3.随意添加任意bean:
package cn.ccb.beans;
import org.springframework.stereotype.Service;
/**
* @auther ccb
* 2021/1/27 10:47
* @Slogan: 可以写不出一行高深的代码,但是写出的每一行代码必须规范
* @描述:
*/
@Service
public class UserServiceImpl {
public void sayHello(){
System.out.println("你好呀spring!");
}
}
4.添加启动配置类:
package cn.ccb.beans;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @auther ccb
* 2021/1/27 10:48
* @Slogan: 可以写不出一行高深的代码,但是写出的每一行代码必须规范
* @描述:
*/
@Configuration
@ComponentScan("cn.ccb")
public class MainStat {
public static void main(String[] args) {
ApplicationContext context=new AnnotationConfigApplicationContext(MainStat.class);
UserServiceImpl bean = context.getBean(UserServiceImpl.class);
bean.sayHello();
}
}
2、案例二
package com.yu;
import org.springframework.stereotype.Component;
/**
* Created by IntelliJ IDEA.
*
* @author IT yu
* description:
* date: 2020/10/27 09:52
*/
@Component
public class User {
public String name = "yuyu";
}
//
package com.yu;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.ComponentScan;
/**
* Created by IntelliJ IDEA.
*
* @author IT yu
* date: 2020/10/27 09:52
*/
@Configurable
@ComponentScan("com.yu")
public class AppConfig {
}
//
package com.yu;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Test {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(AppConfig.class);
User user = (User) context.getBean("user");
System.out.println(user.name);
}
}
完成
八、其他问题
1、tomcat安装配置问题
参考:https://www.cnblogs.com/lpgit/p/10929507.html