Search code examples
javamavenlwjglpom.xml

Maven add natives to library path


I want to include LWJGL via Maven in my project. I get the .jar files but the natives are not in the classpath.

With the help of google I found out that I should use mavennatives in order to automatically extract and copy the natives. However mavennatives will only find the natives that start with native- and the LWJGL-natives all have names like {artifactId}-{version}-natives-{os}.jar.

Question: How can I get Maven to import the dependencies with the proper names and extract these natives?

My pom.xml:

<project ... >
     ...
    <build>
        <plugins>
            ...
            <plugin>
                <groupId>com.googlecode.mavennatives</groupId>
                <artifactId>maven-nativedependencies-plugin</artifactId>
                <version>0.0.7</version>
                <executions>
                    <execution>
                        <id>unpacknatives</id>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        ...
        <dependency>
            <groupId>org.lwjgl</groupId>
            <artifactId>lwjgl</artifactId>
            <version>3.0.0a</version>
        </dependency>
        <dependency>
            <groupId>org.lwjgl</groupId>
            <artifactId>lwjgl-platform</artifactId>
            <version>3.0.0a</version>
            <classifier>natives-linux</classifier>
        </dependency>
        <dependency>
            <groupId>org.lwjgl</groupId>
            <artifactId>lwjgl-platform</artifactId>
            <version>3.0.0a</version>
            <classifier>natives-windows</classifier>
        </dependency>
    </dependencies>
</project>

Solution

  • From official documentation of the plugin:

    This plugin unpacks every dependency with a classifier beginning with natives-.

    That's exactly your use case, the documentation points to the classifier element, which in your case is natives-linux or natives-windows.

    Accordingo to the documentation, theses cases will be handled:

    This are the default values, when enabling separateDirs the plugin will unpack each native dependency to a subdir of the nativesTargetDir named like its classifier (for example: natives-windows will go to target/natives/windows)

    Indeed the whole library's jar is in the form of {artifactId}-{version}-natives-{os}.jar but in Maven the classifier is exactly the string between the {version} and the extension of the file: in this case natives-{os}, which starts by natives and as such is handled by the library.