搭建SpringBoot调试环境

自由编程
https://ziyoubiancheng.github.io

获取SpringBoot源代码

下载地址
https://github.com/spring-projects/spring-boot

国内下载地址
https://gitee.com/mirrors/spring-boot/tree/v2.2.9.RELEASE/

不要下载最新源码,v2.3.x以后构建工具换成了gradle了,方便学习调试源码,选用maven构建的版本。

安装JDK(可选,直接通过IDE配置下载JDK)

可通过如下地址选择适当的JDK版本:https://www.oracle.com/java/technologies/downloads/archive/
例如我们选择JDK8:https://www.oracle.com/java/technologies/javase/jdk8-archive-downloads.html
直接选择windows安装包下载安装即可。
安装后,可在IDE中选择应用相同的JDK。

安装Maven(可选,直接通过IDE配置maven)

  • 下载maven,通过下面链接下载最新maven安装包
    https://maven.apache.org/download.cgi

  • 使用压缩包进行安装,windows系统下载**-bin.zip包
    [图片上传失败...(image-2d5f85-1726668448572)]S

  • 解压后将安装包放在合适的位置

  • 配置环境变量

    • 进入配置系统环境变量界面:我的电脑-右键-属性-高级系统设置-环境变量-系统变量
      [图片上传失败...(image-7c6dea-1726668448572)]
      [图片上传失败...(image-30e624-1726668448572)]

    • 添加系统环境变量MAVEN_HOME
      [图片上传失败...(image-5f982c-1726668448572)]p

    • 添加Path
      [图片上传失败...(image-78244e-1726668448572)]

    • 打开控制台窗口,输入以下命令验证maven是否安装成功

      mvn -v
      

设置maven加速

安装好maven后,windows系统一般在用户home路径下生成maven缓存和配置路径,如C:\Users\user-xxx\.m2,在.m2目录下,修改settings.xml文件,添加国内缓存,示例如下:

<?xml version="1.0" encoding="UTF-8"?>
<settings  xmlns="http://maven.apache.org/SETTINGS/1.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

 <!-- 本地仓库的位置 -->
 <localRepository>${user.home}/.m2/repository</localRepository>

 <!-- Apache Maven 配置 -->
 <pluginGroups/>
 <proxies/>

 <!-- 阿里云镜像 -->
 <mirrors>
     <mirror>
         <id>alimaven</id>
         <name>aliyun maven</name>
         <!-- https://maven.aliyun.com/repository/public/ -->
         <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
         <mirrorOf>central</mirrorOf>
     </mirror>
 </mirrors>

 <profiles>

     <!-- 阿里云配置: 提高国内的jar包下载速度 -->
     <profile>
         <id>ali</id>
         <repositories>
             <repository>
                 <id>alimaven</id>
                 <name>aliyun maven</name>
                 <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
                 <releases>
                     <enabled>true</enabled>
                 </releases>
                 <snapshots>
                     <enabled>true</enabled>
                 </snapshots>
             </repository>
         </repositories>
         <pluginRepositories>
             <pluginRepository>
                 <id>alimaven</id>
                 <name>aliyun maven</name>
                 <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
             </pluginRepository>
         </pluginRepositories>
     </profile>

 </profiles>

 <!-- 激活配置 -->
 <activeProfiles>
     <activeProfile>ali</activeProfile>
 </activeProfiles>
</settings>

安装IDE

可通过下面地址选择安装IDEA:https://www.jetbrains.com/zh-cn/idea/download/?section=windows
注意IDEA有两个版本:Ultimate版本和Community版本,其中Ultimate是收费版,我们选择Community版也基本够用。例子中,我们选择Community版。
选择相应的版本,点击安装即可。

编译SpringBoot源码

解压下载的SpringBoot源码,导入到IDEA中。
编译时,选择1.8(含)版本以上,我们在IDE里选择1.8版本。然后点击Maven下install,将编译后的代码安装到本地maven仓。

设置跳过test

选择install,点击选择“Skip Tests”即可
[图片上传失败...(image-b821bf-1726668448572)]

Formatting violations 处理

跟进提示,执行

mvn spring-javaformat:apply

[图片上传失败...(image-ed716-1726668448572)]

[图片上传失败...(image-3da3de-1726668448572)]

解决pom文件错误提示

项目pom文件中,有基础“disable.checks”提示找不到该属性,在pom文件properties标签内,添加属性,如下:<disable.checks>true</disable.checks>

    <properties>
        <revision>2.2.9.RELEASE</revision>
        <main.basedir>${basedir}</main.basedir>
        <disable.checks>true</disable.checks>
    </properties>

解决其他编译问题

编译过程中,可能出现各种问题,如错误发送在test模块或tools模块,如“ Failed to execute goal com.googlecode.maven-download-plugin:download-maven-plugin:1.4.2:wget (unpack-doc-resources) on project spring-boot-gradle-plugin: IO Error:”,可以将对应模块从其parent模块中注释掉。暂时不要把精力花在不相干的模块上面。

如果错误发生在关键模块,再根据错误提示,找解决办法。

新建测试模块

新建module

[图片上传失败...(image-206616-1726668448572)]

参考下面例子,修改pom文件。parent设置为spring-boot-starter-web,并注意版本。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-build</artifactId>
        <version>2.2.9.RELEASE</version>
    </parent>
    <artifactId>spring-boot-mytest</artifactId>
    <name>Archetype - spring-boot-mytest</name>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.2.9.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.2.9.RELEASE</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.2.9.RELEASE</version>
            </plugin>
        </plugins>
    </build>
</project>

添加相关类

@RestController
public class MytestController {

    @RequestMapping("/test")
    public String test(){
        System.out.println("hello spring boot test");
        return "hello spring boot test";
    }
}

执行Applicatio的main方法

[图片上传失败...(image-62b1b8-1726668448573)]

这时可能遇见Kotlin运行错误提示,主要是版本问题,尝试修改SpringBoot中依赖kotlin版本
再spring-boot-dependencies的pom文件中,修改kotlin版本为1.4.0

<kotlin.version>1.4.0</kotlin.version>

尝试在IDE中修改kotlin版本,如下图
[图片上传失败...(image-ba09eb-1726668448573)]

正常运行main后,正常可在浏览器打开地址http://localhost:8080/test,显示如图页面
[图片上传失败...(image-7e7a47-1726668448573)]

到此,已完成搭建SpringBoot源码调式环境

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容