跳至主要內容

apzs...大约 14 分钟

安装软件

下载软件

下载 spring-boot

spring-boot 源码网址: https://github.com/spring-projects/spring-bootopen in new window 我准备构建的是 v2.3..RELEASE这个版本,,下载该版本的网址为:https://github.com/spring-projects/spring-boot/archive/refs/tags/v2.3.4.RELEASE.zipopen in new window 推荐构建带RELEASE的稳定版本,比其他版本好构建一些,如果直接克隆,最好全部都下载最新版本(IDEA``gradle 这些都要是最新的),不然会有一些意想不到的bug。(当然都使用最新的也会有很多问题) image-20221113100246350

下载gradle

打开 gradle\wrapper\gradle-wrapper.properties这个文件,distributionUrl的值就是这个源码使用的 **gradle版本,去掉 **https后面的\就是下载地址,打开浏览器或IDM直接可以下载。

https://services.gradle.org/distributions/gradle-6.6.1-bin.zip

image.png 下载完成后,解压即可。

修改源码的gradle地址

然后修改gradle\wrapper\gradle-wrapper.properties文件里的 distributionUrl的值为刚刚下载文件的地址 (注意使用的是正斜杠/)

distributionUrl=file:///A:/gradle-6.6.1-bin.zip

image.png 如果下载的是最新的项目源码,可以在以下网址里下载bin文件 下载地址 https://gradle.org/releases/open in new window 或使用如下地址 https://services.gradle.org/distributions/open in new windowimage.png

配置gradle

添加gradle配置

新建 respository文件夹,用来作为仓库 image.pnginit.d里新建init.gradle 文件,在里面配置阿里云的镜像仓库地址

gradle.projectsLoaded {
    rootProject.allprojects {
        buildscript {
            repositories {
                def JCENTER_URL = 'https://maven.aliyun.com/repository/jcenter'
                def GOOGLE_URL = 'https://maven.aliyun.com/repository/google'
                def NEXUS_URL = 'http://maven.aliyun.com/nexus/content/repositories/jcenter'
                all { ArtifactRepository repo ->
                    if (repo instanceof MavenArtifactRepository) {
                        def url = repo.url.toString()
                        if (url.startsWith('https://jcenter.bintray.com/')) {
                            project.logger.lifecycle "Repository ${repo.url} replaced by $JCENTER_URL."
                            println("buildscript ${repo.url} replaced by $JCENTER_URL.")
                            remove repo
                        }
                        else if (url.startsWith('https://dl.google.com/dl/android/maven2/')) {
                            project.logger.lifecycle "Repository ${repo.url} replaced by $GOOGLE_URL."
                            println("buildscript ${repo.url} replaced by $GOOGLE_URL.")
                            remove repo
                        }
                        else if (url.startsWith('https://repo1.maven.org/maven2')) {
                            project.logger.lifecycle "Repository ${repo.url} replaced by $REPOSITORY_URL."
                            println("buildscript ${repo.url} replaced by $REPOSITORY_URL.")
                            remove repo
                        }
                    }
                }
                jcenter {
                    url JCENTER_URL
                }
                google {
                    url GOOGLE_URL
                }
                maven {
                    url NEXUS_URL
                }
            }
        }
        repositories {
            def JCENTER_URL = 'https://maven.aliyun.com/repository/jcenter'
            def GOOGLE_URL = 'https://maven.aliyun.com/repository/google'
            def NEXUS_URL = 'http://maven.aliyun.com/nexus/content/repositories/jcenter'
            all { ArtifactRepository repo ->
                if (repo instanceof MavenArtifactRepository) {
                    def url = repo.url.toString()
                    if (url.startsWith('https://jcenter.bintray.com/')) {
                        project.logger.lifecycle "Repository ${repo.url} replaced by $JCENTER_URL."
                        println("buildscript ${repo.url} replaced by $JCENTER_URL.")
                        remove repo
                    }
                    else if (url.startsWith('https://dl.google.com/dl/android/maven2/')) {
                        project.logger.lifecycle "Repository ${repo.url} replaced by $GOOGLE_URL."
                        println("buildscript ${repo.url} replaced by $GOOGLE_URL.")
                        remove repo
                    }
                    else if (url.startsWith('https://repo1.maven.org/maven2')) {
                        project.logger.lifecycle "Repository ${repo.url} replaced by $REPOSITORY_URL."
                        println("buildscript ${repo.url} replaced by $REPOSITORY_URL.")
                        remove repo
                    }
                }
            }
            jcenter {
                url JCENTER_URL
            }
            google {
                url GOOGLE_URL
            }
            maven {
                url NEXUS_URL
            }
        }
    }
}
image.png
image.png

设置gradle的环境变量

新建系统变量

点击高级里的环境变量 点击新建 输入 GRADLE_HOME 点击浏览目录,选中gradle安装目录(里面有bin等文件夹) image.png

配置gradle的二进制程序位置

双击Path 或 选中Path,点击编辑 在后面添加 ;%GRADLE_HOME%\bin就行了 (如果不是这个界面,可以往后看) image.png 如果想换回新的页面,可以在Path的变量值最前面输入%SystemRoot%system32;就行了。(这应该是以前设置java环境变量的时候,听信了别人的谗言,把java的环境变量移动到最前面,导致%SystemRoot%system32;到后面去了,%SystemRoot%system32;在最前面有效)

%SystemRoot%system32;

image.png 在这个界面里,点击新建,输入%GRADLE_HOME%\binimage.png

配置仓库的地址

(类似于maven的本地仓库地址) 点击新建 输入 GRADLE_USER_HOME 点击浏览目录,选中gradle里新建的respository image.png

在命令行里输入 gradle -v ,出现版本信息就代表成功了 image.png

导入到IDEA

1. 设置 gradle

点击 Customize里的All setting...image.pngBuild, Execution, Deployment-> Build Tools-> Gradle里设置 Gradle user home:(一般IDEA会自动识别,不过做好还是设置一下) image.png

2. 导入项目

点击Openimage.png 选择源码存放的目录,点击OKimage.png 或者选择build.gradle文件也行,点击OKimage.png 点击Open as Projectimage.png

3. 修改源码配置

打开后首先停止IDEA下载各种jar包 参考链接: https://www.jb51.net/article/201552.htmopen in new window (一定要注意文件的位置,这篇有点坑,但又比较详细) 参考链接:https://blog.csdn.net/caoniuniu25/article/details/120071167open in new window

修改gradle的url

打开项目后,IDEA会自动下载指定版本的 **gradle **,可以停止下载,修改 gradle/wrapper/gradle-wrapper.properties 文件,指定我们离线的 gradle 压缩包 (这个以前已经修改过了)

##distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-bin.zip
distributionUrl=file:///A:/gradle-6.6.1-bin.zip
image.png
image.png

build.gradle里添加阿里云镜像

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" }
	}
}

image.png 在本文件的allprojects-> repositories里添加阿里云镜像

maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter' }
image.png
image.png

修改settings.gradle配置

settings.gradle里的pluginManagement-> repositories里添加阿里云镜像

  maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
  maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter' }

image.png 注释掉plugins里的id "io.spring.gradle-enterprise-conventions" version "0.0.3",这行代码是用来做gradle企业约束的,不注释会报Error resolving plugin [id: 'io.spring.gradle-enterprise-conventions', version: '0.0.3']的错误 image.png 以下是错误的完整信息

FAILURE: Build failed with an exception.

* Where:
Settings file 'B:\spring-boot-2.3.4.RELEASE\settings.gradle' line: 29

* What went wrong:
Error resolving plugin [id: 'io.spring.gradle-enterprise-conventions', version: '0.0.3']
> 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.3
      > Could not get resource 'https://repo.spring.io/plugins-release/io/spring/gradle-enterprise-conventions/io.spring.gradle-enterprise-conventions.gradle.plugin/0.0.3/io.spring.gradle-enterprise-conventions.gradle.plugin-0.0.3.jar'.
         > Could not HEAD 'https://repo.spring.io/plugins-release/io/spring/gradle-enterprise-conventions/io.spring.gradle-enterprise-conventions.gradle.plugin/0.0.3/io.spring.gradle-enterprise-conventions.gradle.plugin-0.0.3.jar'. Received status code 401 from server: Unauthorized

* Try:
Run with --info or --debug option to get more log output. Run with --scan to get full insights.
image.png
image.png

修改buildSrc/build.gradle配置

可能是原有的编译环境,注释掉id "io.spring.javaformat" version "${javaFormatVersion}",否则报错 image.pngbuildSrc/build.gradlerepositories里添加阿里云镜像

	//阿里云镜像
	maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
	maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter' }
image.png
image.png

buildSrc/settings.gradle里添加阿里云镜像

buildSrc/settings.gradle里的pluginManagement-> repositories里添加阿里云镜像 image.png

修改buildSrc/gradle.properties配置

buildSrc/gradle.properties里添加如下配置

gradlePropertiesProp=gradlePropertiesValue
sysProp=shouldBeOverWrittenBySysProp
systemProp.system=systemValue
org.gradle.caching=false
org.gradle.jvmargs=-Xms2048m -Xmx4096m
org.gradle.parallel=true
org.gradle.daemon=true
org.gradle.configureondemand=true
image.png
image.png

修改 buildSrc/build.gradle文件

修改 buildSrc/build.gradle文件,注释掉 id "io.spring.javaformat" version "${javaFormatVersion}"

plugins {
	id "java-gradle-plugin"
	//可能是原有的编译环境,注释掉,否则报错
	//id "io.spring.javaformat" version "${javaFormatVersion}"
	id "checkstyle"
}

准备编译

选择java版本

设置项目的java版本

README.adoc文件提到need JDK 1.8.image.pngCONTRIBUTING.adoc里也说了需要JavaSE-1.8环境 image.pngbuildSrc/build.gradle里写了sourceCompatibilitytargetCompatibility都为1.8image.png 因此点击 File->Project Structure... -> ProjectSDK里选择1.8,在Language level里选择8 - Lambdas, type annotations etc.image.png

查看Gradle JVM版本

在设置里查看Build, Execution, Deployment->Gradle里的Gradle JVM是否为1.8 最好也看一下Gradle user home设置的是否为gradle本地仓库地址 image.png

开始编译

点击Gradle的刷新按钮,IDEA将会编译项目 image.png 然后就是漫长的下载文件的过程了,大概10~30分钟 编译的时候有个 fatal: not a git repository (or any of the parent directories): .git这个提示,这个不用管,这是检测到没有使用版本管理的提示 image.png 报这个错也不用管

Build scan background action failed.
org.gradle.process.internal.ExecException: Process 'command 'git'' finished with non-zero exit value 128
	at org.gradle.process.internal.DefaultExecHandle$ExecResultImpl.assertNormalExitValue(DefaultExecHandle.java:415)
	at org.gradle.process.internal.DefaultExecAction.execute(DefaultExecAction.java:38)
	at org.gradle.process.internal.DefaultExecActionFactory.exec(DefaultExecActionFactory.java:202)
	at io.spring.ge.WorkingDirectoryProcessOperations.exec(WorkingDirectoryProcessOperations.java:45)
	at io.spring.ge.BuildScanConventions.exec(BuildScanConventions.java:162)
	at io.spring.ge.BuildScanConventions.addGitMetadata(BuildScanConventions.java:111)
	at com.gradle.scan.plugin.internal.api.n.a(SourceFile:26)
	at com.gradle.scan.plugin.internal.api.o$a.a(SourceFile:112)
	at com.gradle.scan.plugin.internal.api.k.a(SourceFile:67)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)
> Task :prepareKotlinBuildScriptModel UP-TO-DATE

image.png 出现BUILD SUCCESSFUL就证明成功了 image.png

3. 新建 gradle模块

在自己新建的gradle模块的build.gradle里的dependencies里添加如下依赖,引入spring-boot-starter

api(project(":spring-boot-project:spring-boot-starters:spring-boot-starter"))

结果报了如下错误

Build file 'B:\spring-boot-2.3.4.RELEASE\my-test\build.gradle' line: 15

A problem occurred evaluating project ':my-test'.
> Could not find method api() for arguments [project ':spring-boot-project:spring-boot-starters:spring-boot-starter'] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.

image.png_id 'java'__ 改为 _id 'java-library',重新编译就好了 (git的错误不用管)

BUILD SUCCESSFUL in 4s
12 actionable tasks: 1 executed, 11 up-to-date
Build scan background action failed.
org.gradle.process.internal.ExecException: Process 'command 'git'' finished with non-zero exit value 128
	at org.gradle.process.internal.DefaultExecHandle$ExecResultImpl.assertNormalExitValue(DefaultExecHandle.java:415)
	at org.gradle.process.internal.DefaultExecAction.execute(DefaultExecAction.java:38)
	at org.gradle.process.internal.DefaultExecActionFactory.exec(DefaultExecActionFactory.java:202)
	at io.spring.ge.WorkingDirectoryProcessOperations.exec(WorkingDirectoryProcessOperations.java:45)
	at io.spring.ge.BuildScanConventions.exec(BuildScanConventions.java:162)
	at io.spring.ge.BuildScanConventions.addGitMetadata(BuildScanConventions.java:111)
	at com.gradle.scan.plugin.internal.api.n.a(SourceFile:26)
	at com.gradle.scan.plugin.internal.api.o$a.a(SourceFile:112)
	at com.gradle.scan.plugin.internal.api.k.a(SourceFile:67)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)
11:38:47: Execution finished 'dependencies'.

image.png 主类有一个提示 Cannot access org.springframework.context.ConfigurableApplicationContext运行主类,报了如下错误,spring-boot-starter 里的依赖找不到

Execution failed for task ':my-test:compileJava'.
> Could not resolve all files for configuration ':my-test:compileClasspath'.
   > Could not find jakarta.annotation:jakarta.annotation-api:.
     Required by:
         project :my-test > project :spring-boot-project:spring-boot-starters:spring-boot-starter
   > Could not find org.springframework:spring-core:.
     Required by:
         project :my-test > project :spring-boot-project:spring-boot-starters:spring-boot-starter
         project :my-test > project :spring-boot-project:spring-boot-starters:spring-boot-starter > project :spring-boot-project:spring-boot
   > Could not find org.yaml:snakeyaml:.
     Required by:
         project :my-test > project :spring-boot-project:spring-boot-starters:spring-boot-starter
   > Could not find org.springframework:spring-context:.
     Required by:
         project :my-test > project :spring-boot-project:spring-boot-starters:spring-boot-starter > project :spring-boot-project:spring-boot
   > Could not find ch.qos.logback:logback-classic:.
     Required by:
         project :my-test > project :spring-boot-project:spring-boot-starters:spring-boot-starter > project :spring-boot-project:spring-boot-starters:spring-boot-starter-logging
   > Could not find org.apache.logging.log4j:log4j-to-slf4j:.
     Required by:
         project :my-test > project :spring-boot-project:spring-boot-starters:spring-boot-starter > project :spring-boot-project:spring-boot-starters:spring-boot-starter-logging
   > Could not find org.slf4j:jul-to-slf4j:.
     Required by:
         project :my-test > project :spring-boot-project:spring-boot-starters:spring-boot-starter > project :spring-boot-project:spring-boot-starters:spring-boot-starter-logging

Possible solution:
 - Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html

无标题.png 查看我们模块的依赖可以看到 spring-boot-starter 的依赖报红了 image.png

可能遇见的错误

测试类报错
> Task :buildSrc:test

BomPluginIntegrationTests > libraryModulesAreIncludedInDependencyManagementOfGeneratedPom() FAILED
    org.opentest4j.AssertionFailedError at BomPluginIntegrationTests.java:74

BomPluginIntegrationTests > moduleExclusionsAreIncludedInDependencyManagementOfGeneratedPom() FAILED
    org.opentest4j.AssertionFailedError at BomPluginIntegrationTests.java:163

BomPluginIntegrationTests > libraryImportsAreIncludedInDependencyManagementOfGeneratedPom() FAILED
    org.opentest4j.AssertionFailedError at BomPluginIntegrationTests.java:134

74 tests completed, 3 failed

> Task :buildSrc:test FAILED

Execution failed for task ':buildSrc:test'.
> There were failing tests. See the report at: file:///B:/spring-boot-2.3.4.RELEASE/buildSrc/build/reports/tests/test/index.html

* Try:
> Run with --info or --debug option to get more log output.

* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':buildSrc:test'.
	at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.lambda$executeIfValid$1(ExecuteActionsTaskExecuter.java:142)
    ...

image.png 这是测试类报错,哪一行报错注释掉哪一行就行了 image.png

fatal: not a git repository (or any of the parent directories): .git
> Task :buildSrc:compileJava UP-TO-DATE
> Task :buildSrc:compileGroovy NO-SOURCE
> Task :buildSrc:pluginDescriptors UP-TO-DATE
> Task :buildSrc:processResources UP-TO-DATE
> Task :buildSrc:classes UP-TO-DATE
> Task :buildSrc:jar UP-TO-DATE
> Task :buildSrc:assemble UP-TO-DATE
> Task :buildSrc:checkstyleMain UP-TO-DATE
> Task :buildSrc:compileTestJava UP-TO-DATE
> Task :buildSrc:compileTestGroovy NO-SOURCE
> Task :buildSrc:processTestResources UP-TO-DATE
> Task :buildSrc:testClasses UP-TO-DATE
> Task :buildSrc:checkstyleTest UP-TO-DATE
> Task :buildSrc:pluginUnderTestMetadata UP-TO-DATE
> Task :buildSrc:test UP-TO-DATE
> Task :buildSrc:validatePlugins UP-TO-DATE
> Task :buildSrc:check UP-TO-DATE
> Task :buildSrc:build UP-TO-DATE
Download https://maven.aliyun.com/nexus/content/groups/public/org/jetbrains/kotlin/kotlin-gradle-plugin/1.3.72/kotlin-gradle-plugin-1.3.72.pom, took 295 ms (3.94 kB)
Download https://maven.aliyun.com/nexus/content/groups/public/io/spring/nohttp/nohttp-gradle/0.0.3.RELEASE/nohttp-gradle-0.0.3.RELEASE.pom, took 174 ms (2.51 kB)
...
Download https://repo.maven.apache.org/maven2/org/tukaani/xz/1.6/xz-1.6-sources.jar, took 431 ms (123.45 kB)
Download https://repo.maven.apache.org/maven2/org/assertj/assertj-core/3.11.1/assertj-core-3.11.1-sources.jar, took 1 s 57 ms (986.16 kB)
Download https://repo.maven.apache.org/maven2/org/apache/logging/log4j/log4j-core/2.12.1/log4j-core-2.12.1-sources.jar, took 3 s 169 ms (1.25 MB)
Download https://repo.maven.apache.org/maven2/org/junit/jupiter/junit-jupiter/5.6.0/junit-jupiter-5.6.0-sources.jar, took 169 ms (6.09 kB)
Download https://repo.maven.apache.org/maven2/org/apache/logging/log4j/log4j-api/2.12.1/log4j-api-2.12.1-sources.jar, took 208 ms (254.49 kB)
Download https://repo.maven.apache.org/maven2/org/junit/jupiter/junit-jupiter-params/5.6.0/junit-jupiter-params-5.6.0-sources.jar, took 177 ms (64.75 kB)
Download https://repo.maven.apache.org/maven2/org/junit/jupiter/junit-jupiter-api/5.6.0/junit-jupiter-api-5.6.0-sources.jar, took 198 ms (161.05 kB)
Download https://repo.maven.apache.org/maven2/org/junit/platform/junit-platform-commons/1.6.0/junit-platform-commons-1.6.0-sources.jar, took 181 ms (70.89 kB)
Download https://repo.maven.apache.org/maven2/org/junit/jupiter/junit-jupiter-engine/5.6.0/junit-jupiter-engine-5.6.0-sources.jar, took 421 ms (117.16 kB)
Download https://repo.maven.apache.org/maven2/org/junit/platform/junit-platform-engine/1.6.0/junit-platform-engine-1.6.0-sources.jar, took 364 ms (127.68 kB)

BUILD SUCCESSFUL in 49s
image.png
image.png
下载jar报失败

401代表没有权限,Spring从5.X之后访问repo.spring.io仓库需要权限认证了,所以可以用阿里云的镜像代替Spring官网的镜像。(这样后面还会有别的错误,因此建议把前面的修改配置的步骤做完)

Could not HEAD 'https://repo.spring.io/plugins-release/io/spring/gradle-enterprise-conventions/io.spring.gradle-enterprise-conventions.gradle.plugin/0.0.3/io.spring.gradle-enterprise-conventions.gradle.plugin-0.0.3.jar'. Received status code 401 from server: Unauthorized
Disable Gradle 'offline mode' and sync project

image.png 阿里云镜像文档: https://developer.aliyun.com/mvn/guideopen in new window

仓库名称阿里云仓库地址阿里云仓库地址(老版)源地址
centralhttps://maven.aliyun.com/repository/centralhttps://maven.aliyun.com/nexus/content/repositories/centralhttps://repo1.maven.org/maven2/
jcenterhttps://maven.aliyun.com/repository/publichttps://maven.aliyun.com/nexus/content/repositories/jcenterhttp://jcenter.bintray.com/
publichttps://maven.aliyun.com/repository/publichttps://maven.aliyun.com/nexus/content/groups/publiccentral仓和jcenter仓的聚合仓
googlehttps://maven.aliyun.com/repository/googlehttps://maven.aliyun.com/nexus/content/repositories/googlehttps://maven.google.com/
gradle-pluginhttps://maven.aliyun.com/repository/gradle-pluginhttps://maven.aliyun.com/nexus/content/repositories/gradle-pluginhttps://plugins.gradle.org/m2/
springhttps://maven.aliyun.com/repository/springhttps://maven.aliyun.com/nexus/content/repositories/springhttp://repo.spring.io/libs-milestone/
spring-pluginhttps://maven.aliyun.com/repository/spring-pluginhttps://maven.aliyun.com/nexus/content/repositories/spring-pluginhttp://repo.spring.io/plugins-release/
grails-corehttps://maven.aliyun.com/repository/grails-corehttps://maven.aliyun.com/nexus/content/repositories/grails-corehttps://repo.grails.org/grails/core
apache snapshotshttps://maven.aliyun.com/repository/apache-snapshotshttps://maven.aliyun.com/nexus/content/repositories/apache-snapshotshttps://repository.apache.org/snapshots/

**搜索 **https://repo.spring.io/plugins-release 然后替换为 https://maven.aliyun.com/repository/spring-plugin就行了

多个编译错误异常

更换后又报了如下错误,以下错误就是**gradle**版本不对,需要更换为最新版本

Caused by: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
image.png
image.png

新建测试模块

直接复制spring-boot-2.3.4.RELEASE\spring-boot-tests\spring-boot-smoke-tests\spring-boot-smoke-test-actuator-log4j2,粘贴到spring-boot-2.3.4.RELEASE这整个项目下,改名为spring-boot-study并修改里面的包名和启动类名 image.png 直接运行查看,这样就运行成功了 image.png 写个简单的Hello

package com.atguigu.boot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {

	@ResponseBody
	@GetMapping("/hello")
	public String hello(){
		return "hello";
	}

}

image.png 输入这里的用户名和密码就访问成功了 image.png

新建一个模块测试

这个方法尝试了好多次,都没成功 新建一个Gradle构建的模块,第一次构建时间可能会长一些,期间project目录里的文件会闪,这是正常现象。 无标题.png 直接运行,发现运行不了 image.pngBuild and run里将 nodule not specified修改为jdk 1.8,这样就输出Hello world!image.png 查看与spring-boot-starter不在同一个父项目的模块如何引入spring-boot-starter项目,例如spring-boot-tests\spring-boot-smoke-tests\spring-boot-smoke-test-actuator-log4j2里的build.gradle文件

implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter"))
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-web"))

image.png 先运行这个模块,可以看到这个模块可以运行 image.png 在自己项目的build.gradle文件的dependencies里引入spring-boot-starterspring-boot-starter-web,然后点击dependencies左边的运行按钮,让其构建

implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter"))
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-web"))

image.png 修改Main

package com.atguigu.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class Main {
	public static void main(String[] args) {
		SpringApplication.run(Main.class, args);
	}
}

然后启动类使用的@SpringBootApplication注解和SpringApplication报红,点击右上角gradle的刷新 image.png 出现了一条红色的下划线,提示Cannot access org.springframework.context.ConfigurableApplicationContext,不管它,直接运行报了如下错误。大概意思就是找不到这些依赖

Execution failed for task ':spring-boot-study:compileJava'.
> Could not resolve all files for configuration ':spring-boot-study:compileClasspath'.
   > Could not find jakarta.annotation:jakarta.annotation-api:.
     Required by:
         project :spring-boot-study > project :spring-boot-project:spring-boot-starters:spring-boot-starter
         project :spring-boot-study > project :spring-boot-project:spring-boot-starters:spring-boot-starter-web > project :spring-boot-project:spring-boot-starters:spring-boot-starter-tomcat
   > Could not find org.springframework:spring-core:.
     Required by:
         project :spring-boot-study > project :spring-boot-project:spring-boot-starters:spring-boot-starter
         project :spring-boot-study > project :spring-boot-project:spring-boot-starters:spring-boot-starter > project :spring-boot-project:spring-boot
   ...
Possible solution:
 - Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html

无标题.png 这里卡了很久,最后把所有软件全部卸载,重新运行也还是报这个错,才复制别的模块进来。

评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v3.0.0-alpha.8