spring-boot-maven-plugin:debug调试程序

本文介绍如何配置Spring Boot项目以实现有效的调试。通过设置spring-boot-maven-plugin插件中的jvmArguments参数,可以使开发者在不脱离IDE的情况下进行断点调试。文章详细解释了两种配置方法及其步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring-boot-maven-plugin的 run goal 可启动程序运行项目。但发现直接执行 spring-boot:run时,不能debug打断点点调试。spring-boot-maven-plugin的官方文档给出了原因和解决方式。

原因

run goal 默认将应用运行于 a forked process,通常方式设置的命令行参数不会作用于程序,因此直接使用run goal,是不能调试程序的。

解决方式

若想调试,或想设置其他JVM 参数,应配置 jvmArguments参数。

  • 方式一,在pom文件中配置

    <project>
      ...
      <build>
        ...
        <plugins>
          ...
          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.2.1.RELEASE</version>
            <configuration>
              <jvmArguments>
                -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005
              </jvmArguments>
            </configuration>
            ...
          </plugin>
          ...
        </plugins>
        ...
      </build>
      ...
    </project>
    
  • 方式二,在命令行中指定

    mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"
    

通过以上方式运行后,进程将监听本地的5005端口,等待调试器连接进来。我们可以在5005端口进行远程调试。

在idea中实践

  1. 在pom中按上述代码配置 jvmArguments参数。

  2. 添加一个远程调试配置:

    Host为 localhost,Port 应与spring-boot-maven-plugin中配置的端口一致,这里是5005

  3. 执行spring-boot-maven-plugin的 run goal
    在这里插入图片描述
    这时可以看到,进程在监听5005端口,等待调试器连接
    在这里插入图片描述

  4. DEBUG模式下启动远程调试,发现已连接上5005端口
    在这里插入图片描述

  5. 可以打断点调试了,完结撒花

S D:\project_vscode\demo> ./mvnw spring-boot:run [INFO] Scanning for projects... Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-maven-plugin/与%20Spring%20Boot%20版本一致/spring-boot-maven-plugin-与%20Spring%20Boot%20版本一致.pom [WARNING] Failed to retrieve plugin descriptor for org.springframework.boot:spring-boot-maven-plugin:Spring Boot 版本一致: Plugin org.springframework.boot:spring-boot-maven-plugin:Spring Boot 版本一致 or one of its dependencies could not be resolved: The following artifacts could not be resolved: org.springframework.boot:spring-boot-maven-plugin:pom:Spring Boot 版本一致 (absent): Could not transfer artifact org.springframework.boot:spring-boot-maven-plugin:pom:Spring Boot 版本一致 from/to central (https://repo.maven.apache.org/maven2): status code: 400, reason phrase: Invalid URI (400) [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.519 s [INFO] Finished at: 2025-03-21T23:58:44+08:00 [INFO] ------------------------------------------------------------------------ [ERROR] Plugin org.springframework.boot:spring-boot-maven-plugin:Spring Boot 版本一致 or one of its dependencies could not be resolved: [ERROR] The following artifacts could not be resolved: org.springframework.boot:spring-boot-maven-plugin:pom:Spring Boot 版本一致 (absent): org.springframework.boot:spring-boot-maven-plugin:pom:Spring Boot 版本一致 failed to transfer from https://repo.maven.apache.org/maven2 during a previous attempt. This failure was cached in the local repository and resolution is not reattempted until the update interval of central has elapsed or updates are forced. Original error: Could not transfer artifact org.springframework.boot:spring-boot-maven-plugin:pom:Spring Boot 版本一致 from/to central (https://repo.maven.apache.org/maven2): status code: 400, reason phrase: Invalid URI (400) [ERROR] -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginResolutionException
最新发布
03-22
<think>嗯,用户遇到了构建失败的问题,错误信息是关于Maven插件无法解析。首先,我需要仔细看看错误日志。用户执行的是./mvnw spring-boot:run,结果提示无法下载spring-boot-maven-plugin,版本显示的是“与 Spring Boot 版本一致”,这看起来有问题。 可能用户的项目中的pom.xml文件里,插件的版本号被错误地设置成了“与 Spring Boot 版本一致”这样的占位符,而不是具体的版本号,比如3.1.5。Maven在解析时,会把这个字符串作为版本号去仓库查找,自然找不到对应的插件,导致HTTP 400错误,因为URL不合法。 接下来,我需要确认用户在pom.xml中的配置是否正确。通常,Spring Boot的父项目会管理插件版本,所以可能用户没有正确继承父项目,或者手动指定了错误的插件版本。需要检查<parent>部分是否正确,以及<plugins>中的spring-boot-maven-plugin是否指定了版本,或者是否有变量未被替换。 另外,用户可能使用了某个教程或模板,但未正确替换占位符,导致版本号保持原样。这时候需要指导用户打开pom.xml,找到插件配置部分,将版本号改为实际版本,或者引用正确的属性变量,比如${spring-boot.version},并确保属性已被定义。 还有可能是Maven本地仓库缓存的问题,但错误提示说已经缓存了失败记录,可能需要清除缓存或强制更新。不过首要问题还是版本号不正确,必须先解决版本配置问题。 总结下来,解决问题的步骤应该是:检查pom.xml中的插件版本配置,更正为正确版本号,确保父项目正确继承,然后重新运行构建命令。同时提醒用户检查项目结构,确认依赖配置无误。</think>从错误日志分析,您遇到的构建失败问题是由于Maven插件版本号配置错误导致的。以下是逐步排查和解决方案: --- ### 错误原因分析 错误日志显示: ```log Could not transfer artifact org.springframework.boot:spring-boot-maven-plugin:pom:Spring Boot 版本一致 ``` - **问题定位**:`pom.xml` 文件中 `spring-boot-maven-plugin` 插件的版本号被错误地设置为字符串 `与 Spring Boot 版本一致`,而非实际版本号(如 `3.1.5`)。 - **后果**:Maven 尝试从中央仓库下载 `与 Spring Boot 版本一致` 版本的插件,但该版本号不合法,导致 HTTP 400 错误。 --- ### 解决方案 #### 步骤 1:检查 `pom.xml` 文件 找到 `spring-boot-maven-plugin` 插件的配置部分: ```xml <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>与 Spring Boot 版本一致</version> <!-- 错误配置 --> </plugin> </plugins> </build> ``` #### 步骤 2:修正插件版本号 根据您的 Spring Boot 项目实际版本,替换为具体的版本号。例如: ```xml <version>3.1.5</version> <!-- 假设实际版本是 3.1.5 --> ``` 或通过属性占位符动态获取版本号(推荐): ```xml <version>${spring-boot.version}</version> ``` 确保父项目中已定义 `spring-boot.version` 属性(通常继承自 `spring-boot-starter-parent`)。 --- #### 步骤 3:检查父项目继承 确保 `pom.xml` 中已正确继承 Spring Boot 的父项目: ```xml <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.1.5</version> <!-- 与插件版本一致 --> <relativePath/> </parent> ``` --- #### 步骤 4:清理本地仓库缓存 如果之前尝试过错误构建,可能需要清理 Maven 本地仓库中的无效缓存: ```bash # 强制重新下载依赖 ./mvnw clean install -U ``` --- ### 完整修正示例 ```xml <?xml version="1.0" encoding="UTF-8"?> <project> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.1.5</version> <relativePath/> </parent> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <!-- 自动继承父项目版本号 --> </plugin> </plugins> </build> </project> ``` --- ### 附加说明 - **版本号合法性**:所有 Maven 依赖的版本号必须是合法字符串(如 `2.7.0`),不能包含中文或占位符。 - **依赖继承机制**:通过继承 `spring-boot-starter-parent`,可自动管理插件版本,无需手动指定。 修正后重新运行命令即可正常启动: ```bash ./mvnw spring-boot:run ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值