How to Deploy a jar onto Raspberry Pi 2 using Maven Plugin

前端 未结 1 1716
孤独总比滥情好
孤独总比滥情好 2021-02-10 17:42

I have a simple java class using Pi4J that contains a main method. I want to build and deploy it to a Raspberry Pi. I\'m using java 8 on a windows 10 and my IDE is NetBeans 8.1.

相关标签:
1条回答
  • 2021-02-10 18:35

    Solved: This pom file will build an executable JAR of example use cases of Pi4J. It will then transfer the JAR to the Raspberry Pi and then execute it. In netbeans, This is done via the build command. So one button to build, deploy and run!

    One thing I don't like is that it brings dependencies as packages instead of the original JAR. It also creates 2 JAR's at build time; one with and one without dependencies. Open up the Jar and see what can be done to better the structure. Feel free to optimize and give feedback.

    Also, for simplicity purposes on this answer, I put my RASPBERRY PI PROPERTIES in this pom file. However, those should be in a controlled user home location in a settings.xml file or something.

    Note: Pi4J has a C code native ARM dependencies which prevent it from running on non ARM architectures and must be sent to 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>
    

    0 讨论(0)
提交回复
热议问题