Skip to content

Commit

Permalink
build.gradle: add javafx runtime dependency for all platforms
Browse files Browse the repository at this point in the history
We have added the platform-specific javafx runtime dependencies, so the
addressbook is able to run locally.

However, the jar file generated on one OS cannot run on other OS. The
reason is that, after detecting the OS, javafx will try to load
platform-specific classes dynamically. Without corresponding javafx
dependencies for that other OS, the class loading process will fail.

Let's add the javafx runtime dependency for all platforms (MacOS,
Window, Linux) so the jar file generated on one OS is able to run in
other OS [1].

Following are some key facts that guarantee the solution to work:

  * Gradle places all dependency JARs on the classpath and the order is
    not defined by Gradle.

  * When shadowJar encounters duplicate classes with same package name
    and same class name, it picks the first one it sees. ShadowJar
    processes dependency JARs according to their order in classpath.

  * The platform-specific JARs for different OSs can define the same
    classes. (e.g. javafx-graphics-11-win.jar and
    javafx-graphics-11-linux.jar both contain
    javafx/scene/control/TreeView.class). However, via diffing the
    contents of the JARs, we have verified that if two JARs define the
    same class, the class files are also identical to each other. So, it
    doesn't matter if the TreeView.class from javafx-graphics-11-win.jar
    or the TreeView.class from javafx-graphics-11-linux.jar is loaded.
    As such, no matter what order Gradle places JavaFX's JARs on the
    classpath, we can run and distribute addressbook properly.

[1] https://stackoverflow.com/questions/52653836/maven-shade-javafx-runtime-components-are-missing/52654791#52654791
  • Loading branch information
fzdy1914 committed Apr 16, 2019
1 parent adc1935 commit 06a73fe
Showing 1 changed file with 18 additions and 24 deletions.
42 changes: 18 additions & 24 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,8 @@
// For more details take a look at the Java Quickstart chapter in the Gradle
// user guide available at http://gradle.org/docs/5.2.1/userguide/tutorial_java_projects.html

import org.apache.commons.lang3.SystemUtils
import org.gradle.api.tasks.testing.logging.TestLogEvent

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath group: 'org.apache.commons', name: 'commons-lang3', version: '3.8.1'
}
}

plugins {
id 'java'
id 'jacoco'
Expand Down Expand Up @@ -51,25 +41,29 @@ test {
useJUnitPlatform()
}

String platform = SystemUtils.IS_OS_WINDOWS ? 'win'
: SystemUtils.IS_OS_LINUX ? 'linux'
: SystemUtils.IS_OS_MAC ? 'mac'
: null
if (platform == null) {
throw new RuntimeException('The current OS is not supported.')
}

dependencies {
String testFxVersion = '4.0.15-alpha'
String jUnitVersion = '5.1.0'
String javaFxVersion = '11'

implementation group: 'org.openjfx', name: 'javafx-base', version: javaFxVersion, classifier: platform
implementation group: 'org.openjfx', name: 'javafx-controls', version: javaFxVersion, classifier: platform
implementation group: 'org.openjfx', name: 'javafx-fxml', version: javaFxVersion, classifier: platform
implementation group: 'org.openjfx', name: 'javafx-graphics', version: javaFxVersion, classifier: platform
implementation group: 'org.openjfx', name: 'javafx-media', version: javaFxVersion, classifier: platform
implementation group: 'org.openjfx', name: 'javafx-web', version: javaFxVersion, classifier: platform
implementation group: 'org.openjfx', name: 'javafx-base', version: javaFxVersion, classifier: 'win'
implementation group: 'org.openjfx', name: 'javafx-base', version: javaFxVersion, classifier: 'mac'
implementation group: 'org.openjfx', name: 'javafx-base', version: javaFxVersion, classifier: 'linux'
implementation group: 'org.openjfx', name: 'javafx-controls', version: javaFxVersion, classifier: 'win'
implementation group: 'org.openjfx', name: 'javafx-controls', version: javaFxVersion, classifier: 'mac'
implementation group: 'org.openjfx', name: 'javafx-controls', version: javaFxVersion, classifier: 'linux'
implementation group: 'org.openjfx', name: 'javafx-fxml', version: javaFxVersion, classifier: 'win'
implementation group: 'org.openjfx', name: 'javafx-fxml', version: javaFxVersion, classifier: 'mac'
implementation group: 'org.openjfx', name: 'javafx-fxml', version: javaFxVersion, classifier: 'linux'
implementation group: 'org.openjfx', name: 'javafx-graphics', version: javaFxVersion, classifier: 'win'
implementation group: 'org.openjfx', name: 'javafx-graphics', version: javaFxVersion, classifier: 'mac'
implementation group: 'org.openjfx', name: 'javafx-graphics', version: javaFxVersion, classifier: 'linux'
implementation group: 'org.openjfx', name: 'javafx-media', version: javaFxVersion, classifier: 'win'
implementation group: 'org.openjfx', name: 'javafx-media', version: javaFxVersion, classifier: 'mac'
implementation group: 'org.openjfx', name: 'javafx-media', version: javaFxVersion, classifier: 'linux'
implementation group: 'org.openjfx', name: 'javafx-web', version: javaFxVersion, classifier: 'win'
implementation group: 'org.openjfx', name: 'javafx-web', version: javaFxVersion, classifier: 'mac'
implementation group: 'org.openjfx', name: 'javafx-web', version: javaFxVersion, classifier: 'linux'

implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.7.0'
implementation group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: '2.7.4'
Expand Down

0 comments on commit 06a73fe

Please sign in to comment.