From 0890c97929edc5ab0cdd4dc37e39ec0acdba8612 Mon Sep 17 00:00:00 2001 From: danlangford Date: Sat, 5 Jan 2019 23:08:02 -0700 Subject: [PATCH] initial commit demonstrating fat/uber/shade jar with javafx --- .gitignore | 291 ++++++++++++++++++ README.md | 23 ++ pom.xml | 80 +++++ .../github/danlangford/dicefx/DiceApp.java | 23 ++ .../danlangford/dicefx/FatJarLauncher.java | 13 + .../danlangford/dicefx/SceneController.java | 42 +++ .../com/github/danlangford/dicefx/scene.fxml | 26 ++ 7 files changed, 498 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 pom.xml create mode 100644 src/main/java/com/github/danlangford/dicefx/DiceApp.java create mode 100644 src/main/java/com/github/danlangford/dicefx/FatJarLauncher.java create mode 100644 src/main/java/com/github/danlangford/dicefx/SceneController.java create mode 100644 src/main/resources/com/github/danlangford/dicefx/scene.fxml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..05e3e93 --- /dev/null +++ b/.gitignore @@ -0,0 +1,291 @@ + +# my own addition. i prefer 0 IDE files committed. +.idea/ + +# Created by https://www.gitignore.io/api/java,linux,macos,maven,gradle,windows,eclipse,netbeans,intellij +# Edit at https://www.gitignore.io/?templates=java,linux,macos,maven,gradle,windows,eclipse,netbeans,intellij + +### Eclipse ### + +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.settings/ +.loadpath +.recommenders + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# PyDev specific (Python IDE for Eclipse) +*.pydevproject + +# CDT-specific (C/C++ Development Tooling) +.cproject + +# CDT- autotools +.autotools + +# Java annotation processor (APT) +.factorypath + +# PDT-specific (PHP Development Tools) +.buildpath + +# sbteclipse plugin +.target + +# Tern plugin +.tern-project + +# TeXlipse plugin +.texlipse + +# STS (Spring Tool Suite) +.springBeans + +# Code Recommenders +.recommenders/ + +# Annotation Processing +.apt_generated/ + +# Scala IDE specific (Scala & Java development for Eclipse) +.cache-main +.scala_dependencies +.worksheet + +### Eclipse Patch ### +# Eclipse Core +.project + +# JDT-specific (Eclipse Java Development Tools) +.classpath + +# Annotation Processing +.apt_generated + +.sts4-cache/ + +### Intellij ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +.idea/modules.xml +.idea/*.iml +.idea/modules + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +### Intellij Patch ### +# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 + +*.iml +modules.xml +.idea/misc.xml +*.ipr + +# Sonarlint plugin +.idea/sonarlint + +### Java ### +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +### Linux ### +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +### macOS ### +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +### Maven ### +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +.mvn/wrapper/maven-wrapper.jar + +### NetBeans ### +**/nbproject/private/ +**/nbproject/Makefile-*.mk +**/nbproject/Package-*.bash +build/ +nbbuild/ +dist/ +nbdist/ +.nb-gradle/ + +### Windows ### +# Windows thumbnail cache files +Thumbs.db +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +### Gradle ### +.gradle +/build/ + +# Ignore Gradle GUI config +gradle-app.setting + +# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) +!gradle-wrapper.jar + +# Cache of project +.gradletasknamecache + +# # Work around https://youtrack.jetbrains.com/issue/IDEA-116898 +# gradle/wrapper/gradle-wrapper.properties + +### Gradle Patch ### +**/build/ + +# End of https://www.gitignore.io/api/java,linux,macos,maven,gradle,windows,eclipse,netbeans,intellij \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..0cb392a --- /dev/null +++ b/README.md @@ -0,0 +1,23 @@ +# dice-notation-javafx + +this is to demonstrate how to create a java jar that has all dependencies and can be executed on multiple platforms. + +Leverages: +- Java 11 (non-module config) +- Maven +- javafx (with multiple OS specific dependencies) +- maven-shade-plugin + +When importing into your IDE "Open" or "Import" the `pom.xml` file to create a new project. + +Most of the interesting work is in the `pom.xml` file. Also, there is a funny situation that requires the use of a seperate class with a `main` method. See the comments in `FatJarLauncher.java` + +execute maven (`mvn`) with the `package` goal then locate your jar in the `target/` dir. + + mvn package + + This little app is just a simple ui for [Bernardo-MG/dice-notation-java](https://github.com/Bernardo-MG/dice-notation-java). That lib has a handful of transitive dependencies so this demonstrates how `maven-shade-plugin` creates a jar with ALL the necessary dependencies. + + A compiled version of this is over in the [Releases](https://github.com/danlangford/dice-notation-javafx/releases). + + The only dependency that is not included in Java 11 itself. It is possible to include that but then you would not be using the shade plugin, you would be using all proper java modules and `jlink`. I'll demonstrate that another time. \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..939db8b --- /dev/null +++ b/pom.xml @@ -0,0 +1,80 @@ + + 4.0.0 + com.github.danlangford + dice-notation-javafx + 0.0.1 + + 11 + 11 + 11.0.1 + + + + + + com.bernardomg.tabletop + dice + 1.3.0 + + + + + org.openjfx + javafx-controls + ${javafx.version} + + + org.openjfx + javafx-fxml + ${javafx.version} + + + org.openjfx + javafx-graphics + ${javafx.version} + + + + + org.openjfx + javafx-graphics + ${javafx.version} + win + + + org.openjfx + javafx-graphics + ${javafx.version} + mac + + + org.openjfx + javafx-graphics + ${javafx.version} + linux + + + + + + + maven-shade-plugin + + + package + + shade + + + + + com.github.danlangford.dicefx.FatJarLauncher + + + + + + + + + diff --git a/src/main/java/com/github/danlangford/dicefx/DiceApp.java b/src/main/java/com/github/danlangford/dicefx/DiceApp.java new file mode 100644 index 0000000..5ced1da --- /dev/null +++ b/src/main/java/com/github/danlangford/dicefx/DiceApp.java @@ -0,0 +1,23 @@ +package com.github.danlangford.dicefx; + +import javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.stage.Stage; + +public class DiceApp extends Application { + + @Override + public void start(Stage stage) throws Exception { + Parent root = FXMLLoader.load(getClass().getResource("scene.fxml")); + stage.setScene(new Scene(root)); + stage.setTitle("DiceFX"); + stage.show(); + } + + public static void main(String...args){ + launch(args); + } + +} diff --git a/src/main/java/com/github/danlangford/dicefx/FatJarLauncher.java b/src/main/java/com/github/danlangford/dicefx/FatJarLauncher.java new file mode 100644 index 0000000..e91ecc9 --- /dev/null +++ b/src/main/java/com/github/danlangford/dicefx/FatJarLauncher.java @@ -0,0 +1,13 @@ +package com.github.danlangford.dicefx; + +public class FatJarLauncher { + + // Shade, Assembly, and other plugins mess with the jar structure and ... threads? + // and i dont fully understand it but we require this little shim + // https://stackoverflow.com/questions/52653836/maven-shade-javafx-runtime-components-are-missing + + public static void main(String[] args) { + DiceApp.main(args); + } + +} diff --git a/src/main/java/com/github/danlangford/dicefx/SceneController.java b/src/main/java/com/github/danlangford/dicefx/SceneController.java new file mode 100644 index 0000000..bdc2c06 --- /dev/null +++ b/src/main/java/com/github/danlangford/dicefx/SceneController.java @@ -0,0 +1,42 @@ +package com.github.danlangford.dicefx; + +import com.bernardomg.tabletop.dice.parser.DefaultDiceNotationExpressionParser; +import com.bernardomg.tabletop.dice.parser.DiceNotationExpressionParser; +import com.bernardomg.tabletop.dice.roller.DefaultRoller; +import javafx.fxml.FXML; +import javafx.scene.control.Label; +import javafx.scene.control.TextField; + +import static java.text.MessageFormat.format; + +public class SceneController { + + private final DiceNotationExpressionParser diceNotation = new DefaultDiceNotationExpressionParser(new DefaultRoller()); + + @FXML + private TextField expressionField; + + @FXML + private TextField resultsField; + + @FXML + private Label versionLabel; + + @FXML + public void parseExpression(){ + String expression = expressionField.getText(); + try { + Integer rollResults = diceNotation.parse(expression).roll(); + resultsField.setText(rollResults.toString()); + } catch (Exception e) { + resultsField.setText(e.getMessage()); + } + } + + public void initialize() { + String javaVersion = System.getProperty("java.version"); + String javafxVersion = System.getProperty("javafx.version"); + versionLabel.setText(format("JavaFX {0} Running on Java {1}.", javafxVersion, javaVersion)); + } + +} diff --git a/src/main/resources/com/github/danlangford/dicefx/scene.fxml b/src/main/resources/com/github/danlangford/dicefx/scene.fxml new file mode 100644 index 0000000..50999ff --- /dev/null +++ b/src/main/resources/com/github/danlangford/dicefx/scene.fxml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + +