Pick up native JNI files in Maven test (lwjgl)

拜拜、爱过 提交于 2019-12-18 13:35:06

问题


I'm creating a program with LWJGL and Maven, and I'm writing unit tests for the graphical code. My problem is getting Maven to put the native binaries on the classpath so that the tests can pick it up. I can't get past the error:

java.lang.UnsatisfiedLinkError: no lwjgl in java.library.path

I've gotten the binaries to unpack to target/libs/native/, but the tests won't pick them up.

Here's my pom:

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

<groupId>com.ziroby.kata</groupId>
<artifactId>app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <lwjgl.version>2.6</lwjgl.version>
</properties>

<repositories>
    <repository>## Heading ##
        <id>lwjgl</id>
        <name>lwjgl</name>
        <url>http://adterrasperaspera.com/lwjgl</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-library</artifactId>
        <version>1.2.1</version>
        <type>jar</type>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jmock</groupId>
        <artifactId>jmock-junit4</artifactId>
        <version>2.5.1</version>
        <type>jar</type>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.lwjgl</groupId>
        <artifactId>lwjgl</artifactId>
        <version>${lwjgl.version}</version>
    </dependency>
    <dependency>
        <groupId>org.lwjgl</groupId>
        <artifactId>lwjgl-util</artifactId>
        <version>${lwjgl.version}</version>
    </dependency>
    <dependency>
        <groupId>org.lwjgl</groupId>
        <artifactId>lwjgl-native</artifactId>
        <version>2.6</version>
        <type>pom</type>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>org.lwjgl</groupId>
                        <artifactId>lwjgl-native</artifactId>
                        <version>${lwjgl.version}</version>
                        <type>jar</type>
                        <outputDirectory>${project.build.directory}/libs/natives</outputDirectory>
                        <overWrite>true</overWrite>
                    </artifactItem>
                </artifactItems>
            </configuration>
            <executions>
                <execution>
                    <id>unpack</id>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>unpack</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
    </plugins>
</build>
</project>

I've tried Maven - Add directory to classpath while executing tests , but that seems to be talking about resources, not JNI libraries (and it didn't work).

And Specifiy classpath for maven is the opposite problem: Specify things that are already on the classpath.


回答1:


According to http://maven.40175.n5.nabble.com/Trouble-with-Java-Native-Libraries-td114063.html,

the surefire plugin starts the VM and then modifies the system properties before passing control to the junit test classes. This is too late for the VM, which needs to have the java.library.path set up at the time the VM is initialized.

So we need to give the path to Surefire at startup. The following worked:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <forkMode>once</forkMode>
                <argLine>-Djava.library.path=${project.build.directory}/libs/natives/win32:${project.build.directory}/libs/natives/linux:${project.build.directory}/libs/natives/macosx:${project.build.directory}/libs/natives/solaris</argLine>
            </configuration>
        </plugin>



回答2:


Does surefire configuration include setting java library path

For example:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
      <systemProperties>
        <property>
          <name>java.library.path</name>
          <value>target/lib/natives/</value>
        </property>
      </systemProperties>
    </configuration>
  </plugin>



回答3:


...
<project ...>
    ...
    <properties>
        ...
        <natives.version>0.0.6</natives.version>
    <properties>
    ...
    <build>
        ...
        <plugins>
            ...
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <forkMode>once</forkMode>
                    <argLine>-Djava.library.path=target/natives</argLine>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.googlecode.mavennatives</groupId>
                <artifactId>maven-nativedependencies-plugin</artifactId>
                <version>${natives.version}</version>
                <executions>
                    <execution>
                        <id>unpacknatives</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <!--suppress MavenModelInspection (this line is for IDEA)-->
                            <goal>copy</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        ...
    </build>
    ...
</project>


来源:https://stackoverflow.com/questions/6195234/pick-up-native-jni-files-in-maven-test-lwjgl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!