Maven and SpringBoot debugging what does -Drun.jvmArguments=-Xdebug really mean - java

I can start my main Spring Boot application of the shelves in debug mode I can set a breakpoint in IntelliJ and it works! I.e. Running below:
#SpringBootApplication
public class JasperApplication {
public static void main(String[] args) {
SpringApplication.run(JasperApplication.class, args);
}
}
However if I start my Spring Boot application in IntelliJ with the maven run plugin with debug option:
spring-boot:run
Debug does not work. I have read about
-Drun.jvmArguments=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"
I can't fit everything together, could someone enlightning me how it all works or direct me to some articles describing the whole thing under the hood.
I like to be able to run maven Spring Boot plugin from within IntelliJ aswell as the command line, being able to debug and set breakpoints. Thanks!

When you invoke spring-boot:run you are starting a remote process i.e. this process is not running inside your IDE.
If you want to debug this process from within your IDE the following pre requisites apply:
The remote process must be 'remote debug aware', this is what you are doing when you run that process with -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"
You must use a remote debugger within your IDE. More details in the docs but the brief summary is:
Run > Edit Configurations
Click on the + icon and choose Remote
Name the run configuration and choose a module for the Search sources using module's classpath
The invoke mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005" and launch this Run configuration within your IDE and the two will talk to each other.
Here's a screenshot showing the remote run configuration looks like:

Related

Add jvm options to a Spring Boot application started via mvn spring-boot:run

I'd like to add -Xmx6g as a command line argument to a spring boot application that is launched with maven via mvn spring-boot:run. I do this easily enough with Intellij, but I would like to launch the application manually from my terminal so that I have some extra ram.
I've tried to configure in the Maven Surefire Plugin, but this hasn't worked. Could anyone share a working snippit?

How to step through edited code of a sub-module/lib via IntelliJ IDEA Ultimate Edition

Am using JDK 1.8 w/ IntelliJ IDEA 2021.2.3 Ultimate Edition on macOS Big Sur 11.6.1.
The situation is that I have a main Java based Spring Boot Microservice that I import as a Maven project.
However, there's a particular lib that is a dependency (its listed in the <dependencies> section of that Spring Boot Microservice's pom.xml file) which I need to be able to put either System.out.println() or logger.info() (logback.xml) statements into...
When I git clone both Maven projects (the Spring Boot Microservice & the particular lib that I need to debug), I do the following process:
After a fresh git cloneof both projects in separate folders, I first import the main Spring Boot Microservice as a Maven Project inside IntelliJ.
Go to File --> Project Structure and then click on the + sign and import the dependency lib (which I wish to edit the code with printlns and/or log statements) as as Module (Maven project).
Put breakpoints and log.info() statements in key sections of the dependency lib's codebase and issue a mvn clean install from either IntellIJ via a run config and/or from the command line.
mvn clean install (even tried mvn clean install -nsu) within the Spring Boot Microservice's top-level project folder and then Run in debug mode (with breakpoints inside the particular endpoint REST Controller class or service impl class, inside the Spring Boot Microservice's codebase which I know will use the dependent lib), the Spring Boot application containing its main() method.
What happens is when I hit the REST endpoint via Postman or curl, I am able to step through the dependent lib's codebase via the IntelliJ's debugger but only from the one that is inside the compiled jar (which comes downloaded from Artifactory into my local ~/.m2/repository) and not the codebase which I imported as a module (which contains my printlns to stdout and/or my log.info() statements), the step through debug process only shows the original unchanged code from GitHub.
Does anyone know how to make it step through that dependent lib's source code (which I edited and then conducted a mvn clean install) so I can see my local changes (calls to stdout and/or log.info() statements) ?

How to know spring boot app is running via mvn spring-boot:run or java execution

My spring boot application can runs mvn spring-boot:run or java execution, I need to know in which mode the application is running inside the main method.
You can't just detect it, but you can help yourself by setting a property that you can check in your code:
mvn spring-boot:run -Drun.jvmArguments="-Drunning.from.maven=true"
Then you can check using
System.getProperty("running.from.maven")
// or
Boolean.getBoolean("running.from.maven")
Or using Spring. Whatever you want.
Maybe 72.6 Set the active Spring profiles & 72.7 Change configuration depending on the environment will be helpfull:
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html
You can set different profiles for different configurations(e.g. production, dev).

STS, running any of the Guide projects

I install Spring Toolsuite.
I go to New -> Import Getting Started project.
I choose the 'Building a RESTful Web Service' one. Download only the 'complete' project, select maven and not to open the site.
Now what on earth to I have to do to launch it? Right-click on the project, run as, run on server, shows 'The selection cannot be run on any server'.
I have gone through loads of posts trying to find out what else I can do, done maven clean, maven install, changed the JRE Environment, nothing works.
Running a maven clean or maven install will only update the dependencies of the project (for instance, bring in Spring and its child dependencies, loggers, etc. specified in the pom.xml file).
To run the project right click on the class with the #SpringBootApplication
annotation (it will likely be the one with a main method in it, that's the one you want!). Then select 'Run as->Java Application'.
First a disclaimer. This answer applies to most of the guides. But there may be some to which it doesn't apply, because there's such a diversity of guides available.
Assuming you are trying a 'typical' guide which does something in the context of a web-app... then read on.
The reason that you can not run a guide with "Run On Server" is because of spring-boot adheres to the slogan of "Make Jar not War". The guide sample code are not things you deploy on a server (i.e war) but are standalone Java apps (i.e. jar) which contain their own embedded servlet container (if they need one). That means running them is really quite simple. Just find the 'main' method/class in the guide and use Eclipse "Run As >> Java Application".
There's also a convenient alias in STS called "Run As >> Spring Boot App" which does pretty much the same thing but gives you a few extra bells and whistles in the launch configuration editor.
Also... you don't really have to go look for the main method yourself because the "Run As >> Spring Boot App" knows how to find it in most cases. So clicking project and "Run As >> Spring Boot App" should do the trick.

How can I run the IntelliJ debugger on unit tests in a Maven project?

I'm working with a multi-artifact Maven project where artifacts in the project have a few dependencies on each other. I am using IntelliJ 9. I'd like to be able to set breakpoints in my unit tests, but when I right-click on the unit tests folder for my artifact and choose "Debug 'All Tests'", I get a Class not found exception referring to a class in a separate Maven artifact.
I can run the Maven "test" goal on the parent artifact and it works fine.
Any ideas? Thanks.
In you run Maven from command line, you will be able to run it with debugger enabled and just attach Idea as remote debugger. That's how I usually use it.
mvn -Dmaven.surefire.debug="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 -Xnoagent -Djava.compiler=NONE" test
See http://maven.apache.org/plugins/maven-surefire-plugin/examples/debugging.html
This will allow debugger connection to port 8000 and wait for you to attach before execution.
I wanted to run the unit tests for a specific package.
I was able to get this to work by making a new JUnit run/debug configuration in IntelliJ. I told it to run the tests in the specific package and for "Use classpath and JDK of module", I picked the root Maven artifact.

Categories

Resources