簡體   English   中英

如何在 Eclipse 中調試 Maven 應用程序

[英]How to debug a Maven application in Eclipse

讓我首先澄清一下,我已經檢查了所有可能的資源、教程、視頻和其他我可以得到的相關的 stackoverflow 問題,以便嘗試找到答案。 I'm using java 8 and Eclipse Luna Service Release 2 (4.4.2) with m2e plugin , It's quite hard to believe but it seems there isn't a single example that clearly explains how you actually debug a Maven Java application USING ECLIPSE. 這里有不少於 40 個不同的來源

我想要做的是單擊 eclipse 中的調試按鈕並運行允許我命中斷點的調試配置。

我在Eclipse中有如下配置

在此處輸入圖像描述

這從我的 pom.xml 運行 exec:exec 目標,看起來像這樣

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <executable>java</executable>
                    <arguments>
                        <argument>-classpath</argument>
                        <classpath />
                        <argument>core.app.server</argument>
                        <argument>-agentlib:jdwp=transport=dt_socket,suspend=y,server=y,address=*:49875 </argument>
                    </arguments>
                    <workingDirectory>${project.build.outputDirectory}</workingDirectory>
                </configuration>
            </plugin>

好的,到目前為止一切順利。 此時,如果我運行調試配置,應用程序啟動然后掛起,我假設它正在等待調試器基於我的 pom.xml 中的 arguments 遠程連接。 所以應用程序開始掛起,此時我正在查看 Eclipse

在這一點上,我已經嘗試了我能想象到的一切。 我注意到 exec 插件啟動器在圖片中為 51661 的隨機端口上啟動,當我在 pom.xml 中的我的 exec arguments 中將其設置為 49875 所以這似乎關閉了。 如果我從我的 pom.xml 中刪除了<argument>-agentlib:jdwp=transport=dt_socket,suspend=y,server=y,address=*:49875 </argument>行,我注意到的另一件事是應用程序完全啟動,運行良好但這讓我無處可去。 在應用程序啟動后,我嘗試使用“遠程 Java 應用程序”配置進行連接,這也不起作用。 我通常使用 IntelliJ,這讓它變得輕而易舉,因為一切都是 OOTB 處理的,不幸的是,我必須以類似的方式在 Eclipse 中讓它工作。

如何以允許我啟動應用程序並命中斷點的方式配置調試配置?

編輯 1當設置suspend=n時,應用程序在使用參數agentlib:jdwp啟動時仍然掛起

編輯 2為了在不費力的情況下再浪費一天的時間弄清楚這一點,我測試了運行clean install和調試配置,然后使用如下所示的 m2e 提供的命令運行測試,m2e 命令完美運行由於缺少引用,使用調試配置運行相同的確切命令失敗。

EDIT 3 Reading the documentation on the exec maven plugin here https://www.mojohaus.org/exec-maven-plugin/index.html it says the difference between exec:exec and exec:java is that the ladder execute programs and Java程序在一個單獨的進程中,前者在同一 VM 中執行 Java 程序。 我認為這可能與我遇到的問題有關。 對於熟悉 MAven/Eclipse 的人來說,這應該很容易測試我認為,是否有人能夠創建一個超級基本的 hello world 應用程序 maven 項目,看看他們是否可以在主要方法中設置並達到斷點,應該采取但5-10 分鍾?

我讓它像這樣工作:

  • 下載、解壓(最新??) Luna SR2 JavaEE 版
  • 在 eclipse.ini 中設置(正確) JAVA_HOME / vm (oracle-8 最新)
  • 歡迎屏幕,新建 Maven 項目,簡單,跳過 achetype...

(所以使用新的默認值,簡單的項目):

使用以下 pom 部分:

<build>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <version>3.0.0</version>
      <executions>
        <execution>
          <goals>
            <goal>exec</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <executable>C:\Program Files\Java\jdk1.8.0_333\bin\java.exe</executable>
        <arguments>
          <argument>-agentlib:jdwp=transport=dt_socket,suspend=y,server=y,address=49875</argument>
          <argument>-classpath</argument>
          <classpath />
          <argument>com.example.Demo</argument>
        </arguments>
      <workingDirectory>${project.build.outputDirectory</workingDirectory>
      </configuration>
    </plugin>
  </plugins>
</build>

(與您的非常相似,只有顯着區別: addressserver在這種方法中至關重要,也可以suspend

使用相同的(Maven)運行配置,如您所示:

我們現在需要:

  • “調試配置...”
  • 遠程 Java 應用程序(。..因為我們選擇了“服務器”..)
  • 我們將 select (來源)項目作為“項目”
  • 連接類型:附加
  • 主機:本地主機
  • 端口:jdwp中配置的那個

好像:

運行配置 2

  1. 在以下位置設置“我的”斷點:
package com.example;

public class Demo {

    public static void main(String[] args) {
        int x = 0;
        x++; // just to set breakpoint somewhere
        System.out.println(x);
    }

}
  1. “運行”“Exec_Java”(掛起和服務器標志(和所有)都有效!)

  2. Console(Exec_Java) 應該報告:

     ... [INFO] --- exec-maven-plugin:3.0.0:exec (default-cli) @ luna-res --- Listening for transport dt_socket at address: 49875 //!!
  3. “調試”“New_Configuration”。

..和斷點停止(,我們得到“切換透視對話框”;)

成功

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM