如何使用 IntelliJ IDEA 在调试模式下 运行 tomcat7-maven-plugin

How to run tomcat7-maven-plugin in debug mode with IntelliJ IDEA

我的问题是我无法 运行 使用 tomcat7-maven-plugin 进行远程调试。 我用什么:

IntelliJ 默认提供 Ofc Maven。我已经尝试过 mvnDebug tomcat7:run 命令,但 intelliJ 不解析 maven 内置命令行中的 mvnDebug 短语。也不能使用 cmd 命令行,因为我找不到 'home' 路径,因为 maven 是内置的 intelliJ。也尝试使用智能配置远程调试但感到困惑。 在 "Maven Projects" window 中也找不到 tomcat7 插件。但我敢肯定,因为 tomcat7:run 命令启动 tomcat 容器和应用程序。

在 Intellij IDEA 中,在 Maven Projects 选项卡中,向下挖掘到 tomcat7:run 目标,然后右键单击并 select Debug 如下所示:

在上面,注意HelloServlet.java的第34行有一个断点。现在,一旦您命中映射到 servlet 的 URL(在本例中为 http://localhost:9090/hello),就会命中断点,如下所示:

用于测试的代码位于以下存储库中:https://github.com/javacreed/how-to-run-embedded-tomcat-with-maven

关于无法在 Maven Projects 中看到 Plugins(抱歉我错过了您提到的这一点),请注意 Plugins 不是 [= 中的顶级节点55=] .. 但将位于从项目根 pom 的 <name> 中获取的名为节点下。根据我自己使用 Intellij 2016.x 的经验以及此功能非常基本的事实,如果这是 Intellij 中的错误,我会感到非常惊讶。我建议这要么是您的 pom.xml 的问题,要么是(不寒而栗!)用户错误。


更新 - PluginsMaven Projects

中不可见

来自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?)

例如以下更改(here 是您的回购的相应拉取请求):

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

添加到根 pom 的 <build> 部分,立即导致插件部分以及 tomcat7 目标出现在 Maven Projects 中:

你可以 运行 tomcat 使用 maven 命令:

mvn tomcat7:run

如果你想调试,设置这个maven选项:

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

如果你在windows,使用设置命令:

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

然后你就可以用 Eclipse 或 Intellij 调试了。

希望对您有所帮助。

这是一个迟到的答案,但我想强调另一个更 maven 友好的解决方案,它在 @arganzheng 的 answear 背后使用相同的想法。您实际上可以添加调试选项作为 tomcat maven 插件配置的一部分。生成的 pom 看起来像

<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>