多模块maven项目中的Nar依赖

时间:2015-08-10 13:06:19

标签: maven java-native-interface maven-nar-plugin

我设置了一个多模块maven项目,该项目由一个用于构建nar jni库的模块和一个依赖于该库的jar打包模块组成。

我能够将nar库安装到我的本地maven存储库,但是我无法在依赖模块中使用它。

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

[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方法失败:

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:

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

<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

<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项目的库类:

package jnibook;

public class HelloWorld {
    public native void print();


    static {
        NarSystem.loadLibrary();
    }
} 

和客户端应用:

import jnibook.HelloWorld;

public class App {

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

    }
}

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

有什么想法吗? Here is zip with project

1 个答案:

答案 0 :(得分:0)

我已尝试使用该插件的3.6.0版开发您自己的示例。使用该版本,我至少会得到

    Unpacking 1 dependencies to /home/karsten/svn/hellotest/target/nar

,当我在从属模块中运行.so时,mvn nar:nar-unpack会被解压缩。 但是我发现使mvn nar:nar-integration-test在依赖模块中工作的唯一方法是通过编写

    LD_LIBRARY_PATH=target/nar/hellojni-0.0-amd64-Linux-gpp-jni/lib/amd64-Linux-gpp/jni mvn nar:nar-integration-test

我尝试了几种指定java.library.path的方法,但没有成功。