I'm building an app for RaspberryPi that uses Pi4j. It worked fine until I encountered a need to change Pi4j version from 1.1 to 1.2-SNAPSHOT. I get NoCassDefFoundException
when using SNAPSHOT only.
I'm using a few plugins to separate my jar and all libraries, which are stored in lib/*
directory. maven-dependency-plugin
does the job
maven-jar-plugin
links directory with libs to my jar
maven-antrun-plugin
copies my jar and only changed dependencies to my raspberry during mvn install
My pom.xml
file (stripped a lot)
<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>Rpi</groupId>
<artifactId>Rpi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<!--<pi4j.version>1.2-SNAPSHOT</pi4j.version>-->
<pi4j.version>1.1</pi4j.version>
</properties>
<dependencies>
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-core</artifactId>
<version>${pi4j.version}</version>
</dependency>
(...)
</dependencies>
<repositories>
<repository>
<id>oss-snapshots-repo</id>
<name>Sonatype OSS Maven Repository</name>
<url>https://oss.sonatype.org/content/groups/public</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>true</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>${pi.main.class}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<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>
<scp
file="${project.build.directory}/${project.build.finalName}.jar"
todir="${pi.user}:${pi.password}@${pi.host}:${pi.deployDirectory}"
port="${pi.port}" trust="true" verbose="false" failonerror="false">
</scp>
<!-- copy dependencies, only new or modified-->
<scp todir="${pi.user}:${pi.password}@${pi.host}:${pi.deployDirectory}/lib"
port="${pi.port}" trust="true" verbose="false" failonerror="false">
<fileset dir="${project.build.directory}/lib">
<modified>
<param name="cache.cachefile" value="localdev.cache"/>
</modified>
</fileset>
</scp>
</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>
After mvn install
everything looks fine, my jar is where it should be, all dependencies are copied, I run my jar using java -jar Rpi-0.0.1-SNAPSHOT.jar
and it runs, until a NoClassDefFoundError
gets raised.
The problem is when I use Pi4j 1.2-SNAPSHOT only. When I use version 1.1 it works fine (well, crashes, that's why I have to switch to 1.2-SNAPSHOT). If I use version 1.1 in pom.xml and after mvn install
rename lib\pi4j-core-1.2-SNAPSHOT.jar
to lib\pi4j-core-1.1.jar
everything runs as expected, no crashes, using version 1.2...
Maven-jar-plugin
changed name of SNAPSHOT dependency in my jar classpath. To fix this I had to add <useUniqueVersions>false</useUniqueVersions>
to the plugin manifest, so now it looks like this
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>${pi.main.class}</mainClass>
<useUniqueVersions>false</useUniqueVersions>
</manifest>
</archive>
</configuration>
</plugin>
and works fine