使用Eclipse IDE遠端偵錯Raspberry Pi上的java maven專案:握手錯誤

使用Eclipse IDE遠端偵錯Raspberry Pi上的java maven專案:握手錯誤

我正在嘗試在 Ubuntu 20.04 上配置 Eclipse IDE (2021-09 (4.21.0)) 以遠端偵錯 RaspberryPi 上的 java 應用程式。我遵循了多個“教程”(連結1,連結2)。透過結合兩者以及在網路上對錯誤訊息進行深入搜索,我能夠重新建立 pom.xml 和 build.xml

pom.xml

<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org

/2001/XMLSchema-instance"
  xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>pi</groupId>
  <artifactId>hello</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>hello</name>
  <url>http://maven.apache.org</url>

<build>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
               <archive>
                  <manifest>
                     <mainClass>pi.hello.App</mainClass>
                  </manifest>
               </archive>
            </configuration>
         </plugin>
      </plugins>
   </build>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>6</maven.compiler.source>
    <maven.compiler.target>1.6</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

建置.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="hello" default="remote-run" basedir="."
    xmlns:artifact="antlib:org.apache.maven.artifact.ant">

    <!-- Setup RASPBERRY PI properties -->
    <property name="raspberrypi" value="192.168.4.2" />
    <property name="raspberryfolder" value="~" />
    <property name="username" value="pi" />
    <property name="password" value="raspberry" />

    <!--
    <path id="maven-ant-tasks.classpath" path="${ant.libs.dir}/maven-ant-tasks-2.1.3.jar" />
    <typedef 
        resource="org/apache/maven/artifact/ant/antlib.xml"
        uri="antlib:org.apache.maven.artifact.ant"
        classpathref="maven-ant-tasks.classpath" />

        
    <path id="maven-ant-tasks.classpath" path="lib/maven-ant-tasks-2.1.3.jar" />
    <typedef 
        resource="org/apache/maven/artifact/ant/antlib.xml" 
        uri="antlib:org.apache.maven.artifact.ant" 
        classpathref="maven-ant-tasks.classpath" />
    -->

    <!-- Add maven install target to be run before deploy -->
        
    <target name="maven-install"> 
        <artifact:mvn pom="pom.xml"> 
            <arg value="install"/> 
        </artifact:mvn> 
    </target> 

    <!-- Locate the prokect jar and transfer via scp to RASPBERRY PI -->
    <target name="transfer" depends="maven-install">
        <first id="jars">
            <!--
            <fileset dir="target" includes="**/*-SNAPSHOT-jar-with-dependencies.jar" />
            -->
            <fileset dir="target" includes="**/*.jar" />
        </first>
        <pathconvert pathsep="," property="jar.path" refid="jars" />
        <basename file="${jar.path}" property="jar.filename" />
        <echo>">>> Found application ${jar.path}"</echo>

        <echo>">>> Copying application to ${raspberrypi}:${raspberryfolder}/${jar.filename}"</echo>
        <scp 
            localfile="${jar.path}" 
            todir="${username}:${password}@${raspberrypi}:${raspberryfolder}" 
            trust="true" />

    </target>
        
    <!-- Run java -->
    <target name="remote-run" depends="transfer">   
        <echo>">>> Starting ${raspberrypi}:${raspberryfolder}/${jar.filename}"</echo>

        <sshexec 
            host="${raspberrypi}" 
            username="${username}" 
            password="${password}" 
            trust="true" 
            failonerror="true" 
            usepty="true" 
            command="java -jar ${jar.filename}" />
    </target>
    
    <!-- Run java in debug mode and keep waiting for execution -->
    <target name="remote-debug" depends="transfer"> 
        <echo>">>> Starting ${raspberrypi}:${raspberryfolder}/${jar.filename} in debug mode"</echo>
        <sshexec 
            host="${raspberrypi}" 
            username="${username}" 
            password="${password}" 
            trust="true" 
            failonerror="true" 
            usepty="true" 
            command="java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=y -jar ${jar.filename}" />
    </target>
</project>

使用編譯 build.xml 時的輸出

運行方式 -> Ant 構建

結果是

maven-install:
[artifact:mvn] [INFO] Scanning for projects...
[artifact:mvn] [INFO] ------------------------------------------------------------------------
[artifact:mvn] [INFO] Building hello
[artifact:mvn] [INFO]    task-segment: [install]
[artifact:mvn] [INFO] ------------------------------------------------------------------------
[artifact:mvn] [INFO] [resources:resources]
[artifact:mvn] [INFO] Using 'UTF-8' encoding to copy filtered resources.
[artifact:mvn] [INFO] skip non existing resourceDirectory /media/stefan/DATA/Electronics/java/hello/src/main/resources
[artifact:mvn] [INFO] [compiler:compile]
[artifact:mvn] [INFO] Nothing to compile - all classes are up to date
[artifact:mvn] [INFO] [resources:testResources]
[artifact:mvn] [INFO] Using 'UTF-8' encoding to copy filtered resources.
[artifact:mvn] [INFO] skip non existing resourceDirectory /media/stefan/DATA/Electronics/java/hello/src/test/resources
[artifact:mvn] [INFO] [compiler:testCompile]
[artifact:mvn] [INFO] Nothing to compile - all classes are up to date
[artifact:mvn] [INFO] [surefire:test]
[artifact:mvn] [INFO] Surefire report directory: /media/stefan/DATA/Electronics/java/hello/target/surefire-reports
[artifact:mvn] -------------------------------------------------------
[artifact:mvn]  T E S T S
[artifact:mvn] -------------------------------------------------------
[artifact:mvn] Running pi.hello.AppTest
[artifact:mvn] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.021 sec
[artifact:mvn] Results :
[artifact:mvn] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[artifact:mvn] [INFO] [jar:jar]
[artifact:mvn] [INFO] [install:install]
[artifact:mvn] [INFO] Installing /media/stefan/DATA/Electronics/java/hello/target/hello-0.0.1-SNAPSHOT.jar to /home/stefan/.m2/repository/pi/hello/0.0.1-SNAPSHOT/hello-0.0.1-SNAPSHOT.jar
[artifact:mvn] [INFO] ------------------------------------------------------------------------
[artifact:mvn] [INFO] BUILD SUCCESSFUL
[artifact:mvn] [INFO] ------------------------------------------------------------------------
[artifact:mvn] [INFO] Total time: 1 second
[artifact:mvn] [INFO] Finished at: Tue Nov 30 17:12:36 CET 2021
[artifact:mvn] [INFO] Final Memory: 18M/70M
[artifact:mvn] [INFO] ------------------------------------------------------------------------
transfer:
     [echo] ">>> Found application /media/stefan/DATA/Electronics/java/hello/target/hello-0.0.1-SNAPSHOT.jar"
     [echo] ">>> Copying application to 192.168.4.2:~/hello-0.0.1-SNAPSHOT.jar"
      [scp] Connecting to 192.168.4.2:22
      [scp] done.
remote-run:
     [echo] ">>> Starting 192.168.4.2:~/hello-0.0.1-SNAPSHOT.jar"
  [sshexec] Connecting to 192.168.4.2:22
  [sshexec] cmd : java -jar hello-0.0.1-SNAPSHOT.jar
  [sshexec] Hello World!
  [sshexec] 
BUILD SUCCESSFUL
Total time: 6 seconds

maven 專案的預設 build.xml(透過精靈建立)的所有其他外掛程式或工件組合確實會產生錯誤,因為某些外掛程式中的版本錯誤(即使安裝了正確的版本)

解析「org.apache.maven.plugins:maven-resources-plugin」版本時發生錯誤:插件需要 Maven 版本 3.0

目前版本

mvn -version
Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 11.0.11, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.11.0-40-generic", arch: "amd64", family: "unix"

為了開始工作,我寧願不更改當前的 build.xml。

對於配置的最後一步,我需要讓遠端啟動器正常工作。按照教程,配置視窗如下所示 偵錯配置

測試連接結果

錯誤虛擬機

由於建置過程能夠透過 ssh 連接,放置並運行 jar 我不知道為什麼無法建立連接。連接的參數儲存在 pom.xml 中我不確定調試配置中的條目的作用。可以使用 ssh 連線 (ssh pi@) 的命令列命令在 RPi 上建立終端。不應該存在安全存取問題。

  1. 當pom.xml中已經定義了連線時,為什麼遠端設定還需要位址、連接埠?
  2. 是否使用“Debug_As->RPi_Remote”(已建立的偵錯配置)來呼叫 build.xml?
  3. 如何呼叫 build.xml 並啟用斷點?

相關內容