SpringBoot源碼學習(零) 搭建Springboot源碼調試環境


准備環境

若是想將git源碼修改記錄上傳到github,需要將springboot源碼fork到本人gitbub。此文是直接下載源碼到本地的。

  1. git
  2. jdk1.8+
  3. gradle的壓縮包
  4. maven,需配置阿里雲倉庫
  5. idea 2020.2.1

Springboot下載源碼

源碼地址

https://github.com/spring-projects/spring-boot

克隆源碼

git clone -b 2.2.x https://github.com/spring-projects/spring-boot.git

開始構建

打開idea,【File】-->【Open...】,打開剛才拉取的Springboot源碼。

open

檢查環境

選擇【File】->【project Structure…】

  1. 檢查Projectproject
  2. 檢查Modelmodel
  3. 檢查SDKssdk

修改gradle相關配置

下載Gradle

下載地址: https://gradle.org/next-steps/?version=5.6.4&format=bin

修改配置

修改spring-boot-project\spring-boot-tools\spring-boot-gradle-plugin\gradle\wrapper\gradle-wrapper.properties文件distributionUrl屬性。

distributionUrl=file:///D:/gradle-5.6.4/gradle-5.6.4-bin.zip

注意: gradle版本,Springboot依賴spring5.2.12.RELEASE,spring依賴gradle-5.6.4-bin.zip,選擇其他版本可能會導致不兼容問題。可參考此文檔: https://docs.gradle.com/enterprise/compatibility

修改spring-boot-project\spring-boot-tools\spring-boot-gradle-plugin\build.gradle文件。

buildscript {
   repositories {
      // 加上阿里雲倉庫, 記得將此行注釋刪除
      maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
      maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter' }
      maven { url "https://repo.spring.io/plugins-release" }
      mavenLocal()
      mavenCentral()
   }
   dependencies {
      classpath("io.spring.javaformat:spring-javaformat-gradle-plugin:0.0.15")
   }
}

repositories {
   // 加上阿里雲倉庫, 記得將此行注釋刪除
   maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
   maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter' }
   maven { url "https://repo.spring.io/plugins-release" }
   mavenLocal()
   mavenCentral()
}

源碼編譯

編譯源碼時測試及清理

找到Spring Boot Build(root)

clean

開始編譯源碼

compile

此時會拋出異常Run spring-javaformat:apply to fix.

解決方案: 控制台執行mvn命令

mvn spring-javaformat:apply

關閉代碼審查

編輯spring-boot-project\spring-boot-parent\pom.xml, 將<disable.checks>false</disable.checks>改為<disable.checks>true</disable.checks>

刪除nohttp審查

  1. 刪除srccheckstyle文件夾
  2. 刪除pom.xml
<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-checkstyle-plugin</artifactId>
   <version>3.1.0</version>
   <dependencies>
      <dependency>
         <groupId>com.puppycrawl.tools</groupId>
         <artifactId>checkstyle</artifactId>
         <version>8.29</version>
      </dependency>
      <dependency>
         <groupId>io.spring.javaformat</groupId>
         <artifactId>spring-javaformat-checkstyle</artifactId>
         <version>${spring-javaformat.version}</version>
      </dependency>
   </dependencies>
   <executions>
      <execution>
         <id>checkstyle-validation</id>
         <phase>validate</phase>
         <configuration>
            <skip>${disable.checks}</skip>
            <configLocation>src/checkstyle/checkstyle.xml</configLocation>
            <suppressionsLocation>src/checkstyle/checkstyle-suppressions.xml</suppressionsLocation>
            <includeTestSourceDirectory>true</includeTestSourceDirectory>
            <propertyExpansion>main.basedir=${main.basedir}</propertyExpansion>
            <encoding>UTF-8</encoding>
         </configuration>
         <goals>
            <goal>check</goal>
         </goals>
      </execution>
      <execution>
         <id>nohttp-checkstyle-validation</id>
         <phase>validate</phase>
         <configuration>
            <skip>${disable.checks}</skip>
            <configLocation>src/checkstyle/nohttp-checkstyle.xml</configLocation>
            <suppressionsLocation>src/checkstyle/nohttp-checkstyle-suppressions.xml</suppressionsLocation>
            <propertyExpansion>main.basedir=${main.basedir}</propertyExpansion>
            <encoding>UTF-8</encoding>
            <sourceDirectories>${basedir}</sourceDirectories>
            <includes>**/*</includes>
            <excludes>**/.git/**/*,**/target/**/,**/.flattened-pom.xml,**/*.class,**/spring-boot-gradle-plugin/build/**,**/spring-boot-gradle-plugin/bin/**</excludes>
         </configuration>
         <goals>
            <goal>check</goal>
         </goals>
         <inherited>false</inherited>
      </execution>
   </executions>
</plugin>

安裝到本地倉庫

install

給源碼添加注釋之后,記得重新install(按需install)下,要么debbug調試時會出現與源碼行數對應不上的情況。

注意:關閉idea自動導包功能, 此功能會自動合並包,會被spring-javaformat插件攔截導致編譯失敗。

效果圖

效果

Spring源碼下載

源碼地址

https://github.com/spring-projects/spring-framework

克隆源碼

git clone -b 5.2.x https://github.com/spring-projects/spring-framework.git

注意: Springboot依賴於Spring的5.2.12.RELEASE版本。

開始構建

打開idea,【File】-->【Open...】,打開剛才拉取的Spring源碼。

修改gradle相關配置

修改配置

修改spring-framework-5.2.12.RELEASE\gradle\wrapper\gradle-wrapper.properties文件distributionUrl屬性。

distributionUrl=file:///D:/gradle-5.6.4/gradle-5.6.4-bin.zip

修改spring-framework-5.2.12.RELEASE\buildSrc\build.gradle文件

repositories {
   // 加上阿里雲倉庫及本地倉庫, 記得將此行注釋刪除
   maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
   maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter' }
   maven { url "https://repo.spring.io/plugins-release" }
   mavenLocal()
   mavenCentral()
   gradlePluginPortal()
}

修改spring-framework-5.2.12.RELEASE\build.gradle文件

repositories {
   // 加上阿里雲倉庫及本地倉庫, 記得將此行注釋刪除
   maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
   maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter' }
   maven { url "https://repo.spring.io/plugins-release" }
   mavenLocal()
   mavenCentral()
   maven { url "https://repo.spring.io/libs-spring-framework-build" }
}

修改spring-framework-5.2.12.RELEASE\settings.gradle文件

pluginManagement {
   repositories {
      // 加上阿里雲倉庫及本地倉庫, 記得將此行注釋刪除
      maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
      maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter' }
      mavenLocal()
      mavenCentral()
      gradlePluginPortal()
      maven { url 'https://repo.spring.io/plugins-release' }
   }
}

刷新maven依賴

排除依賴異常

Could not find javax.servlet-api.jar (javax.servlet:javax.servlet-api:4.0.1).
Searched in the following locations:
    file:/E:/.m2/repository/javax/servlet/javax.servlet-api/4.0.1/javax.servlet-api-4.0.1.jar

解決方案:按照路徑刪除此依賴重新刷新maven依賴。出現其他依賴包問題,重復此操作。

排除git異常

org.gradle.process.internal.ExecException: Process 'command 'git'' finished with non-zero exit value 128

解決方案: 添加git commit

排除spring-context:test異常

Execution failed for task ':spring-context:test'. > There were failing tests. See the report at: fill

解決方案: import-into-idea.md中已經提示,需先編譯./gradlew :spring-oxm:compileTestJava模塊,windows中執行gradlew :spring-oxm:compileTestJava

_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

編譯源碼

build

build

刪除代碼審查

  1. 刪除spring-framework-5.2.12.RELEASE\src\nohttp文件夾。
  2. 刪除spring-framework-5.2.12.RELEASE\src\checkstyle文件夾。
  3. 刪除spring-framework-5.2.12.RELEASE\build.gradle文件中代碼審查相關配置。
apply plugin: "checkstyle"
checkstyle {
   toolVersion = "8.38"
   configDir = rootProject.file("src/checkstyle")
}
checkstyle {
   toolVersion = "8.38"
   configDir = rootProject.file("src/checkstyle")
}
checkstyle("io.spring.javaformat:spring-javaformat-checkstyle:0.0.15")
id 'io.spring.nohttp' version '0.0.5.RELEASE'
apply plugin: "io.spring.nohttp"
nohttp {
   source.exclude "**/test-output/**"
   allowlistFile = project.file("src/nohttp/allowlist.lines")
   def rootPath = file(rootDir).toPath()
   def projectDirs = allprojects.collect { it.projectDir } + "${rootDir}/buildSrc"
   projectDirs.forEach { dir ->
      [ 'bin', 'build', 'out', '.settings' ]
         .collect { rootPath.relativize(new File(dir, it).toPath()) }
         .forEach { source.exclude "$it/**" }
      [ '.classpath', '.project' ]
         .collect { rootPath.relativize(new File(dir, it).toPath()) }
         .forEach { source.exclude "$it" }
   }
}

發布到本地maven倉庫

publish

關聯源碼

創建read-spring閱讀源碼時,需要關聯源碼,添加注釋。添加注釋后,需要重新打包發布加注釋代碼。按需關聯。

source


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM