使用 Gradle 构建 JavaFX 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31392551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:01:35  来源:igfitidea点击:

Building a JavaFX application using Gradle

javagradlejavafx

提问by Master_T

I'm trying to build a relatively simple JavaFX application using Gradle. However, I have no idea how to do it.

我正在尝试使用 Gradle 构建一个相对简单的 JavaFX 应用程序。但是,我不知道该怎么做。

I'm relatively new to Gradle, and for simple (non-javafx) project I've successfully used the plugins javaand applicationto build and package libraries and command-line applications.

我对 Gradle 比较陌生,对于简单的(非 javafx)项目,我已经成功地使用了插件javaapplication构建和打包了库和命令行应用程序。

However, I'm completely stuck when it comes to JavaFX. I've read this article, which suggests using the JavaFXplugin, however I could only find the source code for this plugin, but no documentation on how to actually obtain it an use it (in the article they simply apply it from a remote URL, but that fails on my machine, says I'm not authorized...)

但是,当涉及到 JavaFX 时,我完全被困住了。我读过这篇文章,它建议使用该JavaFX插件,但是我只能找到这个插件的源代码,但没有关于如何实际获取和使用它的文档(在文章中,他们只是从远程 URL 应用它,但是在我的机器上失败了,说我没有被授权......)

Any clues on how to do this properly?

有关如何正确执行此操作的任何线索?

采纳答案by Peter Ledbrook

Here's an example Gradle JavaFX buildon 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.

这是在 GitHub 上构建的 Gradle JavaFX 示例。请注意,根据 Bintray,插件的最新版本是 8.1.1,因此如果您想要最新版本,请将插件 URL 中的 '0.3.0' 替换为 '8.1.1'。

Just to be clear, this is a very unusual way to distribute a Gradle plugin. Most are much easier to incorporate in a build!

需要明确的是,这是分发 Gradle 插件的一种非常不寻常的方式。大多数都更容易合并到构建中!

Edit: up-to-date fork that works using "apply plugin" https://github.com/FibreFoX/javafx-gradle-plugin

编辑:使用“应用插件” https://github.com/FibreFoX/javafx-gradle-plugin的最新分支

回答by Master_T

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:

Peter Ledbrook 的解决方案有效,但以防万一有人想要一个不依赖外部 bintray 链接的解决方案,我在等待答案时找到了一个:

1) Built the JavaFXplugin from source
2) Put it in the project's libsdirectory
3) Applied it like this:

1)JavaFX从源代码构建插件
2) 将其放在项目libs目录中
3) 像这样应用它:

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)

请注意,由于插件本身存在错误,因此需要在类路径中包含 ant-javafx(如果我理解正确的话)

回答by Peter Lamberg

Here is my example project with OpenJDK 12, JavaFX 12 and Gradle 5.4. It uses the JavaFX Gradle plugin.

这是我使用 OpenJDK 12、JavaFX 12 和 Gradle 5.4 的示例项目。它使用 JavaFX Gradle 插件。

  • 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
  • 打开一个标题为“Hello World!”的 JavaFX 窗口
  • 能够构建一个有效的可运行分发 zip 文件(Windows 待测试)
  • 无需额外配置即可在 IntelliJ 中打开和运行
  • 能够从命令行运行

I hope somebody finds the Github project useful. Feel free to clone it. It is licensed with the Unlicense.

我希望有人发现 Github 项目很有用。随意克隆它。它获得了 Unlicense 许可。

回答by Thufir

I would use the gradle initcommand as:

我将使用 gradleinit命令作为:

thufir@dur:~/NetBeansProjects$ 
thufir@dur:~/NetBeansProjects$ git clone [email protected]:THUFIR/javaFX.git
Cloning into 'javaFX'...
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (4/4), 12.48 KiB | 2.08 MiB/s, done.
thufir@dur:~/NetBeansProjects$ 
thufir@dur:~/NetBeansProjects$ cd javaFX/
thufir@dur:~/NetBeansProjects/javaFX$ 
thufir@dur:~/NetBeansProjects/javaFX$ tree
.
├── LICENSE
└── README.md

0 directories, 2 files
thufir@dur:~/NetBeansProjects/javaFX$ 
thufir@dur:~/NetBeansProjects/javaFX$ gradle init --type java-application --test-framework testng --dsl kotlin

Project name (default: javaFX): 

Source package (default: javaFX): net.bounceme.dur.javafx

BUILD SUCCESSFUL in 17s
2 actionable tasks: 2 executed
thufir@dur:~/NetBeansProjects/javaFX$ 
thufir@dur:~/NetBeansProjects/javaFX$ tree
.
├── build.gradle.kts
├── gradle
│?? └── wrapper
│??     ├── gradle-wrapper.jar
│??     └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── LICENSE
├── README.md
├── settings.gradle.kts
└── src
    ├── main
    │?? ├── java
    │?? │?? └── net
    │?? │??     └── bounceme
    │?? │??         └── dur
    │?? │??             └── javafx
    │?? │??                 └── App.java
    │?? └── resources
    └── test
        ├── java
        │?? └── net
        │??     └── bounceme
        │??         └── dur
        │??             └── javafx
        │??                 └── AppTest.java
        └── resources

17 directories, 10 files
thufir@dur:~/NetBeansProjects/javaFX$ 

here I've first cloned an empty repository (except for the license and readme) from github first, then initialized with gradle. Note that it's of type java-applicationand is using kotlinfor the dsl; those can be omittied or changed to, for example, --type java-library.

在这里,我首先从 github 克隆了一个空存储库(许可证和自述文件除外),然后使用 gradle 进行初始化。请注意,它是类型java-applicationkotlin用于dsl; 这些可以省略或更改为,例如,--type java-library

回答by Farrukh Najmi

The official gradle plugin for javafx is at https://github.com/openjfx/javafx-gradle-plugin.

javafx 的官方 gradle 插件位于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:

我已经成功地使用了来自 INtelliJ IDEA CE 的这个插件,并用以下内容来说明我的 build.gradle 文件:

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 ...
}