我正在尝试在 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 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 上建立终端。不应该存在安全访问问题。
- 为什么当连接已经在pom.xml中定义时,远程配置需要地址和端口?
- 是否甚至使用“Debug_As->RPi_Remote”(创建的调试配置)调用build.xml?
- 我如何调用 build.xml 并启用断点的使用?