3

我正在使用 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>
4

2 回答 2

4

<linkerFinalName>libjniExampleNative</linkerFinalName>伎俩。问题在于我正在运行的生命周期mvn clean install -P linux。阶段install返回。The packaging for this project did not assign a file to the build artifact

改成之后就mvn clean package -P linux可以正常使用了。现在我必须弄清楚为什么install与 冲突<linkerFinalName>libjniExampleNative</linkerFinalName>,因为我只有在设置linkerFinalName选项时才会收到错误。

于 2014-08-06T08:32:48.710 回答
0

查看源代码,我发现 native-maven-plugin 将 设置build.finalName<artifactId>,并忽略了原始的<finalName>and <linkerFinalName>: 。因此,在“解决”问题中更改<artifactId>为您想要的内容。finalName

于 2016-05-24T16:26:20.573 回答