【问题标题】:Pick up native JNI files in Maven test (lwjgl)在 Maven 测试中提取原生 JNI 文件 (lwjgl)
【发布时间】:2011-06-01 01:08:58
【问题描述】:

我正在使用 LWJGL 和 Maven 创建一个程序,并且正在为图形代码编写单元测试。我的问题是让 Maven 将本机二进制文件放在类路径中,以便测试可以拾取它。我无法克服错误:

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

我已将二进制文件解压到 target/libs/native/,但测试无法提取它们。

这是我的 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>

我尝试过 Maven - Add directory to classpath while executing tests ,但这似乎是在谈论资源,而不是 JNI 库(它不起作用)。

Specifiy classpath for maven 是相反的问题:指定已经在类路径上的东西。

【问题讨论】:

  • 我在“”上有一个红色标记,m2e 不支持。你是怎么改变的?

标签: java maven-2 java-native-interface lwjgl


【解决方案1】:

根据http://maven.40175.n5.nabble.com/Trouble-with-Java-Native-Libraries-td114063.html

surefire 插件启动 VM,然后在将控制权传递给 junit 测试类之前修改系统属性。这对 VM 来说太晚了,因为 VM 需要在初始化 VM 时设置 java.library.path。

所以我们需要在启动时给出 Surefire 的路径。以下工作:

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

surefire配置是否包括设置java库路径

例如:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-06
    • 2012-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 2018-07-13
    • 2018-11-03
    相关资源
    最近更新 更多