141

Can you debug a maven goal with Intellij IDEA? I know that I can right-click and run Debug. However, the maven plugin does not appear in my External Libraries list, so I can not go into the code and set a breakpoint. Thus, Debug runs through the goals without stopping, like Run does.

I am using OS X 10.8 and IDEA 12.0.2.

EDIT: Goal

I've written custom specRunner for https://github.com/searls/jasmine-maven-plugin - However, $specs$ stays empty. So I try to see which files are actually loaded.

kc2001
  • 5,008
  • 4
  • 51
  • 92
rweng
  • 6,736
  • 7
  • 27
  • 30
  • 3
    _debug a maven goal_? what is that _maven goal_? what do you want to achieve? and what is your actual problem? – Alonso Dominguez Jan 30 '13 at 11:10
  • With maven goal I mean jasmine:test, or jasmine:bdd. As far as I've seen, each Mojo must implement an execute() method which I'd like to debug. However, since I have no access to the jar from IDEA, I cannot set a breakpoint. – rweng Jan 30 '13 at 14:33

9 Answers9

248

Figured it out:

  • from the command line, run maven goal with mvnDebug instead of mvn. E.g. mvnDebug clean
  • Open the source of the maven plugin you want to debug in intelliJ and set a breakPoint
  • In IDEA, add a Remote JVM Debug Configuration.
    1. Under Settings set:
      • Transport: Socket
      • Debugger Mode: Attach
      • Host: localhost
      • Port: 8000 (default port of mvnDebug)
  1. Run the Configuration in Debug mode. It should connect to the waiting mvnDebug jvm.
radistao
  • 14,889
  • 11
  • 66
  • 92
rweng
  • 6,736
  • 7
  • 27
  • 30
  • 7
    How do you open the source of the maven plugin in Intellij? – CorayThan Jan 18 '15 at 01:49
  • 1
    I think I cloned it from github in the correct version. The rest happened automatically. – rweng Jan 19 '15 at 19:45
  • 7
    What if I want to run tests using `mvn test -Dtest=com.example.MyTest` (my test case fails only when run using maven) - IDEA breakpoints are ignored in this case, it seems... :( – RobertG Jun 20 '17 at 14:52
  • 3
    Found it: In https://stackoverflow.com/q/6573289/1143126, this basically the qustion asked – RobertG Jun 20 '17 at 14:53
  • Specifically, here is how to debug a maven goal with surefire: https://stackoverflow.com/a/8339155/929708 – JJ Brown Mar 09 '19 at 17:38
  • 1
    @rweng works flawlessly. Was able to debug the plugin in minutes. Thanks! – Gaurav Jan 29 '20 at 05:15
  • 1
    [This](https://spin.atomicobject.com/2020/08/20/maven-debugging-intellij/) guide has screenshots too. – Leponzo Aug 30 '22 at 15:04
59

Very easy. I am using Intellj Idea 15.0.4

  1. Set the breakpoint in your maven plugin
  2. In the tag "Maven Projects" go to the project witch is using your maven plugin.
  3. In "Plugins" find your plugin and over the goal right click and Debug

Here is a screenshot:

screenshot

bibi
  • 3,671
  • 5
  • 34
  • 50
gorums
  • 1,567
  • 13
  • 5
  • 1
    Although this answer has a negative score, it did help me fix the issue I had (how to debug a maven-based project in IntelliJ), and when "Googling" the issue it led me to this page. – gbmhunter Oct 17 '16 at 21:31
  • Thank you sir, your answer helped me to figure how to use maven plugins automatically during run in idea – Anatoly Yakimchuk Dec 16 '16 at 13:18
  • This actually is a very good answer. For me it's definitely easier and simpler to do it this way than to set up a remote debug (as suggested in the accepted answer). Not sure why the other one was accepted. Perhaps it wouldn't work for the situation described in the question, but if definitely worked for me, so thanks! – Chanandler Bong May 10 '18 at 12:16
  • 4
    "Set the breakpoint in your maven plugin" I'm not sure what that actually means? Is that different than putting a breakpoint in the java class itself? – Alkanshel Mar 18 '19 at 19:50
  • 1
    "Set the breakpoint in your maven plugin" means put the breakpoint in your maven plugin class – gorums Mar 19 '19 at 21:35
  • Thanks a lot. In case the maven tags is not showed in your maven tools proceed like this: View/Tools Windows/Maven – Enrico Giurin Oct 12 '19 at 17:09
  • I think the problem is, its unclear what the "maven plugin" is. I'm not buidling a maven plugin, I'm trying to debug a java application. As such, my application does not show up in the mavin plugin list. – Hephaestus Feb 12 '21 at 06:21
  • On the IntelliJ new releases, the menu is just called Maven, and is on the right side. – Luiz Rossi Jul 06 '21 at 13:52
29

Old question, but I was having the same need and it took me a while to get it to work. Hopefully can help someone.

For test debugging use:

mvn <goal> -Dmaven.surefire.debug

or

mvn <goal> -Dmaven.failsafe.debug

When execution stops and listens to socket at address 5005 (default) you run your configured remote debugger.

How to configure it:

Run -> Edit configurations -> Remote Transport: socket Debugger mode: Attach Port: 5005 (default)

-> Save.

DimaSan
  • 12,264
  • 11
  • 65
  • 75
vilkg
  • 625
  • 6
  • 7
18

I think the easiest solution is to temporarily add the maven plugin as a dependency. Once this is done, IntelliJ will treat this just like any other dependency and you can set breakpoints the usual way.

Peter Szanto
  • 7,568
  • 2
  • 51
  • 53
  • 2
    dot this, and instead of mvn use: mvnDebug ..., then create a remote debug runner in intellij on port 8000 – user85155 May 08 '17 at 19:44
  • 1
    A 1000x this! I was trying to debug a third-party plugin and adding it as a normal Maven dependency meant I could easily download the source code, search for the class that I knew had the problem (based on the Github changelog) and set a breakpoint. Right-clicking on the desired Maven target and selecting 'Debug' as others have mentioned worked perfectly – Patrick Herrera Aug 29 '20 at 08:10
15

The easiest way to debug maven goal ONLY within IntelliJ is to create a regular maven goal and in the runner tab pass those VM options:

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000

Where 8000 is a port number for remote debugging.

Maven goal configuration

Then create new Remote configuration with port 8000. Run this configuration after running maven goal.

Remote debugging configuration

Michał Stochmal
  • 5,895
  • 4
  • 36
  • 44
5

No need to setup up Java Remote Debugger or anything like that. It is literally just a right click -> debug on the Maven goal now, as explained in the official docs.

andras
  • 3,305
  • 5
  • 30
  • 45
3

Either You can refer to above answer Or just add this plugin to pom.xml

           <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
            <jvmArguments>
            -Xdebug - 
            Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000
            </jvmArguments>
            </configuration>
        </plugin>

And run maven goal with mvn instead of mvnDebug. E.g. mvn spring-boot:run

In IDEA, add a Remote Configuration. Under Settings, set Transport: Socket, Debugger Mode: Attach, Host: localhost, Port: 8000 (default port of mvnDebug).

Run as Debug in IDEA , whenever you want to debug the code.

abhishek ringsia
  • 1,970
  • 2
  • 20
  • 28
2

Since you are working with Intellij, there is already a built-in debugger there and you do not need to necessarily use mvnDebug which is a command line tool. Check out this tutorial: How to Debug Maven Applications in Intellij IDEA.

The tutorial uses the Maven Exec Plugin and lets you debug the application without a need to use the command line or MvnDebug. Thought sharing it might be of value here.

ambodi
  • 6,116
  • 2
  • 32
  • 22
  • 2
    The tutorial only covers debugging `exec:java`. However, if you need to provide additional arguments for JVM, you need to use `exec:exec` (See http://stackoverflow.com/questions/7305090/how-to-run-a-java-project-with-vm-arguments-from-command-line), which cannot be debugged that way. – mirelon Mar 13 '14 at 10:37
2

@Peter Szanto 's answer work for me, but I don't like mess my source code.

And I can't make those MvnDebug way work.

So I try another way, add plugin source as IDEA module.

Here is the detail step:

  1. Clone the plugin source as an independent project.

  2. In your project, go to File -> New -> Module from Exist Sources and add the plugin project you clone in step 1.

  3. Now you can open the plugin source code and set some break point.

  4. Run your maven goal as debug mode, it should stop at the break point.

min
  • 953
  • 1
  • 11
  • 23