Building a JavaFX application using Gradle
The official gradle plugin for javafx is at https://github.com/openjfx/javafx-gradle-plugin.
I have successfully used this plugin from INtelliJ IDEA CE with following to illustrate my build.gradle file:
plugins {
id 'java'
id 'application'
id 'maven'
id 'maven-publish'
id 'org.openjfx.javafxplugin' version '0.0.8'
}
javafx {
// Points to JDK and its JavaFX libraries, also declares target runtime JDK
// javaRuntime = '/Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk'
version = '13' // Specify javafx sdk version
modules = [ 'javafx.controls', 'javafx.fxml', 'javafx.swing', 'javafx.media', 'javafx.graphics']
sdk = System.getenv('JAVAFX_HOME')
if (sdk == null || sdk.isBlank()) {
throw new InvalidUserDataException("JAVAFX_HOME environment variable is not set. It must be set to root folder where JAVAFX SDK is located")
}
application {
def javafxHome = System.getenv('JAVAFX_HOME')
mainClassName = 'com.foo.FooApp'
applicationName = 'foo-app'
applicationDefaultJvmArgs = [
"--module-path=${javafxHome}" + File.separator + 'lib',
'--add-modules=javafx.controls,javafx.swing,javafx.media,javafx.graphics']
println("applicationDefaultJvmArgs:" + applicationDefaultJvmArgs)
}
}
dependencies {
... ommitted ...
}
Peter Ledbrook's solution works, but just in case someone wants a solution that doesn't rely on external bintray links, I found one while waiting for an answer:
1) Built the JavaFX
plugin from source
2) Put it in the project's libs
directory
3) Applied it like this:
buildscript {
repositories {
flatDir dirs: "../libs"
}
dependencies {
classpath "org.bitbucket.shemnon.javafxplugin:gradle-javafx-plugin:0.5.0-SNAPSHOT"
classpath files("${System.properties['java.home']}/../lib/ant-javafx.jar")
}
}
if (!project.plugins.findPlugin(org.bitbucket.shemnon.javafxplugin.JavaFXPlugin)) {
project.apply(plugin: org.bitbucket.shemnon.javafxplugin.JavaFXPlugin)
}
Note that including ant-javafx in the classpath is needed due to a bug in the plugin itself (if I understand correctly)
Here is my example project with OpenJDK 12, JavaFX 12 and Gradle 5.4. It uses the JavaFX Gradle plugin.
- Opens a JavaFX window with the title "Hello World!"
- Able to build a working runnable distribution zip file (Windows to be tested)
- Able to open and run in IntelliJ without additional configuration
- Able to run from the command line
I hope somebody finds the Github project useful. Feel free to clone it. It is licensed with the Unlicense.
Here's an example Gradle JavaFX build on GitHub. Note that according to Bintray, the latest version of the plugin is 8.1.1, so replace the '0.3.0' in the plugin URL with '8.1.1' if you want the latest.
Just to be clear, this is a very unusual way to distribute a Gradle plugin. Most are much easier to incorporate in a build!
Edit: up-to-date fork that works using "apply plugin" https://github.com/FibreFoX/javafx-gradle-plugin