Gradle如何添加VM选项以在javafx 11上运行和部署jfoenix的想法?

时间:2019-09-27 21:36:08

标签: java gradle intellij-idea java-11 javafx-11

我正在使用javafx 11.0.2和java 11.0.3 为了发现“ gradle”,我下载了该项目https://github.com/fthdgn/java11-javafx-demo,并在main.fxml中添加了这样的jfoenix控件

Private Sub UserForm_Initialize()
   Dim wb As Workbook
   Dim lastSheet As Worksheet
   Dim sh As Worksheet

     Set wb = ActiveWorkbook
     For Each sh In wb.Worksheets
     ListBox1.AddItem sh.Name
     Set lastSheet = sh
     Next

    With New UserForm1'<~ forms are objects too; avoid using their global state
      .Show
    End With
 End Sub

我遇到了这个著名的错误

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.*?>
<?import com.jfoenix.controls.JFXTextField?>
<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXTabPane?>
<?import javafx.scene.control.Tab?>
<AnchorPane xmlns="http://javafx.com/javafx/8.0.121"
            xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.fthdgn.app.controller.MainController">
    <VBox alignment="CENTER" spacing="10.0" AnchorPane.bottomAnchor="10"
          AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="10" fx:id="vbox">
        <TextField fx:id="inputTextField"/>
        <Button mnemonicParsing="false" onAction="#reveseButtonClicked" text="Reverse"/>
        <TextField fx:id="outputTextField"/>
        <JFXTextField fx:id="tf"/>
        <JFXButton text="AZUL" fx:id="button"/>
        <JFXTabPane>
            <Tab>
            </Tab>
        </JFXTabPane>

    </VBox>
</AnchorPane>

我编辑了build.gradle以添加一些jvm选项(用于模块app),

Caused by: java.lang.IllegalAccessError: class com.jfoenix.skins.JFXTabPaneSkin (in module com.jfoenix) cannot access class com.sun.javafx.scene.control.behavior.TabPaneBehavior (in module javafx.controls) because module javafx.controls does not export com.sun.javafx.scene.control.behavior to module com.jfoenix

我刚刚添加了

plugins {
    id 'java'
    id 'application'
    id 'com.zyxist.chainsaw' version '0.3.1'
    id 'org.beryx.jlink' version '2.15.1'
}
compileJava.options.encoding = 'UTF-8'

import org.gradle.internal.jvm.Jvm
import org.gradle.internal.os.OperatingSystem

group 'com.fthdgn'
version '1.0-SNAPSHOT'

sourceCompatibility = 11
targetCompatibility = 11

repositories {
    mavenCentral()
}

mainClassName = mainClass

def currentOS = OperatingSystem.current()
def platform
if (currentOS.isWindows()) {
    platform = 'win'
} else if (currentOS.isLinux()) {
    platform = 'linux'
} else if (currentOS.isMacOsX()) {
    platform = 'mac'
}

jlink {
    javaHome.set(Jvm.current().getJavaHome().getAbsolutePath())

}

dependencies {
    implementation "org.openjfx:javafx-base:11:${platform}"
    implementation "org.openjfx:javafx-graphics:11:${platform}"
    implementation "org.openjfx:javafx-controls:11:${platform}"
    implementation "org.openjfx:javafx-fxml:11:${platform}"
    compile group: 'com.jfoenix', name: 'jfoenix', version: '9.0.9'


}

run {
    jvmArgs += ['--add-exports', 'javafx.controls/com.sun.javafx.scene.control.behavior=com.jfoenix']
    jvmArgs += ['--add-exports', 'javafx.controls/com.sun.javafx.scene.control=com.jfoenix']
    jvmArgs += ['--add-exports', 'javafx.base/com.sun.javafx.binding=com.jfoenix']
    jvmArgs += ['--add-exports', 'javafx.graphics/com.sun.javafx.stage=com.jfoenix']
    jvmArgs += ['--add-exports', 'javafx.base/com.sun.javafx.event=com.jfoenix']

}


运行得像个符咒,但这对部署没有任何作用,我该如何为所有任务设置jvmArgs以便在javafx 11和java 11的gradle中使用jfoenix?

run {
    jvmArgs += ['--add-exports', 'javafx.controls/com.sun.javafx.scene.control.behavior=com.jfoenix']
    jvmArgs += ['--add-exports', 'javafx.controls/com.sun.javafx.scene.control=com.jfoenix']
    jvmArgs += ['--add-exports', 'javafx.base/com.sun.javafx.binding=com.jfoenix']
    jvmArgs += ['--add-exports', 'javafx.graphics/com.sun.javafx.stage=com.jfoenix']
    jvmArgs += ['--add-exports', 'javafx.base/com.sun.javafx.event=com.jfoenix']

}


1 个答案:

答案 0 :(得分:1)

对于JavaFX 11+,您可以遵循documentation,在“带Gradle的模块化”部分。

有一个sample利用JavaFX gradle插件来处理JavaFX依赖关系以及构建和运行任务,还有一个Jlink插件来处理自定义映像的部署运行时。

您要做的就是将Jfoenix依赖项添加到该项目,相应地修改其源代码,并最终在jvmArgsrun任务中都包含所需的jlink,如下所示:

plugins {
  id 'application'
  id 'org.openjfx.javafxplugin' version '0.0.8'
  id 'org.beryx.jlink' version '2.15.1'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation group: 'com.jfoenix', name: 'jfoenix', version: '9.0.9'
}

javafx {
    version = "13"
    modules = [ 'javafx.controls', 'javafx.fxml' ]
}

run {
    jvmArgs = [
            "--add-exports=javafx.controls/com.sun.javafx.scene.control.behavior=com.jfoenix",
            "--add-exports=javafx.controls/com.sun.javafx.scene.control=com.jfoenix",
            "--add-exports=javafx.base/com.sun.javafx.binding=com.jfoenix",
            "--add-exports=javafx.graphics/com.sun.javafx.stage=com.jfoenix",
            "--add-exports=javafx.controls/com.sun.javafx.scene.control.behavior=com.jfoenix",
    ]
}

jlink {
    launcher {
        name = 'hellofx'
        jvmArgs = [
                "--add-exports=javafx.controls/com.sun.javafx.scene.control.behavior=com.jfoenix",
                "--add-exports=javafx.controls/com.sun.javafx.scene.control=com.jfoenix",
                "--add-exports=javafx.base/com.sun.javafx.binding=com.jfoenix",
                "--add-exports=javafx.graphics/com.sun.javafx.stage=com.jfoenix",
                "--add-exports=javafx.controls/com.sun.javafx.scene.control.behavior=com.jfoenix",
        ]
    }
}

mainClassName = "$moduleName/org.openjfx.MainApp"

现在,您可以使用以下命令运行项目:

./gradlew run

并使用以下命令创建运行时映像:

./gradlew jlink

完成构建后,您可以从脚本中运行它:

build/image/bin/hellofx