GITHUB PROJECT: https://github.com/Pi4J/pi4j-maven-archetype
Writing your Java program, compiling and running it directly on the Raspberry Pi board as shown in the previous chapter is perfectly fine, of course, but there is an alternative way to arrange your developing laboratory, using a normal desktop computer as Remote Developing Workstation (RDW).
This Maven Archetype will give you a tool to generate Pi4J V.2 skeleton Java projects. You can use it for your next Pi4j project and you will be able to develop your program on the remote workstation (RDW), compile them, transmit the executable code on the target Pi board and run it. You can also start a remote debugging session.
There are some pros. and cons. in such a developing arrangement:
The Headless Mode configuration enables the RPi board to communicate with the RDW over SSH protocol.
These are the needed steps:
You should now be able to open a SSH Terminal window on RDW and to remotely login on the RPi board.
Goto the Github Pi4J Maven Archetype repository and download the project clicking on the green Code button and selecting Download ZIP
cd FOLDER/pi4j-maven-archetype-main
mvn install
Congratulation ! - Now you are ready to generate your first Project Template
Let suppose you want to begin the new wonderful PI4J V.2 project my-project, to do this follow these steps:
mkdir my-project
cd my-project
mvn archetype:generate -DarchetypeCatalog=local
Before starting the new project generation, the archetype asks some configuration data. The list of question and the replies are shown here below:
The archetype now shows you a summary of the configuration parameters you have just typed in, plus the values proposed for the main-class and package parameters. If the list is ok for you, reply Y to accept, otherwise reply N to change one or more values (you will have to re-type all parameter values …)
After the list confirmation, the archetype generates a new maven project template for you.
Congratulations
You should be able to open the new project with your preferred java IDE. The IDE should be able to recognize the project as a valid Maven project.
If you are not using the default Raspberry Pi OS full edition and/or included Java, you may get this kind of error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:3.0.0:run (exec) on project ...: An Ant BuildException has occured: The following error occurred while executing this line:
[ERROR] ...\antrun\build.xml:166: The following error occurred while executing this line:
[ERROR] ...\antrun\build.xml:123: Remote command failed with exit status 1
[ERROR] around Ant part ...... @ 9:59 in ...\antrun\build-main.xml
This can be caused by a mis-configured Java runtime. The default value in raspberry.properties
is:
target.remote.jre=/usr/lib/jvm/default-java
Check if this value exists and links to your Java runtime, or find the location of your installed JDK with
sudo find / -iname java
and use the result in your configuration.
For instance: a Raspberry Pi Zero (type 1) with ARMv6 requires a specific Java version for this type of processor. This is described more in detail on “Java for ARMv6/7/8”. If you use Azul Zulu JDK, you will need to change the configuration to:
target.remote.jre=/usr/lib/jvm/zulu11.41.75-ca-jdk11.0.8-linux_aarch32hf
Feel free to explore the new project familiarizing with the folder structure. These are the most important features:
Thanks to Adi and Luca Buraggi for this contribution.