It’s really simple, actually. I’ll describe it in terms of getting a JME app up and running.
Step 1: have gradle installed so that you can run gradle from the command line.
Step 2: make a directory somewhere that you want your project. We’ll call it ‘pie’ for the sake of this discussion.
md pie
Step 3: make the src directories and a .java file in it. I’ll use a sort of example similar to the one the SDK makes.
cd pie
md src
md src/main
md src/main/java
md src/main/java/mygame
Now create the Main.java file in there:
package mygame;
import com.jme3.app.SimpleApplication;
import com.jme3.math.ColorRGBA;
import com.jme3.material.Material;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
public class Main extends SimpleApplication {
public static void main( String... args ) {
Main main = new Main();
main.start();
}
@Override
public void simpleInitApp() {
Box box = new Box(1, 1, 1);
Geometry geom = new Geometry("My Box", box);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Blue);
geom.setMaterial(mat);
rootNode.attachChild(geom);
}
}
So far… that’s pretty much how you’d start any Java project from the command line.
Now… let’s create the gradle build file.
Step 4: Create the gadle build file (from the pie directory)
gradle init
Step 5: Edit the build file
It starts off looking like this:
/*
* This build file was auto generated by running the Gradle 'init' task
* by 'PSpeed' at '1/28/16 5:04 PM' with Gradle 2.10
*
* This generated file contains a commented-out sample Java project to get you started.
* For more details take a look at the Java Quickstart chapter in the Gradle
* user guide available at https://docs.gradle.org/2.10/userguide/tutorial_java_projects.html
*/
/*
// Apply the java plugin to add support for Java
apply plugin: 'java'
// In this section you declare where to find the dependencies of your project
repositories {
// Use 'jcenter' for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
// In this section you declare the dependencies for your production and test code
dependencies {
// The production code uses the SLF4J logging API at compile time
compile 'org.slf4j:slf4j-api:1.7.13'
// Declare the dependency for your favourite test framework you want to use in your tests.
// TestNG is also supported by the Gradle Test task. Just change the
// testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
// 'test.useTestNG()' to your build script.
testCompile 'junit:junit:4.12'
}
*/
First thing you’ll want to do is uncomment all of that.
// Apply the java plugin to add support for Java
apply plugin: 'java'
// In this section you declare where to find the dependencies of your project
repositories {
// Use 'jcenter' for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
// In this section you declare the dependencies for your production and test code
dependencies {
// The production code uses the SLF4J logging API at compile time
compile 'org.slf4j:slf4j-api:1.7.13'
// Declare the dependency for your favourite test framework you want to use in your tests.
// TestNG is also supported by the Gradle Test task. Just change the
// testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
// 'test.useTestNG()' to your build script.
testCompile 'junit:junit:4.12'
}
Now, normally that would be enough to start declaring JME dependencies but right now JME is not on jcenter so we have to add the bintray repo:
maven { url “http://dl.bintray.com/jmonkeyengine/org.jmonkeyengine” }
Also, for the app to compile we will need to add a compile dependency on jme core.
compile “org.jmonkeyengine:jme3-core:3.1.+”
And to actually run you will need the desktop and lwjgl dependencies but they are only runtime dependencies:
runtime “org.jmonkeyengine:jme3-desktop:3.1.+”
runtime “org.jmonkeyengine:jme3-lwjgl:3.1.+”
Now, you should be able to build with the build file like this:
apply plugin: 'java'
// In this section you declare where to find the dependencies of your project
repositories {
jcenter()
maven { url "http://dl.bintray.com/jmonkeyengine/org.jmonkeyengine" }
}
// In this section you declare the dependencies for your production and test code
dependencies {
compile "org.jmonkeyengine:jme3-core:3.1.+"
runtime "org.jmonkeyengine:jme3-desktop:3.1.+"
runtime "org.jmonkeyengine:jme3-lwjgl:3.1.+"
}
Note: the + at the end of the versions is because currently what’s up is a milestone build and this will give you the latest 3.1 whatever that happens to be.
“That’s great, Paul… but how do I run it.”
Well, I’m glad you asked. If you add two lines to the gradle file you can use the app plugin.
Load the plugin with:
apply plugin: ‘application’
…and that plugin requires that you declare a main class:
mainClassName = ‘mygame.Main’
Now the build file looks like this:
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'mygame.Main'
// In this section you declare where to find the dependencies of your project
repositories {
jcenter()
maven { url "http://dl.bintray.com/jmonkeyengine/org.jmonkeyengine" }
}
// In this section you declare the dependencies for your production and test code
dependencies {
compile "org.jmonkeyengine:jme3-core:3.1.+"
runtime "org.jmonkeyengine:jme3-desktop:3.1.+"
runtime "org.jmonkeyengine:jme3-lwjgl:3.1.+"
}
And you can simply use ‘gradle run’ to build and run your app.