冲动下容易做出出格的事情
在使用springboot工程进行crud时,发现好多底层的东西都不知道。随即,产生读一读源码的冲动。说是冲动吧,也冲动了一个多星期,现在才有机会将源码的工程搭建起来。
搭建过程如下
一、找到springboot的源码
https://github.com/spring-projects/spring-boot
访问上面的路径,就能找到源码库,由于是国外的网站,会比较慢,耐心等待就可以打开。如果你还是打不开,可以搭梯子,怎么搭梯子就要自行度娘了。
二、对应的版本
springboot更新版本很快的,但是我们也没必要追求最新的。我们的理念应该是【少出bug,少加班,少掉头发。】
我这里是新起的工程,为了不在日后落后太多,选择了最新的2.5.4版本进行学习。是不是矛盾(狗头)?
无奈,网站太慢直接clone不下来,直接下载zip的包,将下载的zip包进行解压,再导入idea。
三、导入idea
在导入之前,需要下载gradle工具。本人也不太会用gradle这里不在赘述,自行度娘解决。
为了快速编译,需要在工程中修改的gradle配置有如下几个。
1、build.gradle
buildscript {
//新版使用了kotlin,增加支持
ext.kotlin_version = '1.4.32'
//阿里云
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" }
}
}
plugins {
id "org.jetbrains.kotlin.jvm" apply false // https://youtrack.jetbrains.com/issue/KT-30276
id "io.spring.nohttp" version "0.0.6.RELEASE"
}
description = "Spring Boot Build"
defaultTasks 'build'
nohttp {
allowlistFile = project.file("src/nohttp/allowlist.lines")
source.exclude "**/bin/**"
source.exclude "**/build/**"
source.exclude "**/out/**"
source.exclude "**/target/**"
source.exclude "spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/resources/org/springframework/boot/buildpack/platform/docker/export.tar"
}
task build {
dependsOn checkstyleNohttp
}
allprojects {
group "org.springframework.boot"
repositories {
//阿里云镜像
maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter' }
mavenCentral()
if (version.contains('-')) {
maven { url "https://repo.spring.io/milestone" }
}
if (version.endsWith('-SNAPSHOT')) {
maven { url "https://repo.spring.io/snapshot" }
}
}
configurations.all {
resolutionStrategy.cacheChangingModulesFor 0, "minutes"
}
}
2、buildSrc/build.gradle
plugins {
id "java-gradle-plugin"
id "io.spring.javaformat" version "${javaFormatVersion}"
id "checkstyle"
}
repositories {
//阿里云
maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter' }
mavenCentral()
gradlePluginPortal()
maven { url "https://repo.spring.io/release" }
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
checkstyle "io.spring.javaformat:spring-javaformat-checkstyle:${javaFormatVersion}"
implementation("com.fasterxml.jackson.core:jackson-databind:2.11.4")
implementation("commons-codec:commons-codec:1.13")
implementation("org.apache.maven:maven-embedder:3.6.2")
implementation("org.asciidoctor:asciidoctor-gradle-jvm:3.3.2")
implementation("org.gradle:test-retry-gradle-plugin:1.1.9")
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.0")
implementation("org.jetbrains.kotlin:kotlin-compiler-embeddable:1.5.0")
implementation("org.springframework:spring-core:5.2.2.RELEASE")
implementation("org.springframework:spring-web:5.2.2.RELEASE")
implementation("io.spring.javaformat:spring-javaformat-gradle-plugin:${javaFormatVersion}")
testImplementation("org.assertj:assertj-core:3.11.1")
testImplementation("org.apache.logging.log4j:log4j-core:2.12.1")
testImplementation("org.junit.jupiter:junit-jupiter:5.6.0")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}
checkstyle {
def archive = configurations.checkstyle.filter { it.name.startsWith("spring-javaformat-checkstyle")}
config = resources.text.fromArchiveEntry(archive, "io/spring/javaformat/checkstyle/checkstyle.xml")
toolVersion = 8.11
}
gradlePlugin {
plugins {
annotationProcessorPlugin {
id = "org.springframework.boot.annotation-processor"
implementationClass = "org.springframework.boot.build.processors.AnnotationProcessorPlugin"
}
autoConfigurationPlugin {
id = "org.springframework.boot.auto-configuration"
implementationClass = "org.springframework.boot.build.autoconfigure.AutoConfigurationPlugin"
}
bomPlugin {
id = "org.springframework.boot.bom"
implementationClass = "org.springframework.boot.build.bom.BomPlugin"
}
configurationPropertiesPlugin {
id = "org.springframework.boot.configuration-properties"
implementationClass = "org.springframework.boot.build.context.properties.ConfigurationPropertiesPlugin"
}
conventionsPlugin {
id = "org.springframework.boot.conventions"
implementationClass = "org.springframework.boot.build.ConventionsPlugin"
}
deployedPlugin {
id = "org.springframework.boot.deployed"
implementationClass = "org.springframework.boot.build.DeployedPlugin"
}
integrationTestPlugin {
id = "org.springframework.boot.integration-test"
implementationClass = "org.springframework.boot.build.test.IntegrationTestPlugin"
}
mavenPluginPlugin {
id = "org.springframework.boot.maven-plugin"
implementationClass = "org.springframework.boot.build.mavenplugin.MavenPluginPlugin"
}
mavenRepositoryPlugin {
id = "org.springframework.boot.maven-repository"
implementationClass = "org.springframework.boot.build.MavenRepositoryPlugin"
}
optionalDependenciesPlugin {
id = "org.springframework.boot.optional-dependencies"
implementationClass = "org.springframework.boot.build.optional.OptionalDependenciesPlugin"
}
starterPlugin {
id = "org.springframework.boot.starter"
implementationClass = "org.springframework.boot.build.starters.StarterPlugin"
}
testFailuresPlugin {
id = "org.springframework.boot.test-failures"
implementationClass = "org.springframework.boot.build.testing.TestFailuresPlugin"
}
}
}
test {
useJUnitPlatform()
}
3、buildSrc/settings.gradle
pluginManagement {
repositories {
//阿里云
maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter' }
mavenCentral()
gradlePluginPortal()
}
resolutionStrategy {
eachPlugin {
if (requested.id.id == "io.spring.javaformat") {
useModule "io.spring.javaformat:spring-javaformat-gradle-plugin:${requested.version}"
}
}
}
}
4、settings.gradle
pluginManagement {
repositories {
//阿里云镜像
maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter' }
mavenCentral()
gradlePluginPortal()
maven {
url 'https://repo.spring.io/release'
}
if (version.endsWith('-SNAPSHOT')) {
maven { url "https://repo.spring.io/snapshot" }
}
}
resolutionStrategy {
eachPlugin {
if (requested.id.id == "org.jetbrains.kotlin.jvm") {
useVersion "${kotlinVersion}"
}
if (requested.id.id == "org.jetbrains.kotlin.plugin.spring") {
useVersion "${kotlinVersion}"
}
}
}
}
plugins {
id "com.gradle.enterprise" version "3.6.1"
id "io.spring.ge.conventions" version "0.0.8"
}
rootProject.name="spring-boot-build"
settings.gradle.projectsLoaded {
gradleEnterprise {
buildScan {
def toolchainVersion = settings.gradle.rootProject.findProperty('toolchainVersion')
if (toolchainVersion != null) {
value('Toolchain version', toolchainVersion)
tag("JDK-$toolchainVersion")
}
settings.gradle.rootProject.getBuildDir().mkdirs()
new File(settings.gradle.rootProject.getBuildDir(), "build-scan-uri.txt").text = "build scan not generated"
buildScanPublished { scan ->
new File(settings.gradle.rootProject.getBuildDir(), "build-scan-uri.txt").text = "<${scan.buildScanUri}|build scan>\n"
}
}
}
}
include "spring-boot-project:spring-boot-dependencies"
include "spring-boot-project:spring-boot-parent"
include "spring-boot-project:spring-boot-tools:spring-boot-antlib"
include "spring-boot-project:spring-boot-tools:spring-boot-autoconfigure-processor"
include "spring-boot-project:spring-boot-tools:spring-boot-buildpack-platform"
include "spring-boot-project:spring-boot-tools:spring-boot-configuration-metadata"
include "spring-boot-project:spring-boot-tools:spring-boot-configuration-processor"
include "spring-boot-project:spring-boot-tools:spring-boot-gradle-plugin"
include "spring-boot-project:spring-boot-tools:spring-boot-jarmode-layertools"
include "spring-boot-project:spring-boot-tools:spring-boot-loader"
include "spring-boot-project:spring-boot-tools:spring-boot-loader-tools"
include "spring-boot-project:spring-boot-tools:spring-boot-maven-plugin"
include "spring-boot-project:spring-boot-tools:spring-boot-test-support"
include "spring-boot-project:spring-boot"
include "spring-boot-project:spring-boot-autoconfigure"
include "spring-boot-project:spring-boot-actuator"
include "spring-boot-project:spring-boot-actuator-autoconfigure"
include "spring-boot-project:spring-boot-cli"
include "spring-boot-project:spring-boot-devtools"
include "spring-boot-project:spring-boot-docs"
include "spring-boot-project:spring-boot-properties-migrator"
include "spring-boot-project:spring-boot-test"
include "spring-boot-project:spring-boot-test-autoconfigure"
include "spring-boot-tests:spring-boot-deployment-tests"
include "spring-boot-tests:spring-boot-integration-tests:spring-boot-configuration-processor-tests"
include "spring-boot-tests:spring-boot-integration-tests:spring-boot-launch-script-tests"
include "spring-boot-tests:spring-boot-integration-tests:spring-boot-loader-tests"
include "spring-boot-tests:spring-boot-integration-tests:spring-boot-server-tests"
file("${rootDir}/spring-boot-project/spring-boot-starters").eachDirMatch(~/spring-boot-starter.*/) {
include "spring-boot-project:spring-boot-starters:${it.name}"
}
file("${rootDir}/spring-boot-tests/spring-boot-smoke-tests").eachDirMatch(~/spring-boot-smoke-test.*/) {
include "spring-boot-tests:spring-boot-smoke-tests:${it.name}"
}
修改完后,导入idea。等待编译。
四、编译错误解决
1、增加idea的内存
修改idea的bin目录下的idea.exe.vmoptions文件,修改idea的初始化内存和最大内存参数:
-Xms1024m,-Xmx4096m
具体如下
-Xms128m
-Xmx4096m
-XX:ReservedCodeCacheSize=240m
-XX:+UseConcMarkSweepGC
-XX:SoftRefLRUPolicyMSPerMB=50
-ea
-XX:CICompilerCount=2
-Dsun.io.useCanonPrefixCache=false
-Djdk.http.auth.tunneling.disabledSchemes=""
-XX:+HeapDumpOnOutOfMemoryError
-XX:-OmitStackTraceInFastThrow
-Djdk.attach.allowAttachSelf=true
-Dkotlinx.coroutines.debug=off
-Djdk.module.illegalAccess.silent=true
#-javaagent:C:\Users\Public\.jetbrains\jetbrains-agent-v3.2.0.de72.619
#-javaagent:C:\Users\Public\.jetbrains\jetbrains-agent-v3.2.0.de72.619
#-javaagent:C:\Users\Public\.jetbrains\jetbrains-agent-v3.2.0.de72.619
-javaagent:C:\Users\Public\.jetbrains\jetbrains-agent-v3.2.0.de72.619
-Dfile.encoding=UTF-8