I wish to add a JNI library, including its shared object (.so) file to my project using Maven. Unfortunately it is not yet on a public repository so I guess I have to install it
My approach:
Put .so
files to repository with platform specific classifier, like this: sqlite3-3.7.9-linux-x86_64.so
.
Add .so
dependencies for all required platforms:
<dependency>
<groupId>de.ch-werner</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.7.9</version>
<type>so</type>
<classifier>linux-x86_64</classifier>
</dependency>
Use this maven assembly plugin config to put all native libs into lib/native
directory of you dist:
<dependencySet>
<outputDirectory>lib/native</outputDirectory>
<outputFileNameMapping>${artifact.artifactId}${dashClassifier?}.${artifact.extension}</outputFileNameMapping>
<unpack>false</unpack>
<useProjectArtifact>false</useProjectArtifact>
<useStrictFiltering>false</useStrictFiltering>
<includes>
<include>*:*:dll:*</include>
<include>*:*:so:*</include>
<include>*:*:jnilib:*</include>
</includes>
</dependencySet>
Use this class to load libs on app startup (planning to change classifier naming to GNU triplets):
CtzJniUtils.loadJniLibsFromStandardPath(Launcher.class, "sqlite3")
I include the .so in the jar and extra the platform specific shared library before loading it. This way it is deployed just like any other jar.
An example of a project where this is done, with multiple .so for different platforms is https://github.com/peter-lawrey/Java-Thread-Affinity
The main class to look at is https://github.com/peter-lawrey/Java-Thread-Affinity/blob/master/src/main/java/com/higherfrequencytrading/affinity/impl/NativeAffinity.java
As an alternative to unpacking your libraries at runtime, you could store them as jars in Maven but unpack them at build time: http://www.buildanddeploy.com/node/17.
The maven-nativedependencies-plugin plugin will do this for you automatically, as long as you follow their naming convention.