Adding a JNI library to the local Maven Repository - java

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 myself on my local repository to have it working.
How do I go about including the native part in Maven to be bundled in my project (and eventually exported with the copy-dependencies plugin). This is a standard J2SE app (not a web-app), with packaging .jar?
The library I am trying to add is junixsocket, just in case it helps to know.
It has a .so (native library) component, and the Java .jar component.
I came across maven-nar-plugin which seems to target native builds, but seems to be more oriented towards building a JNI project from code, rather than bundling a 3rd party JNI library, and I can't get to piece the jigsaw puzzle together.
How do I go about:
Installing these in my local repository, having the .jar depending on the .so library.
Including the dependency (on the .jar and .so) in the POM file.
Thanks.

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.

Related

How to import Processing libraries into Java application

I am calling Processing functions from Java code.
This works fine for the standard Processing classes, but how to you import other Processing libraries; e.g. gicentre?
I've actually got it working by extracting the jar file from the processing library and then manually installing the artifact into the maven project.
Is there a proper way to do it?
Add this dependancy in your maven pom.xml file.
<!-- mvnrepository.com/artifact/org.processing/core -->
<dependency>
<groupId>org.processing</groupId>
<artifactId>core</artifactId>
<version>2.2.1</version>
</dependency>
Sandip's answer will work for the core Processing library (with the caveat that you should use the latest version, not version 2.2.1), but like you've discovered, gicentre doesn't have a maven repository.
You can download the various gicentre libraries from this page. Each of those libraries comes as a .zip file that contains a .jar file.
Now that you have the .jar file, it's just a matter of adding that .jar to your classpath. How you do that depends on how you've set up your project. The simplest way to do it is to use the command line to compile your project, and then you'd use the -cp argument. You've said you're using Maven, so Googling "maven local jar" will lead to a ton of results, including this one: How to add local jar files to a Maven project?
But note that you don't have to use Maven. You could just set the classpath yourself, either via the command line or via your IDE settings. For simple projects, this can be a better option, especially if Maven is giving you trouble.

Using third-party libraries in Eclipse RCP Tycho app

I've created a boiler-plate project following vogella's extensive Tycho tutorial.
Facts:
There's no feature, and there's no plugin. The only plugin is the RCP app, which is also the entry-point.
Problem:
I have no idea in which pom.xml do I include the 3rd party dependencies.
I cannot include them in the RCP project, because the packaging of that pom is eclipse-plugin, and not jar. From what I've noticed, if I change the packaging to jar, then the "Maven Dependencies" library is added automatically. If I change back to eclipse-plugin, they get removed.
Questions:
Where do I add the dependencies? There's no pom with jar packaging in my project.
Should I create a separate project with the necessary JARs? How do I include that dependency to my entire project?
Is it really that much of a good practice to create a separate plugin and a feature for this RCP app?
Related solutions:
"Update projects" doesn't work, and neither do the n other solutions in the other SO questions.
There's also this question and that question, but I don't fully get the answers
I think that you have a fundamental misunderstanding.
Maven: Maven determines all of the project dependencies via the pom.xml and resolves transitive dependencies automatically (assuming that all of the pom files and artifacts exist in repositories that you've configured and correctly declare their dependencies).
Tycho: The problem is that Eclipse already has its own project model based on product files, feature.xml files, and plug-in MANIFEST.MF files. Tycho leverages the Maven machinery for Eclipse, but the idea is that the pom.xml files just configure the Maven plug-ins and declare the packaging type. That provides an entry point for Maven, but then Tycho takes over. While Maven would normally build the dependency chain from information in the pom.xml files, Tycho is building the dependency change from information in the product, feature, and MANIFEST.MF files. You don't put any dependencies in the pom.xml files. Tycho also uses Eclipse p2 repositories (instead of normal Maven repositories) for finding dependent plug-ins that are not found in the local modules or target platform.
That's actually a benefit for many Eclipse developers since they've already set up everything properly in their Eclipse plug-ins, features, and products. They do not want to have to repeat all of the dependencies in the pom.xml.
Using Libraries in Eclipse plug-ins: In Eclipse, if you want to use a library that is not already packaged as an Eclipse plug-in, you have a few options. Your plug-in can include a set of JARs in a libs folder and then include that libs folder in the plug-in and runtime classpath (see the build.properties file). Another option is to create your own "library plug-in" that repackages a JAR library as an Eclipse plug-in. See also https://wiki.eclipse.org/FAQ_What_is_the_classpath_of_a_plug-in%3F. That's the answer that you're getting above.
The problem is that if you're trying to include a complex library with multiple JARs that is normally distributed and included in a standard Java project via Maven. We hit this problem with the Jersey JAX-RS implementation in my project. There's no p2 repository that includes all of the pieces of the libraries as plug-ins with correct dependency information.
Easy Solution: If you need a common library, check the Orbit project first to see whether the libraries have already been packaged as Eclipse plug-ins, http://www.eclipse.org/orbit/. In that case, you can download them and include them in your target platform, or you can pull them in dynamically at (Tycho) build time from their p2 repository. Your plug-ins would just include those plug-ins as dependencies (in the their MANIFEST.MF files).
Workaround / Solution: In our case, Jersey JAX-RS was not available as an Eclipse plug-in, and it had a bunch of transitive dependencies. The workaround was to create an Eclipse "library plug-in" like I mentioned above with two pom files. We initially created a skeleton plug-in with an empty libs folder. One pom file is just a standard Maven pom file with <packaging>jar</packaging> that declares the top-level dependencies required to pull in the Jersey JAX-RS implementation and all of its dependencies. The dependencies are declared with <scope>compile</scope>. We use the maven-dependency-plugin to copy all of those dependencies into the project's libs folder.
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>compile</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>libs</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
We actually ended up running Maven with that pom by hand from time to time to update the libs, and then we just checked the plug-in with all of its dependent JARs into source control. Checking the build later, I see that we actually populate the libs folder on-the-fly with Maven with a separate build task just before we start the Maven/Tycho part of the build. Of course, plug-in's MANIFEST-MF file's Bundle-ClassPath and Export-Package entries are coming straight from source control. We have to check those from time to time to ensure that they match the libraries and packages that we're getting from Maven. (That doesn't tend to change much unless we bump major library versions or add a new dependency at the Maven level.) The plug-in's build.properties has the libs/ folder as part of bin.includes.
In the development environment, after we first check out the code, we just run mvn (with an External Tools launch config that's also checked in with the project) on the project's "copy dependencies" pom file. That populates the libs folder with all of the JAX-RS libraries and dependencies. We only have to run it again when we update something about the dependencies or when we're jumping between branches that have different versions of the JAX-RS dependencies. We set .gitignore to ensure that we don't commit the libs to Git.
The other pom for this project is set up like a normal Tycho pom file with <packaging>eclipse-plugin</packaging>. During our automated build, we run one step early in the build process (just after check out) that calls mvn with the jar pom to populate the libs. Then we proceed with the main Maven/Tycho build using the eclipse-plugin pom. The eclipse-plugin pom has no dependency information (as I said above). It's just providing Tycho a way to recognize the Eclipse plug-in and build it based on its MANIFEST.MF and build.properties files. But the built plug-in includes and exposes all of those libs that were populated by the mvn call to the jar pom step.
So, it's a bit of a mess, but that's the best solution we found a couple of years ago when we hit this problem. I'm not sure whether Tycho is doing any work to permit some sort of hybrid Maven/Tycho build that could do this automatically as part of the build. I guess I should ask the developers. :)
Your questions:
Where do I add the dependencies? There's no pom with jar packaging in my project. Answer: The workaround above lets you do it with one project. You just have two pom files, like pom_deps.xml and pom.xml. You just have to invoke the pom_deps.xml separately to populate the libs folder (in the dev environment and with your automated builds).
Should I create a separate project with the necessary JARs? How do I include that dependency to my entire project? Answer: the workaround that I described above lets you do it with a single project. Another way to do it is to create a separate JAR project, but I don't think that your Eclipse RCP app can really include a <packaging>jar</packaging> module in a useful way. The only way I've found to do it is to use a similar workaround. You build the JAR module first, install it into the maven repository, and then have one of your plug-in projects bundle the JAR in its libs folder. (If you really want to do it that way, ask. We have a case where we have to do that, too, and I can provide the steps we do in development and the build to make it work. I think the single project workaround that I provided above makes more sense for your case.)
Is it really that much of a good practice to create a separate plugin and a feature for this RCP app? Answer: that's really a separate question. If you have a feature with multiple plug-ins, you have the same problem. Tycho can handle the product/feature/plug-ins, but it cannot jump across into Maven-based dependency resolution. You'll end up having to use the same workarounds
Summary: The fundamental issue is that Eclipse plug-ins can't "see" a bare JAR library. The plug-in needs to have the library included in its local libs folder (with a matching Bundle-ClassPath entry in MANIFEST.MF), or it needs to depend on some other plug-in that exports the appropriate packages. Tycho just resolves dependencies via Eclipse plug-ins, and it cannot leverage normal Maven dependency resolution directly to pull in a bunch of JARs. If all of your dependencies are already plug-ins, you're fine. If not, you may have to use the workaround above to package a set of libraries for your plug-ins to use.
Just adding the plugin to pom dependencies and including the entry <pomDependencies>consider</pomDependencies> in the configuration of target-platform-configuration makes it work.
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>target-platform-configuration</artifactId>
<version>${tycho.version}</version>
<configuration>
<!-- The configuration to make tycho consider the maven dependencies -->
<pomDependencies>consider</pomDependencies>
<!-- other configurations -->
</configuartion>
</plugin>
<!-- other plugins-->
</plugins>
<dependencies>
<!-- An example third-party bundle (plugin) present in maven repository-->
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.gogo.shell</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
Reference link here.

Maven Dll Dependency Problem

I am developing a swing based desktop application and this application is using MediaInfo.dll library. I have first installed this dll to my local repository like described this answer. Then I have added a dependency in my pom.xml like this
<dependency>
<groupId>com.mediainfo</groupId>
<artifactId>mediainfo</artifactId>
<version>0.7.44</version>
<type>dll</type>
</dependency>
I am using Maven 3.0.3 version and when I executed mvn install assembly:assembly it says
PlexusIoResourceCollection not found, no archiever found for dll
I am new to maven, so I am searching for a help before pulling my hear out .
This will depend on your assembly descriptor, but it seems like you have a <dependencySet> under which <unpack>true</unpack> is specified, that does not exclude the DLL from the set. Try adding this into that dependencySet element:
<excludes>
<exclude>*:dll*</exclude>
</excludes>
If you intend to incorporate the DLL dependencies without unpacking them, then you might need an additional dependencySet element that includes them and doesn't specify the unpack flag. See http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html#class_dependencySet for more information.

How do I add a NAR dependency to a maven pom file?

I'm trying to use JNI, and I'm using the maven-nar-plugin to package up the C++ code into a NAR. Now, I want to include this code in another Java project. How do I go about doing this properly?
It depends on how you want to use the dependencies created by maven nar plugin. From the documentation,
Other maven projects may specify
dependencies on these nar files using
the standard maven dependency
declaration. Nar files get downloaded,
unpacked and installed in the local
maven repository, just like jar files
are (apart from the unpacking).
The FAQ has a section on how to load the native library from java code.
Hope these help.

Building a maven project with external JNI libraries

I'm developing a program on MacOSx that uses third party jar files that all use JNI to call C functions.
How can I include those on my build path and set the java.library.path to use the external dependencies properly ?
I would like to avoid having to install anything in my maven repository. This is important since I'll have to deploy my program to other linux platforms as well, which already have those third-party jars and C libraries installed somewhere...
For now what I've done is adding my jar dependencies with scope=system and systemPath pointing to the full path of my jar files, but I don't know how to set the os-specific dependencies...
Regards,
Philippe
Could you use:
<dependency>
<groupId>foo</groupId>
<artifactId>bar</artifactId>
<version>1.0.0</version>
<scope>provided</scope>
<systemPath>/my/path/to/c_libs_folder/myexecutable.jar</systemPath>
</dependency>

Categories

Resources