Setting Up LWJGL with Maven

'This is a semi-tutorial including a complete POM. To get just the POM snippets, go here: LWJGL use in Maven'

Introduction
This tutorial assumes you have no familiarity with Maven, but it is not a Maven tutorial. For that, I recommend Maven in Five Minutes for a lightning tour, and Maven: The Complete Reference for a more comprehensive guide (despite "reference" in the name, it's an excellent intro to Maven).

All commands should work equally well in Windows or Unix (Linux, Solaris, OSX) operating systems, except where I otherwise give separate examples.

Step 1: Install Maven
If you don't have maven installed already, download the latest version from http://maven.apache.org/download.html and install it anywhere you like (I like C:\devel\maven on windows and /opt/maven or ~/opt/maven on unix). Make sure that wherever you install it, you have maven's bin/ directory on your PATH; in other words, just typing "mvn" on the command line results in running maven.

Linux users: you might have maven in your package manager, but if it's not Maven 3, you should go ahead and install it from maven.apache.org anyway.

Step 2: Creating a project
First off, if you have a favorite IDE already, whether it's Eclipse, NetBeans, or IDEA, you should use its maven features to create a project instead of following this step. Assuming you want to continue using maven as a "command line IDE" and possibly import it as a new IDE project later, then read on..

First, change to the parent directory your project will be. I tend to use C:\proj or ~/proj so that's where I assume you are. Your first maven command will be a doozy:

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=myproject -DartifactId=mygame -Dversion=1.0-SNAPSHOT

If this is the first time you've run maven, you'll wait while maven downloads the entire internet -- or at least several dozen libraries -- before it runs anything else. You'll only have to sit through this once when you run new maven commands, and after that they're cached. At the end, it'll ask you to confirm, so go ahead and hit Enter, or fix the settings however you want.

Step 3: Edit the POM
Maven creates a directory named for your artifactId (which I'll assume here is "mygame"). Change to that directory. Under this directory, Maven has created a src directory tree structure, and another file called pom.xml. Open that file up in your favorite editor or your IDE (if you're in Eclipse, change to the "source" tab at the bottom of the editor pane). What this file contains is an uninteresting project definition, so just replace the entire contents of the file with this (click the 'view source' button on the upper right of the xml below to get something you can copy/paste).

   4.0.0

myProject myGame 1.0-SNAPSHOT

UTF-8 0.0.6 2.8.2 274</slick.version> <junit.version>4.10</junit.version>

<id>mavenNatives</id> Maven Natives Repository http://mavennatives.googlecode.com/svn/repo true

<id>slick</id> slick http://slick.cokeandcode.com/mavenrepo

org.lwjgl.lwjgl</groupId> lwjgl</artifactId> ${lwjgl.version} slick</groupId> slick</artifactId> ${slick.version} java3d</groupId> vecmath</artifactId> 1.3.1            junit</groupId> junit</artifactId> ${junit.version} test

org.apache.maven.plugins</groupId> maven-compiler-plugin</artifactId> 1.6

com.googlecode.mavennatives</groupId> maven-nativedependencies-plugin</artifactId> ${natives.version} <id>unpacknatives</id> generate-resources copy

You'll notice I've added some extra dependencies like Slick2D and java3d's vecmath package. If you don't want or need them, feel free to delete them. There's also a plugin defintion for "mavennatives", which is really handy for LWJGL projects, so you'll want to keep it there.

Step 4: Edit and Run
At this point, edit your source files, which should live under <tt>src/main/java</tt>. Add unit tests under <tt>src/test/java</tt> if desired. For executing from the command line, you need one last setup item, and that's to set the MAVEN_OPTS environment variable:

windows: set MAVEN_OPTS="-Djava.library.path=target/natives" unix: export MAVEN_OPTS=-Djava.library.path=target/natives

When you've done that, you can run your example:

mvn compile exec:java -Dexec.mainClass=my.package.MainClass

In your IDE, you need to set your <tt>java.library.path</tt> variable the same as you would for an LWJGL project without maven. Just set it to <tt>target/natives</tt> under your project root.

Native Libraries can't be found
Make sure the target/natives directory exists! If it doesn't, run <tt>mvn package</tt> which should create it.

Make sure you set your java.library.path as given above. If you're using an IDE, you need to add it to your launch settings as described in the setup guides on this wiki. If you're launching from the command line, make sure you set MAVEN_OPTS as given in Step 4 (note that -Djava.library.path does not work as a normal vm argument in maven's exec:java goal, and you must use MAVEN_OPTS instead).