51

I want to debug Eclipse build with tests. I tried to run it by Run > Debug Configurations > Maven Build. In Base directory is my Maven repo directory with pom.xml file, in goals 'clean install'. When I click on debug Eclipse starts build, run tests but it doesn't stops on breakpoints.

dur
  • 15,689
  • 25
  • 79
  • 125
Maciej Laskowski
  • 697
  • 1
  • 5
  • 7

4 Answers4

79

Easiest way I find is to:

  1. Right click project

  2. Debug as -> Maven build ...

  3. In the goals field put -Dmaven.surefire.debug test

  4. In the parameters put a new parameter called forkCount with a value of 0 (previously was forkMode=never but it is deprecated and doesn't work anymore)

Set your breakpoints down and run this configuration and it should hit the breakpoint.

lincetto
  • 972
  • 2
  • 13
  • 28
alex.p
  • 2,627
  • 17
  • 28
  • I am getting error 'could not load [aa.properties] as a classloader resource'. I am using ClassLoader.getSystemClassLoader().getResourceAsStream() function to load this. Should I set somewhere working dir or change this code a bit? – Kangur Aug 01 '13 at 17:05
  • 1
    The parameter forkMode is deprecated since version 2.14. Instead use forkCount=0 – Noam Manos Apr 07 '15 at 14:40
  • 1
    tried this in surefire 2.19, 2.18 and 2.17 without any success. It did work for 2.12.4 though. Did something change ? – kellogs Oct 24 '15 at 18:29
  • 1
    I believe #3 should be `-Dmaven.surefire.debug=test` (note the `=`)? – Ternary Feb 10 '16 at 20:32
  • I had to add a source from the file system and not from the workspace. In the Source tab add a file system directory. Click the Add... button and open the project directory. Tick the Search subfolders checkbox. – Stephane Feb 13 '16 at 17:54
  • this is the option with eclipse and maven 3.2.3 and sutrefire plug in 2.18.1after clicking debug it will appear a debug window with source notfound then connect the code ( by clicking that window)with the process by selecting your proyect and you will debug line by line – GoAntonio Dec 10 '17 at 18:00
  • 1
    @Ternary That's why believing is better done in churches than in front of computers. ;) [`test`](https://maven.apache.org/surefire/maven-surefire-plugin/examples/debugging.html) is the goal. [`-Dmaven.surefire.debug`](https://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html) is the property. I'd written it as `test -Dmaven.surefire.debug`, though. – Gerold Broser Aug 15 '18 at 03:20
  • It does not work for me with a Spring Boot application, Eclipse 2021-09 4.21.0. It runs anyway in run mode and not in debug – Marco Sulla Oct 14 '21 at 08:59
  • "forkCount=0" might cause errors with relative paths in the tests, as the Java VM of the root maven process is started from a different path that of forked processes. E.g. "File.getAbsolutePath" returns an expected path, but "File.exists" for the same path does not work (see [https://bugs.java.com/bugdatabase/view_bug?bug_id=4483097](https://bugs.java.com/bugdatabase/view_bug?bug_id=4483097) ). Same for ClassLoader operations. So, use "-Dmaven.surefire.debug", but dont' set "forkCount=0". – wknauf Apr 23 '23 at 10:32
8

if you are using Maven 2.0.8+, then it will be very simple, run mvndebug from the console, and connect to it via Remote Debug Java Application with port 8000.

AGan
  • 457
  • 5
  • 12
Rajesh
  • 1,911
  • 1
  • 22
  • 19
7

The Run/Debug configuration you're using is meant to let you run Maven on your workspace as if from the command line without leaving Eclipse.

Assuming your tests are JUnit based you should be able to debug them by choosing a source folder containing tests with the right button and choose Debug as... -> JUnit tests.

Nicola Musatti
  • 17,834
  • 2
  • 46
  • 55
  • 1
    Awesome! I was searching how to debug maven project in eclipse, and the number 1 link had a million steps, I followed that, must have made mistakes, and did not work. Found out that article was dated 2008. – Bin He May 21 '14 at 01:12
7

probleme : unit test result are not the same runing with eclipse and maven due ti order of library used by eclipse and maven. In my case the test was success with maven but i want to debug my unit test using eclipse, so the most easy way to debug unit test class with eclipse and runing maven is :

1) mvn -Dtest=MySuperClassTest -Dmaven.surefire.debug test ==> it will listen to the 5005 port (default port)

2) Go to eclipse, open a debug configuration, add a new java remote application and change the port to 5005 and debug

3) of course you must add break point somewhere in the class that you want to debug

KyleHodgetts
  • 317
  • 1
  • 4
  • 13