本文介绍了多模块Maven项目中的Nar依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了一个多模块maven项目,该项目由一个旨在构建nar jni库的模块和一个依赖于该库的jar打包模块组成.

I set up a multi module maven project, which is comprised of a module destined to build nar jni library, and a jar packaged module that is dependent on that library.

我能够将nar库安装到本地maven存储库中,但是无法在相关模块中使用它.

I am able to install the nar library to my local maven repository, but I fail to use it in dependent module.

例如,我运行mvn nar:nar-unpack,我得到:

For instance, I run mvn nar:nar-unpack and I get:

[INFO] ------------------------------------------------------------------------
[INFO] Building nar-dependent 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- nar-maven-plugin:3.2.0:nar-unpack (default-cli) @ nar-dependent ---
[INFO] Unpacking 0 dependencies to /home/przemek/Documents/stimulant/nar-dependent/target/nar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

似乎没有nar依赖性,这显然是不正确的.此外,尝试执行使用jni库的类的main方法失败:

It seems that there are no nar dependencies, which is obviously not true.Moreover, trying to execute the main method of the class that makes use of the jni library fails:

mvn exec:java -Dexec.mainClass=App

[INFO] --- exec-maven-plugin:1.4.0:java (default-cli) @ nar-dependent ---
[WARNING]
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:293)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.UnsatisfiedLinkError: no nar-library-1.0-SNAPSHOT in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1865)
    at java.lang.Runtime.loadLibrary0(Runtime.java:870)
    at java.lang.System.loadLibrary(System.java:1122)
    at jnibook.NarSystem.loadLibrary(NarSystem.java:23)
    at jnibook.HelloWorld.<clinit>(HelloWorld.java:10)
    at App.main(App.java:9)
    ... 6 more
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE

项目的结构如下:

.
├── nar-dependent
│   ├── pom.xml
│   └── src
│       └── main
│           └── java
│               └── App.java
├── nar-library
│   ├── pom.xml
│   └── src
│       ├── main
│       │   ├── c
│       │   │   └── HelloWorld.c
│       │   ├── include
│       │   ├── java
│       │   │   └── jnibook
│       │   │       └── HelloWorld.java
│       │   └── resources
│       └── test
│           └── java
├── parent
│   └── pom.xml

这是父pom.xml:

Here is the parent pom.xml:

 <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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>sidec</groupId>
    <artifactId>stimulant</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>../nar-library</module>
        <module>../nar-dependent</module>
    </modules>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.7</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

nar-library模块pom.xml:

The nar-library module pom.xml:

<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>

  <parent>
      <groupId>sidec</groupId>
      <artifactId>stimulant</artifactId>
      <version>1.0-SNAPSHOT</version>
      <relativePath>../parent/pom.xml</relativePath>
  </parent>


  <artifactId>nar-library</artifactId>
  <packaging>nar</packaging>

  <name>nar-library</name>

    <properties>
        <skipTests>true</skipTests>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>com.github.maven-nar</groupId>
                <artifactId>nar-maven-plugin</artifactId>
                <version>3.2.0</version>
                <extensions>true</extensions>
                <configuration>
                    <cpp>
                        <exceptions>false</exceptions>
                    </cpp>
                    <libraries>
                        <library>
                            <type>jni</type>
                            <linkCPP>false</linkCPP>
                            <narSystemPackage>jnibook</narSystemPackage>
                        </library>
                    </libraries>
                    <javah>
                        <includes>
                            <include></include>
                        </includes>
                    </javah>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

与nar相关的pom.xml

The nar-dependent pom.xml

<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>

  <parent>
      <groupId>sidec</groupId>
      <artifactId>stimulant</artifactId>
      <version>1.0-SNAPSHOT</version>
      <relativePath>../parent/pom.xml</relativePath>
  </parent>


  <artifactId>nar-dependent</artifactId>
  <packaging>jar</packaging>

  <name>nar-dependent</name>


    <build>
        <plugins>
            <plugin>
                <groupId>com.github.maven-nar</groupId>
                <artifactId>nar-maven-plugin</artifactId>
                <version>3.2.0</version>
                <extensions>true</extensions>
                <!--<executions>-->
                    <!--<execution>-->
                        <!--<id>nar-download</id>-->
                        <!--<goals>-->
                            <!--<goal>nar-download</goal>-->
                        <!--</goals>-->
                    <!--</execution>-->
                <!--</executions>-->
            </plugin>

        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>sidec</groupId>
            <artifactId>nar-library</artifactId>
            <version>1.0-SNAPSHOT</version>
            <type>nar</type>
        </dependency>
    </dependencies>

</project>

最后,为了证明它确实是HelloWorld项目,它是一个库类:

Finally, as a proof that it is really the HelloWorld project, a library class:

package jnibook;

public class HelloWorld {
    public native void print();


    static {
        NarSystem.loadLibrary();
    }
}

和一个客户端应用程序:

and a client app:

import jnibook.HelloWorld;

public class App {

    public static void main(String ... args){
        (new HelloWorld()).print();

    }
}

我引用了 https://maven-nar.github.io/examples.html没有成功.我不知道出了什么问题.

I referenced https://maven-nar.github.io/examples.html with no success.I have no idea what is going wrong.

有什么想法吗? 此处为项目的zip .

推荐答案

这可能是一个过时的问题,但我会一一回答:

This might be an outdated question, but I'll answer all the same :

运行Java JNI应用程序时,必须告知在哪里可以找到保存有JNI使用的相关本机C代码的.so库.例如,如果您在可执行jar jar.app中关闭了应用程序:

When running the java JNI app, it must be told where to find the .so library holding the relevant native C code used by JNI.For example, if you closed your app in the executable jar app.jar :

java -Djava.library.path=[path to the .so native C library] -jar app.jar

PS-您可以看到JVM由于以下异常而找不到本机C库:java.lang.UnsatisfiedLinkError: no nar-library-1.0-SNAPSHOT in java.library.path

PS - you can see that the JVM can't find the native C library thanks to the exception : java.lang.UnsatisfiedLinkError: no nar-library-1.0-SNAPSHOT in java.library.path

这篇关于多模块Maven项目中的Nar依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-09 22:13