微信公众号:我其实目前没有耶
我是一个互联网公司的螺丝钉;
魔术师耿
利用码云gitee创建一个github上的镜像仓库,然后下载或者克隆这个镜像仓库,就是正常的网速了
优点: 是可以下载快点,防止没有科学上网,下载不下来
缺点:很明显,不能更新代码了
然后 git clone 到本地
git clone [email protected]:yanlong10829/spring-framework.git
git clone [email protected]:yanlong10829/spring-boot.git
在使用 git 时,提示
error: unable to create xxxx: Filename too long
解决方案 在该 Clone 的路径下,运行该命令: git config --global core.longpaths true
删除老的 Clone 项目,重新 Clone 即可!
GIT 切换到TAG或BRANCH分支
git查看tag命令 : git tag
git切换到tag
git clone整个仓库后,使用以下命令就可以取得对应tag的代码:
git checkout tag_name
此时git可能会提示你当前处于“detached HEAD” 状态。
因为tag相当于一个快照,不能修改它的代码。需要在tag代码基础上做修改,并创建一个分支:
git checkout -b branch_name tag_name
/e/development/codes/githubCodes/spring-projects/spring-framework
git checkout -b tag-v5.2.5.RELEASE v5.2.5.RELEASE
cd /e/development/codes/githubCodes/spring-projects/spring-boot
git checkout -b tag-v2.2.6.RELEASE v2.2.6.RELEASE
git 常用命令
git切换分支与切换tag命令一样,查看分支命令:
git branch #查看本地分支
git branch -a 查看远程的分支
git checkout -b branch_name origin-branch
下载地址http://services.gradle.org/distributions/
安装配置gradle ,我下载的是gradle-5.6.4 ,因为我下载spring源码时,spring-framework/grade/wrapper/gradle-wrapper.properties里面用的是5.6.4
#Tue May 12 14:00:36 CST 2020
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
配置环境变量 GRADLE_HOME=E:\development\tools\gradle-5.6.4 #自己的路径
Path 编辑 增加 %GRADLE_HOME%\bin
GRADLE_USER_HOME=E:\development\tools\gradle_user_home.gradle #自己的路径
第一步:
idea file->open Project 将spring-framework 导入idea
修改Settings ->Build Tools->Gradle 参数
Gradle user home
Use Gradle from 本地安装的gradle E:/development/tools/gradle-5.6.4
Gradle JVM 指定jdk 版本1.8以上
第二步:
为了加快下载jar包的速度
修改build.gradle 文件的 repsitories, 目的是调整maven仓库的查找顺序,添加aliyun的仓库地址
repositories {
mavenLocal()
maven { url "https://repo.spring.io/libs-spring-framework-build" }
maven { url 'https://maven.aliyun.com/repository/central' }
mavenCentral()
}
注释掉 checkstyle相关的东西,要不然,回头我们自己添加模块,写class类后重新编译spring他会校验,说不符合他们的注释规范
// apply plugin: "checkstyle"
...
// checkstyle("io.spring.javaformat:spring-javaformat-checkstyle:0.0.15")
修改spring-framework/grade/docs.gradle
注释掉dokka 和 asciidoctor,(这俩是和文档生成相关的) 这两个影响编译速度,
/*dokka {
dependsOn {
tasks.getByName("api")
}
doFirst {
configuration {
classpath = moduleProjects.collect { project -> project.jar.outputs.files.getFiles() }.flatten()
classpath += files(moduleProjects.collect { it.sourceSets.main.compileClasspath })
moduleProjects.findAll {
it.pluginManager.hasPlugin("kotlin")
}.each { project ->
def kotlinDirs = project.sourceSets.main.kotlin.srcDirs.collect()
kotlinDirs -= project.sourceSets.main.java.srcDirs
kotlinDirs.each { dir ->
if (dir.exists()) {
sourceRoot {
path = dir.path
}
}
}
}
}
}
outputFormat = "html"
outputDirectory = "$buildDir/docs/kdoc"
configuration {
moduleName = "spring-framework"
externalDocumentationLink {
url = new URL("https://docs.spring.io/spring-framework/docs/$version/javadoc-api/")
packageListUrl = new File(buildDir, "docs/javadoc/package-list").toURI().toURL()
}
externalDocumentationLink {
url = new URL("https://projectreactor.io/docs/core/release/api/")
}
externalDocumentationLink {
url = new URL("https://www.reactive-streams.org/reactive-streams-1.0.1-javadoc/")
}
externalDocumentationLink {
url = new URL("https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/")
}
}
}*/
/*asciidoctor {
baseDirFollowsSourceDir()
configurations 'asciidoctorExt'
sources {
include '*.adoc'
}
outputDir "$buildDir/docs/ref-docs/"
resources {
from(sourceDir) {
include 'images/*', 'css/**', 'js/**'
}
from extractDocResources
}
logDocuments = true
outputOptions {
backends = ["html5", "pdf"]
}
options doctype: 'book', eruby: 'erubis'
attributes([
icons: 'font',
idprefix: '',
idseparator: '-',
docinfo: 'shared',
revnumber: project.version,
sectanchors: '',
sectnums: '',
'source-highlighter': 'highlight.js',
highlightjsdir: 'js/highlight',
'highlightjs-theme': 'github',
stylesdir: 'css/',
stylesheet: 'stylesheet.css',
'spring-version': project.version
])
}*/
第三步:
查看主目录的import-into-idea.md 跑到命令行执行命令
_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
需要预编译spring-oxm 和spring-core,这俩模块是其他模块的基础和依赖
./gradlew :spring-oxm:compileTestJava
./gradlew :spring-core:compileTestJava
第四步:
Gradle视图 -> Tasks ->build -》 build 执行构建
BUILD SUCCESSFUL in 26s
207 actionable tasks: 207 up-to-date
17:07:15: Task execution finished ‘build’.
构建成功
参考的视频:B站上的一个图灵学院杨过老师的公开课《Spring5源码分析与源码环境搭建》
cd /e/development/codes/githubCodes/spring-projects/spring-boot
./mvnw clean install -DskipTests -Pfast
修改spring-boot源码
重新构建,
mvnw.cmd clean install -DskipTests -Pfast
构建成功后debug spring-boot客户端项目,spring-boot项目打上断点
源码构建真费劲,还是因为对gradle不熟悉的原因。
在Settings中的Build Tools -->Gradle 配置参数 不用改什么
new Project --> gradle -->勾上Java -->填写项目name ,Location ;填写 groupId , ArtifactId,Version -->Finish后新窗口打开
自动创建和maven一样的目录结构
src
main
java
resources
test
java
resources
main
webapp
Groovy语言常用语法
Tools --> Groovy Console 调出 groovy编辑窗口
println(1);
println("hello") //可以省略分号
println 12 //可以省略括号
println "我和python差不多"
def i =8 //定义变量
println i
i="abc" // ---类型能变,弱类型语言
println i
List list =["a","b"] //定义 集合类型
list.add("a")
println(list.get(1))
println(list)
Set set = ["a","b","b"] //定义 集合类型
println(set)
//定义 map
def map = ["key":"value","keyaa":"valueaa"]
map.keybb="valuebb" //添加元素
println(map)
println map.get("key")
println(map.keyaa) // 获取元素
//----------------------------------------
println "--------------------------------------------"
//groovy中的闭包
// 闭包? 就是一段代码块,在gradle中主要把闭包当参数使用
//定义一个闭包
def demo = {
println "hello demo"
}
//定义一个方法,方法里面需要闭包类型的参数
def method(Closure closure){
closure()
}
//调用方法method
method(demo)
//定义一个闭包,带参数的
def demo2 = {
v->
println "hello, ${v}"
}
//定义一个方法,方法里面需要闭包类型的参数
def method2(Closure closure){
closure("magic")
}
//调用方法method
method2(demo2)
gradle下载的jar包放在E:\development\tools\gradle_user_home.gradle\caches\modules-2\files-2.1
E:\development\tools\gradle_user_home.gradle是我使用的Gradle user home
我的环境变量里设置GRADLE_USER_HOME=E:\development\tools\gradle_user_home.gradle
build.gradle文件如下:
group 'com.magic'
version '1.0-SNAPSHOT'
apply plugin:"java"
/**
* Tasks -->build -->jar打包路径存放在
* 当前工程的目录下的\build\libs下
* build
* classes #编译的class文件
* libs # 打的jar包
* generated
* temp
* E:\development\codes\githubCodes\spring-projects\magic-grovvy\build\libs
*/
sourceCompatibility=1.8
/*指定所使用的仓库
* mavenCentral() 使用中央仓库,项目中所需要的jar包都会默认从中央仓库下载到本地指定目录
*
* mavenLocal()
mavenCentral()
以上配置表示,先从本地仓库寻找依赖,如果没有再从中央仓库下载,
* mavenCentral()
* 如果只配置中央仓库,表示直接从中央仓库下载jar包,但是如果指定下载的位置已经有了,就不会再次下载了
*
* 我的maven本地仓库路径是 D:\develop\local\repo\repository
* 我的GRADLE_USER_HOME=E:\development\tools\gradle_user_home\.gradle
*
*
* */
repositories {
mavenLocal()
mavenCentral()
}
/*
jar包下载路径 ${GRADLE_USER_HOME}\caches\modules-2\files-2.1
gradle所有的jar包的坐标都在dependencies
* jar 包坐标 group,name,version
* testCompile jar包的作用域
* 如何添加spring依赖
1. 在maven中央仓库去查 https://search.maven.org
- 查到 Gradle Groovy DSL
implementation 'org.springframework:spring-context:5.2.6.RELEASE'
- Gradle Kotlin DSL
implementation("org.springframework:spring-context:5.2.6.RELEASE")
2. 在 中央仓库查 https://mvnrepository.com
// https://mvnrepository.com/artifact/org.springframework/spring-context
compile group: 'org.springframework', name: 'spring-context', version: '5.2.6.RELEASE'
*/
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'org.springframework', name: 'spring-context', version: '5.2.6.RELEASE'
// implementation 'org.springframework:spring-context:5.2.6.RELEASE'
}
整理的代码放这里
https://gitee.com/yanlong10829/magic-grovvy.git
整理的代码放这里
https://gitee.com/yanlong10829/magic-gradle-parent.git
B站上黑马程序员的Gradle视频(侧重应用一点)
https://www.bilibili.com/video/BV1iW411C7CV?from=search&seid=2266826011734735930
B站上Gradle开发团队的视频,(侧重原理一点)
https://www.bilibili.com/video/BV1DE411Z7nt?from=search&seid=2266826011734735930
现在gitee上创建一个和本地项目同名的仓库 magic-gradle-parent
在本地项目路径下执行
git init
git status
git add build.gradle
git add gradle*
git add magic*
git add settings.gradle
git commit -m "gradle多模块"
git remote add origin [email protected]:yanlong10829/magic-gradle-parent.git
git push -u origin master ##因为远端仓库初始化时已经有了一些文件,不能直接push
To gitee.com:yanlong10829/magic-gradle-parent.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to '[email protected]:yanlong10829/magic-gradle-parent.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
git pull --rebase origin master #进行代码合并
From gitee.com:yanlong10829/magic-gradle-parent
* branch master -> FETCH_HEAD
First, rewinding head to replay your work on top of it...
Applying: gradle多模块
git push -u origin master # 执行成功
参看
https://blog.csdn.net/belonghuang157405/article/details/83014664
https://blog.csdn.net/zsyoung/article/details/76891211
接下来,下一步开始分析一遍spring源码,spring boot源码
理解透彻spring源码面试中的常见问题