在 IntelliJ 社区中调试 Spring Boot 3

问题描述 投票:0回答:3

自从迁移到 Spring Boot 3 以来,当我运行 Maven Spring Boot 目标时,我的应用程序不再停留在断点处

mvn spring-boot:run

这是因为 Spring 分叉了线程或进程,并且调试器未附加到此。

在以前的 Spring-Boot 版本中,您可以通过传递

-Dspring-boot.run.fork=false
来禁用分叉(请参阅如何使用 IntelliJ IDEA 社区版调试 spring-boot 应用程序?)。

不幸的是,此选项已被删除,您可以在 Spring Boot 3.0 迁移指南中阅读:

Spring Boot 2.7 中已弃用的 spring-boot:run 和 spring-boot:start 的 fork 属性已被删除。

有没有可能让断点再次起作用?

目前的想法

  1. 当然IntelliJ Ultimate有更好的Spring Boot集成。我正在尝试使其与社区版一起使用。

  2. 我还尝试不运行 Maven 目标,而是进行应用程序运行配置。到目前为止,这失败了,因为

    java.lang.ClassNotFoundException
    ,它找不到 Main 类。不确定我是否应该进一步调查该选项。

  3. 最后一个想法是使用调试选项启动 Maven 目标,以便可以附加外部调试器。这也不起作用,即使可以,我也只能在启动后附加调试器,这将使得调试 spring 上下文的创建变得不可能。

spring-boot debugging intellij-idea
3个回答
4
投票

选项 3 似乎有效,在单独的终端中启动 Maven 目标。

pom.xml:

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <jvmArguments>
                        -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005
                    </jvmArguments>
                </configuration>
            </plugin>
        </plugins>
    </build>

终端:

PS> mvn spring-boot:run -f .\pom.xml
# ...
[INFO] Attaching agents: []
Listening for transport dt_socket at address: 5005

IntelliJ IDEA:

  1. 创建远程调试配置
  2. 设置端口号以匹配 pom.xml 中输入的端口号(例如
    5005
  3. 在终端中运行 Maven 目标。
    • 在启动 Spring Boot 应用程序之前,它似乎等待端口上的调试连接
  4. 在 IntelliJ IDEA 中运行远程调试配置。
    • 当它连接到“远程”端口时,Spring Boot 应用程序将启动,并且 IDE 中将命中断点。

1
投票

我终于找到了至少一个解决方法。可能并非在所有情况下都理想。

我用 JUnit 做了一个

@SpringBootTest
,然后是一个只休眠一个测试方法。这样 Spring Context 就会出现并保留下来进行调试。如果我让 Spring 绑定到定义的端口,我什至可以调用 REST 端点。

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
class TemplateApplicationTests {

    @Test
    void contextLoads() throws InterruptedException {
        Thread.sleep(360000);
    }
}

0
投票

还有另一个插件可以让你在与 maven 相同的 JVM 中运行任何 java 应用程序,而不仅仅是 spring-boot。 exec-maven-插件
配置示例:

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <id>java</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>com.example.MySpringApp</mainClass>
                </configuration>
            </plugin>

这允许您通过 intellij 进行调试,还可以使用 maven 启动的初始 JVM 中已有的环境变量和属性。

我们目前在我的公司中使用它与 vault-maven-plugin 合作在本地开发人员环境中运行/调试我们的应用程序,而无需操作我们的依赖项的密码/凭据。

© www.soinside.com 2019 - 2024. All rights reserved.