【问题标题】:How to Deploy a jar onto Raspberry Pi 2 using Maven Plugin如何使用 Maven 插件将 jar 部署到 Raspberry Pi 2
【发布时间】:2016-02-02 08:07:26
【问题描述】:

我有一个使用 Pi4J 的简单 java 类,其中包含一个 main 方法。我想构建并将其部署到 Raspberry Pi。我在 Windows 10 上使用 java 8,我的 IDE 是 NetBeans 8.1。如果项目设置为普通(非 Maven)Java 应用程序并设置为通过 NetBeans 构建配置部署到 Raspberry Pi,如 here 所示,则项目将通过 jar 构建正常和 FTP,然后运行它没有问题。 我想做同样的事情,但使用 Maven。问题是正在运行的代码具有 ARM 的 Pi 架构的原生 C 代码,而我正在使用的 Maven 插件似乎首先在我的本地机器上运行 jar,其中 C 代码将崩溃并抛出 Exception

严重:无法使用路径加载 [libpi4j.so]:[/lib/libpi4j.so] java.lang.IllegalArgumentException:路径必须是绝对的,但发现:\lib\libpi4j.so 在 com.pi4j.util.NativeLibraryLoader.loadLibraryFromClasspath(NativeLibraryLoader.java:120) 在 com.pi4j.util.NativeLibraryLoader.load(NativeLibraryLoader.java:92) 在 com.pi4j.wiringpi.Gpio.(Gpio.java:174) 在 com.pi4j.io.gpio.RaspiGpioProvider.(RaspiGpioProvider.java:51) 在 com.pi4j.io.gpio.GpioFactory.getDefaultProvider(GpioFactory.java:106) 在 com.pi4j.io.gpio.impl.GpioControllerImpl.(GpioControllerImpl.java:54) 在 com.pi4j.io.gpio.GpioFactory.getInstance(GpioFactory.java:89) 在 com.hadronix.pi4jsample2.ControlGpioExample.main(ControlGpioExample.java:24)

线程“主”java.lang.UnsatisfiedLinkError 中的异常: com.pi4j.wiringpi.Gpio.wiringPiSetup()我在 com.pi4j.wiringpi.Gpio.wiringPiSetup(本机方法)在 com.pi4j.io.gpio.RaspiGpioProvider.(RaspiGpioProvider.java:51) 在 com.pi4j.io.gpio.GpioFactory.getDefaultProvider(GpioFactory.java:106) 在 com.pi4j.io.gpio.impl.GpioControllerImpl.(GpioControllerImpl.java:54) 在 com.pi4j.io.gpio.GpioFactory.getInstance(GpioFactory.java:89) 在 com.hadronix.pi4jsample2.ControlGpioExample.main(ControlGpioExample.java:24)

如果我只是做一个干净的构建,它将构建良好并创建 jar。但是,如果我选择运行构建,那么这就是崩溃发生的地方。这是我用来将 jar 部署到 Pi 的 Maven 插件。

<!-- OPTIONALLY DEPLOY THE FINAL JAR TO THE RASPBERRY PI -->
        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>

                <!-- copy the compiled JAR file to the Raspberry Pi platform platform -->
                <execution>
                    <id>ControlGpioExample.jar</id>
                    <phase>install</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <tasks>
                            <taskdef resource="net/sf/antcontrib/antcontrib.properties"
                                     classpathref="maven.plugin.classpath" />
                            <if>
                                <equals arg1="${pi.transfer.dev}" arg2="true" />
                                <then>
                                    <!-- ensure the target directory exists on the Raspberry Pi -->
                                    <sshexec host="${pi.host.dev}" port="${pi.port.dev}" username="${pi.user.dev}"
                                                                                     password="${pi.password.dev}" trust="true" failonerror="false"
                                                                                     verbose="true" command="mkdir --parents ${pi.dirCopyTo.dev}" />
                                    <!-- copy the JAR file to the Raspberry Pi -->
                                    <scp
                                        file="${project.build.directory}/${project.build.finalName}.jar"
                                        todir="${pi.user.dev}:${pi.password.dev}@${pi.host.dev}:${pi.dirCopyTo.dev}"
                                        port="${pi.port.dev}" trust="true" verbose="true" failonerror="true">
                                    </scp>
                                </then>
                            </if>
                        </tasks>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.apache.ant</groupId>
                    <artifactId>ant-jsch</artifactId>
                    <version>${ant-jsch.version}</version>
                </dependency>
                <dependency>
                    <groupId>com.jcraft</groupId>
                    <artifactId>jsch</artifactId>
                    <version>${jsch.version}</version>
                </dependency>
                <dependency>
                    <groupId>ant-contrib</groupId>
                    <artifactId>ant-contrib</artifactId>
                    <version>${ant-contrib.version}</version>
                </dependency>
            </dependencies>
        </plugin>

我正在尝试弄清楚如何更改此插件以不在我的本地计算机上首先执行代码。我只是希望它假设 jar 已构建,ssh 到远程 Pi 并复制 jar,然后在那里运行它。此外,如果有人在使用 Maven 时有更好的方法,那么我愿意接受建议。任何帮助都会很棒!

这里是我用作参考的链接 link1

提前致谢。

【问题讨论】:

    标签: java maven netbeans ant raspberry-pi2


    【解决方案1】:

    已解决:此 pom 文件将构建 Pi4J 示例用例的可执行 JAR。然后它将 JAR 传输到 Raspberry Pi 并执行它。在 netbeans 中,这是通过 build 命令完成的。所以一键构建、部署和运行!

    我不喜欢的一件事是它将依赖项作为包而不是原始 JAR。它还在构建时创建了 2 个 JAR;一个有依赖,一个没有依赖。打开 Jar,看看可以做些什么来改善结构。随意优化并提供反馈。

    另外,为了简单起见,我将我的 RASPBERRY PI PROPERTIES 放在这个 pom 文件中。但是,这些应该位于 settings.xml 文件或其他文件中的受控用户主位置。

    注意:Pi4J 有一个 C 代码本机 ARM 依赖项,这会阻止它在非 ARM 架构上运行,并且必须发送到 Pi。

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.company</groupId>
    <artifactId>Swarm</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    
        <!-- DEFAULT RASPBERRY PI PROPERTIES -->
        <pi.host>192.168.1.20</pi.host>
        <pi.port>22</pi.port>
        <pi.user>root</pi.user>
        <pi.password>root</pi.password>
        <pi.deployDirectory>/home/pi/artifacts</pi.deployDirectory>
        <pi.main.class>com.company.test.ControlGpioExample</pi.main.class>
    
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.pi4j</groupId>
            <artifactId>pi4j-core</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
    
            <!-- This plugin will generate JAR MANIFEST file inside the JAR in order to make our applicationeasily runnable -->
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>${pi.main.class}</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-my-jar-with-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
    
            <!--This plugin will Transfer the executable JAR file to the Pi and runs it -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.8</version>
                <executions>
                    <execution>
                        <phase>install</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <!-- ensure the target directory exists on the Raspberry Pi -->
                                <sshexec host="${pi.host}" port="${pi.port}" username="${pi.user}" password="${pi.password}" 
                                         trust="true" failonerror="false" verbose="true" 
                                         command="mkdir --parents ${pi.deployDirectory}"/>
    
                                <!-- copy the JAR file to the Raspberry Pi -->
                                <scp
                                    file="${project.build.directory}/${project.build.finalName}-jar-with-dependencies.jar"
                                    todir="${pi.user}:${pi.password}@${pi.host}:${pi.deployDirectory}"
                                    port="${pi.port}" trust="true" verbose="true" failonerror="true">
                                </scp> 
    
                                <!-- run the JAR file on the Raspberry Pi -->
                                <sshexec host="${pi.host}" port="${pi.port}" username="${pi.user}"
                                         password="${pi.password}" trust="true" failonerror="false"
                                         verbose="true" 
                                         command="java -jar ${pi.deployDirectory}/${project.build.finalName}-jar-with-dependencies.jar"/>
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.ant</groupId>
                        <artifactId>ant-jsch</artifactId>
                        <version>1.9.6</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
    

    【讨论】:

    • 可以用这个从Netbeans调试远程运行的jar吗?
    猜你喜欢
    • 2018-06-28
    • 2019-01-26
    • 2013-08-22
    • 2018-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多