How to debug tests from a Maven build in Eclipse? - java

How is it possible to start a Maven build and let the tests appear in the debug perspective of Eclipse?

A package or install is meant to be an atomic action that really shouldn't be stopped for debugging (unless you're debugging a maven plug-in). You ought to be able to debug the tests by selecting the Eclipse project and doing a "Debug As -> JUnit Test", though.

How to configure it:
Create a new Java project in Eclipse. Let's call it "Maven debugger project". It's a dummy project and is not intented to contain any code.
Create a new debug configuration. Let's call it "Maven debug launch". In the Connect tab, enter "localhost" in the Host field and "5005" in the Port field, which is the port where the Surefire plugin communicates with a debugger. Choose "Standard (Socket Attach)" in Connection Type. In the Source tab, add the projects you want to debug.
How to use it:
You can now set breakpoints in your test code.
Start your "Maven debug launch" in debug
Start your Maven build with the "-Dmaven.surefire.debug" option
I found that interesting solution here, and it was useful to me (which mean I tested it, and it worked), so I wanted to share it.

If you run the Maven build with the Ecplise Maven Plugin :
Place the debug point in java code
Right click on your pom.xml or your project in Eclipse and Select "Debug As" and then "Maven Install" or If you want an other Maven goal Select "Maven build..."

Related

How to run a main method method without building the WHOLE maven project in idea?

I have a maven project with many demo classes with their own main methods, they don't dependent on each other. I used to be able to run a class by right clicking on its main method, and select Run MyClass.main(). But now instead of running it, it started to build the maven project as a whole.
Is there a way to "fix" this?
IDE performs tasks that are set in Before launch section of the Run/Debug Configuration. By default the Build set is set there - which will build the all the sources which deletd on the code you are trying to run.
You can remove the Build step from there and build the module alone from the module context menu of this module. See compile module for more description.
Use Build, no error check step in Before launch section.
Check that in the settings the "Delegate IDE build..." is not selected:

Use IDEA Run/Debug Configuration to debug Gradle web app with Jetty plugin

What's the problem?
Is it possible to debug a Gradle web app running via Jetty plugin (using jettyRun or jettyRunWar) by using a IntelliJ IDEA Run/Debug Configuration?
I know that it is possible (I've done it before) to do it with Maven/Tomcat7 plugin and creating a simple Maven Run/Debug Configuration with the clean package tomcat7:run goals.
I've tried to create a Gradle Run/Debug Configuration with the clean jettyRunWar tasks. If I Run the configuration, everything works well. But the Debug option is not working as expected: the application actually runs, but debugger won't connect.
However, I did manage to debug the project by running on debug mode externally, like this:
$ export GRADLE_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=9999,server=y,suspend=n"
$ ./gradlew clean jettyRunWar
And then creating a Remote Run/Degug Configuration with all the default configurations except the port, which was set to 9999.
But that's not the solution I was looking for. I want to be able to debug the project by only clicking a button. Am I missing something?
Not in the mood to read everything? Here's an example
I've created a simple IDEA/Gradle/Jetty web app using Jersey available on Github here. The problem can be reproduced by:
Cloning the repository
Importing it on IDEA
Creating a Gradle Run/Debug Configuration for the root project with the clean jettyRunWar tasks
Debug using the configuration
The debugger won't connect and breakpoints won't work.
I am using:
IntelliJ IDEA Community Edition 2016.1.3 (OS X)
Gradle 2.7 (a wrapper is available on the Git repository, so it doesn't matter)

Maven command line arguments in IntelliJ 14.

I've been learning Maven and absolutely love it, it's an incredible tool. I'm currently running mvn clean, build, compile, package etc from terminal. This works fine but I figure I could do all this from the IDE. However, using -clean as a command line arg in the configuration settings throws an error.
The current command is simply idea:idea. When this runs it appears to be building the project and adding resources but no mention of clean / verify etc.
I've searched various jet brains pages but can't find a commandline list. Ideally I'd like to be able to set up a couple of different configurations, these mimicking the cones that I'd call from the terminal.
thanks for any help
In intellij you have Maven Projects Tool Window which you can use to run any maven build phase or plugin goals, you can also create run configurations from them (just right click and select create).
You can also create maven run configurations by yourself using Run/Debug Configurations dialog. There should be maven type available on the list.
However, using -clean as a command line arg in the configuration settings throws an error.
You should pass just clean as command line argument.
You can read more about how to call maven for example from Maven: The Complete Reference

Maven build debug in Eclipse

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.
Easiest way I find is to:
Right click project
Debug as -> Maven build ...
In the goals field put -Dmaven.surefire.debug test
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.
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.
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
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.

Custom Maven goals in IntelliJ IDEA

I am trying to find the way to configure the custom maven goals in IntelliJ. For e.g. I want the custom goal to be: clean install -U and I want to name it: update, but I can't find how to configure this in IntelliJ. Is this even possible? If so how can I do this?
There are 2 things you can do:
You can right-click your project root in the "Maven Projects" view and select the "Create XXX [install]..." option (where XXX is your project/module name. Then, in the subsequent dialog, specify clean install -U as the command line option. This will create a run configuration that invokes maven (you can name it "update" if you want). While it won't show up in the Maven view, it will be accessible from the normal run/debug configuration.
Alternatively, you can define a new profile in your pom that rebinds "install" to "clean install -U". You won't be able to rename it to "update" in the intelliJ ui, but you can at least ensure that both clean and install are run whenever someone runs the install goal.
Another way to do that is create a new run/debug Maven configuration. There you can specify the full command line arguments and a lot of other things.
Run\Edit configurations...
Select + (to create a new one)\Maven

Categories