java 将 JNI 库添加到本地 Maven 存储库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10071058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:28:49  来源:igfitidea点击:

Adding a JNI library to the local Maven Repository

javamavenjava-native-interfacemaven-nar-plugin

提问by jbx

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 库,包括它的共享对象 (.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). This is a standard J2SE app (not a web-app), with packaging .jar?

我如何将 Maven 中的本机部分包含在我的项目中(并最终与 copy-dependencies 插件一起导出)。这是一个标准的 J2SE 应用程序(不是网络应用程序),带有 .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.

我要添加的库是junixsocket,以防万一它有助于了解。它有一个 .so(本机库)组件和 Java .jar 组件。

I came across maven-nar-pluginwhich 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 项目,而不是捆绑 3rd 方 JNI 库,而且我无法将拼图拼凑在一起.

How do I go about:

我该怎么做:

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

Thanks.

谢谢。

回答by alexkasko

My approach:

我的做法:

Put .sofiles to repository with platform specific classifier, like this: sqlite3-3.7.9-linux-x86_64.so. Add .sodependencies for all required platforms:

.so文件存储库平台的具体分类,如:sqlite3-3.7.9-linux-x86_64.so.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/nativedirectory of you dist:

使用这个 Maven 程序集插件配置将所有本机库放入lib/native您的 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 classto load libs on app startup (planning to change classifier naming to GNU triplets):

使用此类在应用程序启动时加载库(计划将分类器命名更改为GNU 三元组):

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

回答by Peter Lawrey

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.

我在 jar 中包含 .so 并在加载之前额外添加平台特定的共享库。通过这种方式,它就像任何其他 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

完成此操作的项目示例,针对不同平台使用多个 .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

要查看的主要类是https://github.com/peter-lawrey/Java-Thread-Affinity/blob/master/src/main/java/com/higherfrequencytrading/affinity/impl/NativeAffinity.java

回答by Dan Berindei

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.

作为在运行时解压缩库的替代方法,您可以将它们作为 jar 存储在 Maven 中,但在构建时解压缩它们:http: //www.buildanddeploy.com/node/17

The maven-nativedependencies-pluginplugin will do this for you automatically, as long as you follow their naming convention.

只要您遵循它们的命名约定,maven-nativedependencies-plugin插件就会自动为您执行此操作。