14

My issue is that i can't run remote debug with tomcat7-maven-plugin. What i use:

  • Maven
  • Tomcat7 plugin to maven
  • IntelliJ IDEA Ultimate 2016.2.2

Ofc Maven is provided by default with IntelliJ. I already tried with mvnDebug tomcat7:run command but intelliJ doesn't resolve mvnDebug phrase in maven's built-in command line. Can't use cmd command line neither because i can't find 'home' path since maven is built-in intelliJ. Tried configure remote-debug with intelli also but get confused. Also can't find tomcat7 plugin in "Maven Projects" window. But i'm sure it is, since tomcat7:run command starts tomcat container and app works.

Piotr Bartoch
  • 407
  • 1
  • 5
  • 18
  • 1
    Regarding not being able to see Plugins section, what _do_ you see under your project node in `Maven Projects` ? Can you upload a snapshot of Maven Projects (either embed in your question or if the upload image privilege is not yet available to you then please upload on imgur or some place and post the link here)? Also is it worth comparing your `` section with the one in the repository linked in my answer? [*Here*](https://github.com/javacreed/how-to-run-embedded-tomcat-with-maven/blob/master/pom.xml#L19) is the pom from that repo. – Ashutosh Jindal Nov 20 '16 at 17:52
  • A snippet of your `` section would also be useful. – Ashutosh Jindal Nov 20 '16 at 17:55
  • Screens of Maven Settings and Projects tab: http://imgur.com/a/B6Ys6 http://imgur.com/a/9Y554 Here is a snippet from parent pom file http://pastebin.com/cyCLrCzd – Piotr Bartoch Nov 20 '16 at 18:33
  • It's plugins section in each module doesn't have tomcat7. Here is a link to project: https://github.com/PiotrBartoch/Cats – Piotr Bartoch Nov 20 '16 at 18:39
  • Perfect! Lemme have a look at the project – Ashutosh Jindal Nov 20 '16 at 18:39
  • Is is as I suspected (and the reason why I asked for a snippet of the pom). I'll update my answer – Ashutosh Jindal Nov 20 '16 at 18:41

3 Answers3

27

In Intellij IDEA, in the Maven Projects tab, dig down to the tomcat7:run goal and then right click and select Debug like so:

enter image description here

In the abouve, notice that there is a breakpoint on Line 34 of HelloServlet.java. Now as soon as you hit the URL mapped to the servlet (http://localhost:9090/hello in this case), the breakpoint gets hit as seen below:

enter image description here

The code used to test this is at the following repository: https://github.com/javacreed/how-to-run-embedded-tomcat-with-maven

Regarding not being able to see Plugins in Maven Projects (sorry I missed that you've mentioned this), note that Plugins is not a top level node in 'Maven Projects' .. but will be under a node named taken from <name> of your project's root pom. Based on my own experience with Intellij 2016.x as well as on the fact that this functionality is pretty basic, I'd be quite surprised if this is a bug in Intellij. I'd suggest that this is either a problem with your pom.xml or a (shudder!) user error.


Update - Plugins not visible in Maven Projects

From the pom.xml (here), the tomcat7 plugin is in the build -> pluginManagement -> plugins section. This section is intended to be used in a root pom (as you have) to centralize the plugin configuration which can then be inherited by any of the child modules by simply mentioning the plugin. But without doing so, the tomcat7 plugin will not be available anywhere. Therefore, you must have a build -> plugins -> plugin section with the tomcat7 maven plugin somewhere (Also see relevant question : Maven: What is pluginManagement?)

For example the following change (here is the corresponding pull request for your repo):

    <plugins>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
        </plugin>
    </plugins>

Added to <build> section of your root pom, immediately results in the Plugins section, along with the tomcat7 goals, to appear in Maven Projects :

enter image description here

Community
  • 1
  • 1
Ashutosh Jindal
  • 18,501
  • 4
  • 62
  • 91
  • Unfortunatelly as I told above, there is no tomcat7 plugin visible in "Maven Projects" tab... Maybe You know how to tunr it on? I added plugin with pom file and it works fine for deploy purposes "tomcat7:run" only... – Piotr Bartoch Nov 20 '16 at 16:58
  • Update: I've already done this. in command line set MAVEN_DEBUG_OPTS="-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000" and configured intelliJ run/debug->edit configurations->remote-> host: localhost port:8000. And when i start debugging i got this: "Error running tomcat-7-debug: Unable to open debugger port (localhost:8000): java.net.ConnectException "Connection refused: connect" – Piotr Bartoch Nov 20 '16 at 17:06
  • 1
    I think the primary problem here is that you can't see `Plugins`. When you `Debug` on the plugin goal from within Intellij you do not need to do anything else (i.e. no need to setup MAVEN_DEBUG_OPTS etc). – Ashutosh Jindal Nov 20 '16 at 17:44
  • I've requested more information to solve the invisible `Plugins` problem. – Ashutosh Jindal Nov 20 '16 at 17:53
  • Thank's a lot! you rescued me! – Piotr Bartoch Nov 20 '16 at 19:24
14

you can run tomcat with maven with this command:

mvn tomcat7:run

and if you want to debug, set this maven options:

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

if you are in windows, use the set command:

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

then you can debug with Eclipse or Intellij.

Hope this help.

arganzheng
  • 1,294
  • 15
  • 20
1

This is a late answer but I would like to highlight an other more maven friendly solution that uses the same idea behind the answear of @arganzheng. You can actually add debuging options as part of the tomcat maven plugin configuration. The resulting pom would look like

<plugins>
   <plugin>
      <groupId>org.apache.tomcat.maven</groupId>
      <artifactId>tomcat7-maven-plugin</artifactId>
      <configuration>
         <systemProperties>
              <MAVEN_OPTS>-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000</MAVEN_OPTS>
         </systemProperties>
      </configuration>
   </plugin>
</plugins>
Youssef NAIT
  • 1,362
  • 11
  • 27
  • 1
    This did not work for me. Creating a jvm.config file in the .mvn folder of the project and adding -agentlib:jdwp=transport=dt_socket,server=y,address=8000,suspend=n worked. (Spacing is bad, start with the - sign) – downeyt Jun 17 '20 at 21:24
  • As soon as this is done and pom is reloaded, debug option is enabled on the Maven project tab. Worked perfectly, thanks. – lkamal Nov 11 '20 at 06:56
  • lkamal - what is "this" and what do you mean by "done"? – Alex Worden Jan 24 '21 at 23:09