spring-bootdebuggingintellij-idea

Debug Spring Boot 3 in IntelliJ Community


Since the migration to Spring Boot 3 my application doesn't hold at breakpoints any more, when I run the Maven Spring Boot goal mvn spring-boot:run.

This is because Spring forks the thread or the process and the debugger is not attached to this.

In former Spring-Boot versions you could disable forking by passing -Dspring-boot.run.fork=false (see How to debug spring-boot application with IntelliJ IDEA community Edition?).

Unfortunatly this option was removed as you can read in the Spring Boot 3.0 Migration Guide:

The fork attribute of spring-boot:run and spring-boot:start that was deprecated in Spring Boot 2.7 has been removed.

Is there any possibility to make breakpoints work again?

Ideas so far

  1. Of course IntelliJ Ultimate has better Spring Boot integration. I'm trying to make it work with the Community Edition.

  2. I also tried not to run the Maven goal but to make an Application run configuration. This failed so far, because of java.lang.ClassNotFoundException, it doesn't find the Main class. Not sure if I should investigate that option further.

  3. Last idea was to start the Maven goal with debug options such that an external debugger can attach. That didn't work either and even if it would, I could only attach the debuger after startup and that would make debugging the creation of spring context impossible.


Solution

  • Option 3 seems to work, starting the maven goal in a separate terminal.

    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>
    

    Terminal:

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

    IntelliJ IDEA:

    1. Create a Remote debug configuration
    2. Set the port number to match that entered in the pom.xml (e.g. 5005)
    3. Run the maven goal in a terminal.
      • It appears to wait for a debug connection on the port before starting the Spring Boot application
    4. Run the Remote debug configuration in IntelliJ IDEA.
      • When it connects to the "remote" port, the Spring Boot application will start up and break points will be hit in the IDE.