Tuesday, March 17, 2015

Deploying jar to Raspberry Pi Using Intellij and Maven

I decided to switch from python to java for the Raspberry Pi controller for the robot. I wanted to see how good Java on the pi is not that Oracle are releasing proper JVM support for the Raspberry Pi ARM Processors.

I ran the first few hello world examples in Netbeans. I was really impressed with the remote platform. However I didn't want to use ant and netbeans. I wanted to use Intellij and Maven for developing and building the code for the Raspberry Pi.

The remote platform support (Netbeans Howto incase your interested) in Netbeans is excellent for working with the Raspberry Pi but as the project grows I want to stick with my familiar world.

I had three problems to solve;
  1. How to package the dependencies into an executable jar for deployment onto the Pi
  2. Deploying the built jar to the Pi
  3. Remote running and debugging the code on the pi.
For now I have got an acceptable answer to the first two. Number three will have to wait until it gets on my nerves more.

1. Packaging dependencies

I decided the simplest way to do this would be to use the maven shade plugin in my pom file. 

Maven Shade plugin in pom file:
Notice the transformer starting at line 7. This sets the main class in the manifest to make this an executable jar file. The shade plugin grabs all the transient dependencies and packages them in the jar file for you.

2. Deploying the built jar on the Raspberry Pi

This is a bit of a cheat for getting the jar file on the pi. I'm sure there are better ways and creating a remote runtime tool would be better but for this phase of development it makes it super easy for me just to run maven:install and the jar is shaded as above then copied to the pi using antrun tasks. First the task creates the folder specified if it doesn't exist then copies the build single executable jar to the pi using scp. Very convenient for simple testing.


Now the code is on the pi I still have to run it on the pi. But this is much simpler as it's now just one command

pi@raspberrypi ~ $ java -jar /home/pi/pihex/pihexj-1.0-SNAPSHOT.jar
[main] INFO com.margic.pihex.PiHex - Starting PiHex application
[main] DEBUG com.margic.adafruitpwm.AdafruitServoDriver - Creating new servo driver Adafruit-PCA9685
pi@raspberrypi ~ $ 

No comments:

Post a Comment