Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug Spring Boot with Netbeans via Maven

After fiddling around for way too long till I got proper debuging setup in Netbeans 8.2 with Spring Boot 1.4.3 I figured I write down my findings as Q&A for others.

The problem is that the default configuration for Netbeans fails to properly launch Spring in debug mode and when you search the internet you only find the outdated information in the Spring docs that won't work.

The solution is simple if you know how. Please find the correct setup instructions below.

like image 447
TwoThe Avatar asked Jan 13 '17 11:01

TwoThe


People also ask

How do I debug a Maven project in NetBeans?

You can debug any Maven goal in NetBeans going to /Project Properties/Actions/ , select the goal you wan to debug, in the last option Set Properties choose Add, and then select Debug Maven build.

How do I enable debug in spring boot?

You can also enable a “debug” mode by starting your application with a --debug flag. You can also specify debug=true in your application. properties . When the debug mode is enabled, a selection of core loggers (embedded container, Hibernate, and Spring Boot) are configured to output more information.


3 Answers

Tested and works with Netbeans 8.2 and Spring-Boot 1.4.3:

First of all make sure you have the Spring Maven plugin included (this should be already included when making a new Netbeans Spring project):

<plugins>
  ...
  <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
      <execution>
        <goals>
          <goal>repackage</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
  ...
</plugins>

Also it is a good idea to include the Spring Devtools like this:

<dependencies>
  ...
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
  </dependency>
  ...
</dependencies>

Now navigate to your project settings -> Actions -> Debug project and set the following:

enter image description here

Execute goals:

spring-boot:run

Set properties:

run.jvmArguments=-Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address}
jpda.listen=true

Now run your application via the usual debug button and Spring should properly connect to the JVM debugger.

Spring Boot 2.x

To enable Netbeans debugging for a Spring Boot 2.x project (and more specifically version 2.x of the spring-boot-maven-plugin) the procedure is exactly the same, except the run.jvmArguments property name has changed to spring-boot.run.jvmArguments:

spring-boot.run.jvmArguments=-Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address}
jpda.listen=true
like image 132
TwoThe Avatar answered Sep 19 '22 21:09

TwoThe


Testing NetBeans 8.2 and Spring Boot 2.0.1, I was not able to make things work following @TwoThe's instructions. First, I encountered an issue where all I saw was "JPDA Listening Start..." in the output window. To resolve that problem, I added Spring Devtools as an optional dependency. Second, even though debugging appeared to be running okay, the "Debugging" window, which normally displays the list of active threads, was empty and breakpoints that I set were not triggered. Third, attempting to stop the debugging session by pressing the red "Finish Debugger Session" button would not stop the Tomcat server.

Instead of changing the execute goals to "spring-boot:run", I found that it was sufficient to use the default "Debug project" action execute goals:

process-classes org.codehaus.mojo:exec-maven-plugin:1.2.1:exec

.. and properties:

exec.args=-Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address} -classpath %classpath ${packageClassName}
exec.executable=java
jpda.listen=true

(As a sidenote, debugging as a regular Java application is apparently the recommended approach to debugging Spring Boot applications in Eclipse; see How to debug Spring Boot application with Eclipse?)

One helpful tip is that if you want to debug using a certain Spring Boot profile, say "debug", you can prepend "-Dspring.profiles.active=debug " to the "exec.args" property. See also: Spring boot running a fully executable JAR and specify -D properties

like image 24
Daniel Trebbien Avatar answered Sep 17 '22 21:09

Daniel Trebbien


enter image description here

Tested on NetBeans9

Action: Add any name Set Properties: select Add> button, select Debug Maven Build And debug as always -> IDE debug button

like image 21
Marvel Alvarez Avatar answered Sep 19 '22 21:09

Marvel Alvarez