Sunday, December 30, 2012

How to Setup JavaFX Project with Gradle

apply plugin: "java"
apply plugin: "eclipse"

version = "0.1.0"

sourceCompatibility = 1.7
targetCompatibility = 1.7

defaultTasks = ["clean", "jar"]

def javafxLib = "jfxrt.jar"

def getJavaFXPath(def javafxLib) {
    def javaHome = System.env["JAVA_HOME"]
    if (javaHome == null) {
        throw new RuntimeException("JAVA_HOME environment variable must be set")
    }
    def javafxrt = "jre" + File.separator + "lib" + File.separator + javafxLib
    return new File(javaHome, javafxrt).absolutePath
}

dependencies {
    compile files(getJavaFXPath(javafxLib))
}

jar {
    // When creating a fat JAR, make sure we exclude javafx runtime
    // from the fat JAR
    dependsOn configurations.runtime
    from {
        configurations.runtime.findAll { !it.name.contains(javafxLib) }.collect { 
            it.isDirectory() ? it : zipTree(it)
        }
    }
    from sourceSets.main.allJava

    // There's no need to explicitly specify the -classpath or -cp
    // since the classpath information is already stored in the MANIFEST.MF
    manifest {
        attributes "Main-Class": "javafxapp.JavaFXApp"
        attributes "Class-Path": getJavaFXPath(javafxLib)
    }
}
Now we can easily execute the generated JAR by calling
java -jar <jar_file.jar>

No comments:

Post a Comment