【问题标题】:Build Java module runtime image for other OS为其他操作系统构建 Java 模块运行时映像
【发布时间】:2019-05-14 10:20:24
【问题描述】:

我已经将我的 Java 8 小项目从简单的 jar 重写为 Java 11 中的单个模块。过去我使用 Gradle 构建 jar,它与 Windows 和 Linux 兼容。现在我配置了 Gradle 来构建我的模块并创建自定义运行时映像,它可以工作,但只能在 Linux 上运行。我的自定义运行时映像仅包含 Linux 库。是否有可能在 Linux 上为 Windows 构建映像?我知道我可以在 Windows 上打开我的项目并在那里创建图像,但我想将我的项目保留在单个操作系统上。 这是我的 Gradle 构建:

plugins {
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.5'
}

group 'eu.sample'
version '2.0'


repositories {
    mavenCentral()
}

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

mainClassName = "$moduleName/eu.sample.app.Main"

def java_home = hasProperty('org.gradle.java.home') ? getProperty('org.gradle.java.home') : System.getenv('JAVA_HOME')
def fx_jmods = hasProperty('path.to.fx.mods') ? getProperty('path.to.fx.mods') : System.getenv('PATH_TO_FX_MODS')

dependencies {

}

task jlink(type: Exec) {
    dependsOn 'clean'
    dependsOn 'jar'

    workingDir 'build'

    if (java_home == null) {
        throw new RuntimeException("java_home is not defined.")
    }
    if (fx_jmods == null) {
        throw new RuntimeException("fx_jmods is not defined.")
    }
    commandLine "${java_home}/bin/jlink", '--module-path', "libs${File.pathSeparatorChar}${fx_jmods}",
            '--add-modules', "${moduleName}", '--output', "${moduleName}", '--strip-debug',
            '--compress', '2', '--no-header-files', '--no-man-pages'
}

我添加到 build.gradle 行,在触发 jlinkWin 任务之前我运行 clean 任务:

task jlinkWin(type: Exec) {
    dependsOn 'clean'
    dependsOn 'jar'

    workingDir 'build'

    if (java_home == null) {
        throw new RuntimeException("java_home is not defined.")
    }
    if (fx_jmods == null) {
        throw new RuntimeException("fx_jmods is not defined.")
    }
    commandLine "${java_home}/bin/jlink", '--module-path', "/home/user1/Download/win-jdk-11.0.1/jmods${File.pathSeparatorChar}libs${File.pathSeparatorChar}${fx_jmods}",
            '--add-modules', "${moduleName}", '--output', "${moduleName}", '--strip-debug',
            '--compress', '2', '--no-header-files', '--no-man-pages'
}

更新了上面的代码,它为 Windows 创建了自定义运行时映像,但没有 JavaFX 库。

【问题讨论】:

  • 这与 gradle 无关,但可能会有所帮助:stackoverflow.com/questions/47593409/…
  • @ernest_k 这是有用的信息,谢谢。我没有找到这个。
  • 您需要在您的发行版中包含 Windows 库和 Linux 库。
  • 问题未解决。我无法在 Windows 上运行图像。图片中没有JavaFX的dll库。

标签: java gradle javafx


【解决方案1】:

为您感兴趣的平台(在此示例中为 Windows 和 Linux)下载 JDK(例如 openJDK)和 openjfx jmods 存档,将它们解压缩到某个位置,然后修改 gradle build.gradle 的配置文件。

plugins {
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.5'
}

group 'eu.sample'
version '2.0'


repositories {
    mavenCentral()
}

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

mainClassName = "$moduleName/eu.sample.app.Main"

def lin_java_home = hasProperty('org.gradle.java.home') ? getProperty('org.gradle.java.home') : System.getenv('JAVA_HOME')
def lin_fx_jmods = hasProperty('linux.fx.mods') ? getProperty('linux.fx.mods') : System.getenv('PATH_TO_FX_MODS_LIN')

def win_java_home = hasProperty('windows.java.home') ? getProperty('windows.java.home') : System.getenv('JAVA_HOME_WIN')
def win_fx_jmods = hasProperty('windows.fx.mods') ? getProperty('windows.fx.mods') : System.getenv('PATH_TO_FX_MODS_WIN')

dependencies {

}

task jlink(type: Exec) {
    dependsOn 'clean'
    dependsOn 'jar'

    workingDir 'build'

    if (lin_java_home == null) {
        throw new RuntimeException("java_home is not defined.")
    }
    if (lin_fx_jmods == null) {
        throw new RuntimeException("fx_jmods is not defined.")
    }
    commandLine "${lin_java_home}/bin/jlink", '--module-path', "libs${File.pathSeparatorChar}${lin_fx_jmods}",
            '--add-modules', "${moduleName}", '--output', "${moduleName}", '--strip-debug',
            '--compress', '2', '--no-header-files', '--no-man-pages'
}

task jlinkWin(type: Exec) {
    dependsOn 'clean'
    dependsOn 'jar'

    workingDir 'build'

    if (win_java_home == null) {
        throw new RuntimeException("java_home is not defined.")
    }
    if (win_fx_jmods == null) {
        throw new RuntimeException("fx_jmods is not defined.")
    }
    commandLine "${lin_java_home}/bin/jlink", '--module-path', 
            "${win_java_home}/jmods${File.pathSeparatorChar}libs${File.pathSeparatorChar}${win_fx_jmods}",
            '--add-modules', "${moduleName}", '--output', "${moduleName}", '--strip-debug',
            '--compress', '2', '--no-header-files', '--no-man-pages'
}

并添加或修改 gradle.properties,添加 JDK 和 openjfx jmods 的路径:

org.gradle.java.home=/usr/java/jdk-11/
windows.java.home=/home/user1/Download/win-jdk-11.0.1/

linux.fx.mods=/usr/lib64/javafx-jmods-11.0.1
windows.fx.mods=/home/user1/Download/javafx-jmods-11.0.1/

最后触发 gradle 任务 jlink 为 Linux 构建映像或为 Windows 构建 jlinkWin

【讨论】:

    猜你喜欢
    • 2010-10-18
    • 1970-01-01
    • 1970-01-01
    • 2012-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-16
    • 1970-01-01
    相关资源
    最近更新 更多