我正在使用 JNI 和 Maven 来了解如何自动化构建过程。当涉及到输出文件名时,我在 Linux 平台上遇到了一些困难。当我运行 maven 构建(下面是 linux 平台的 pom.xml)时,返回的共享库文件名为jniExampleNative.so
. 如果我尝试用它运行 java 程序,我会得到java.lang.UnsatisfiedLinkError: no jniExampleNative in java.library.path
. 如果我将文件重命名为libjniExampleNative.so
它可以工作。
我想设置 Maven,以便它自动创建具有工作文件名的文件,但不知道如何。我尝试了设置linkerFinalName
选项(在下面的 pom.xml 中有注释),但我得到了The packaging for this project did not assign a file to the build artifact
.
我在哪里做错了事情,如何设置输出共享库的名称?
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>net.tricoder.jnitest</groupId>
<artifactId>nativeParent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>net.tricoder.jnitest</groupId>
<artifactId>jniExampleNative</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>JNI example native Linux</name>
<url>http://maven.apache.org</url>
<packaging>so</packaging>
<dependencies>
<dependency>
<groupId>net.tricoder.jnitest</groupId>
<artifactId>jniExampleJava</artifactId>
<version>1.0-SNAPSHOT</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>native-maven-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<!-- trigger javah -->
<javahOS>linux</javahOS>
<compilerProvider>generic-classic</compilerProvider>
<compilerExecutable>gcc</compilerExecutable>
<linkerExecutable>gcc</linkerExecutable>
<sources>
<source>
<directory>../src/main/native</directory>
<fileNames>
<fileName>jni_example.c</fileName>
</fileNames>
</source>
</sources>
<compilerStartOptions>
<compilerStartOption>-fPIC</compilerStartOption>
</compilerStartOptions>
<linkerStartOptions>
<linkerStartOption>-shared</linkerStartOption>
</linkerStartOptions>
<!--linkerOutputDirectory>${project.build.directory}</linkerOutputDirectory>
<linkerFinalName>libjniExampleNative</linkerFinalName-->
</configuration>
<executions>
<execution>
<id>javah</id>
<phase>generate-sources</phase>
<configuration>
<javahOS>linux</javahOS>
<javahProvider>default</javahProvider>
<javahOutputDirectory>${project.build.directory}/custom-javah</javahOutputDirectory>
<workingDirectory>${basedir}</workingDirectory>
<javahOutputFileName>NativeStuff.h</javahOutputFileName>
<javahClassNames>
<javahClassName>net.tricoder.jnitest.NativeStuff</javahClassName>
</javahClassNames>
</configuration>
<goals>
<goal>javah</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>