Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug spring boot maven project in debug mode in Intellij?

I run my maven project with the following command for dev profile from my terminal

 sudo mvn spring-boot:run -Dspring.profiles.active=dev

How can I run my project to run in debug mode with given profile. How to set configuration in intellij for this?

like image 613
Mr.Robot Avatar asked Sep 13 '20 10:09

Mr.Robot


3 Answers

Running a Spring Boot project from Maven with a specific profile

mvn spring-boot:run -Dspring-boot.run.profiles=foo,bar

Or by using a shell variable

SPRING_PROFILES_ACTIVE=foo mvn spring-boot:run

Or by passing arguments to the JVM

mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Dspring.profiles.active=foo,bar"

These are the only methods that I know of that work for Spring Boot v2.0+.
The first option is recognized by the Spring Boot Maven plugin and passes it on to the application JVM.
Since version 2.0 of Spring Boot, the run goal forks the process by default. Since -Dspring.profiles.active is not recognized by the plugin directly, it's only seen by the Maven process and not passed on to the app itself. This is why it doesn't work in the form mvn spring-boot:run -Dspring.profiles.active=foo,bar.
In the second option, the shell variable should be visible to any subprocesses spawned from that shell.

Starting a Spring Boot project in debug mode from Maven

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

Putting it together

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

Alternative, passing all arguments to JVM

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

The Maven pom.xml should include the Spring Boot plugin

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>2.0.1.RELEASE</version>
</plugin>

In IntelliJ you should create a new "Remote" debug configuration from the "Run/Debug Configurations" tool window. You'll find it in the main menu - "Run / Edit Configurations..."
The default config will use the same 5005 port. After that, launch that debug config. The console should display "Connected to the target VM...".

Sources:

  • https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/maven-plugin/examples/run-profiles.html
  • https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/maven-plugin/examples/run-debug.html
like image 192
ovichiro Avatar answered Oct 14 '22 11:10

ovichiro


If you're running from maven, then add the following parameters:

 mvn spring-boot:run -Dspring.profiles.active=dev -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5555"

Here 5555 is a debug port number (you can use any other unoccupied port).

Then in IntelliJ you can use Remote Debug configuration and connect to that port.

If you open the pom.xml from intelliJ, you can create a Run Configuration with --spring.profiles.active=dev and main class that is a class with method main just like in a regular the most simple java application.

like image 39
Mark Bramnik Avatar answered Oct 14 '22 12:10

Mark Bramnik


enter image description here

Just click run button (green triable button) then click Debug...

IntelliJ will run your spring-boot app in debugging mode

If you want to run with arguments just open edit configuration and put your args in VM Options/Program arguments like

enter image description here

like image 38
Toàn Nguyễn Hải Avatar answered Oct 14 '22 12:10

Toàn Nguyễn Hải