简体   繁体   English

将 JNI 库添加到本地 Maven Repository

[英]Adding a JNI library to the local Maven Repository

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.我希望使用 Maven 添加一个 JNI 库,包括它的共享 object (.so) 文件到我的项目中。不幸的是,它还没有在公共存储库中,所以我想我必须自己将它安装在我的本地存储库中才能让它工作。

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).我 go 如何将 Maven 中的本机部分包含在我的项目中(并最终使用复制依赖项插件导出)。 This is a standard J2SE app (not a web-app), with packaging.jar?这是一个标准的 J2SE 应用程序(不是网络应用程序),带有 packaging.jar?

The library I am trying to add is junixsocket , just in case it helps to know.我要添加的库是junixsocket ,以防万一它有助于了解。 It has a.so (native library) component, and the Java.jar component.它有一个 .so(本机库)组件和 Java.jar 组件。

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.我遇到了maven-nar-plugin ,它似乎以本机构建为目标,但似乎更倾向于从代码构建 JNI 项目,而不是捆绑第 3 方 JNI 库,而且我无法将拼图拼凑在一起.

How do I go about:我如何 go 关于:

  1. Installing these in my local repository, having the.jar depending on the.so library.在我的本地存储库中安装这些,具有 .jar 取决于 .so 库。
  2. Including the dependency (on the.jar and.so) in the POM file.在 POM 文件中包含依赖项(对 .jar 和 .so)。

Thanks.谢谢。

My approach:我的方法:

Put .so files to repository with platform specific classifier, like this: sqlite3-3.7.9-linux-x86_64.so ..so文件放入具有平台特定分类器的存储库,如下所示: sqlite3-3.7.9-linux-x86_64.so Add .so dependencies for all required platforms:为所有需要的平台添加.so依赖项:

<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:使用此 maven 程序集插件配置将所有本机库放入您的 dist 的lib/native目录中:

<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 ):使用这个 class在应用程序启动时加载库(计划将分类器命名更改为GNU 三元组):

CtzJniUtils.loadJniLibsFromStandardPath(Launcher.class, "sqlite3")

I include the.so in the jar and extra the platform specific shared library before loading it.在加载它之前,我在 jar 中包含了 .so 和额外的平台特定共享库。 This way it is deployed just like any other jar.通过这种方式,它的部署方式与任何其他 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完成此操作的项目示例,针对不同平台使用 multiple.so 是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要查看的主要 class 是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 .作为在运行时解压库的替代方法,您可以将它们存储为 jars 中的 Maven 但在构建时解压它们: 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. maven-nativedependencies-plugin插件会自动为你做这件事,只要你遵循他们的命名约定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM