Run Maven tests with -Dmaven.surefire.debug and -D

2020-06-17 05:10发布

问题:

I am working on a project with Maven and Surefire plugin v. 2.11. To run single tests I am using -Dtest=TestClass#testMethod and -DforkMode=never (without DforkMode=never I can't run tests because of lack of space for object heap). I got used to it and it was working for me fine. So I am running:

mvn -Dtest=TestClass#testMethod test -DforkMode=never

and test is run fine.

But when I am running

mvn -Dtest=TestClass#testMethod -Dmaven.surefire.debug test -DforkMode=never

it just skips debug "waiting" part and the test is being executed (I am unable to connect using IDE). mvn -Dmaven.surefire.debug test works for me fine with other project (where I don't need to care about fork mode).

Any ideas why the combination of forkMode=never and -Dmaven.surefire.debug doesn't work as expected?

回答1:

The property maven.surefire.debug is setting the debugForkedProcess parameter of the surefire plugin.

The documentation for this parameter reads as follows:

Attach a debugger to the forked JVM. If set to "true", the process will suspend and wait for a debugger to attach on port 5005. If set to some other string, that string will be appended to the argLine, allowing you to configure arbitrary debuggability options (without overwriting the other options specified through the argLine parameter).

So it will only debug forked JVMs, which explains why it doesn't work when the tests aren't being forked. It isn't able to setup the debugging of a non-forked JVM process that is already running.

Use mvnDebug

What you can do instead is use mvnDebug, which allows you to debug the maven process itself - and as your tests are not being forked those as well.

i.e. instead of mvn -Dtest=TestClass#testMethod test -DforkMode=never you would execute mvnDebug -Dtest=TestClass#testMethod test -DforkMode=never. By default it will listen on port 8000 when it starts maven.



标签: java maven