为其他操作系统构建Java模块运行时映像 [英] Build Java module runtime image for other OS

查看:55
本文介绍了为其他操作系统构建Java模块运行时映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将我的Java 8小项目从简单的jar重写为Java 11中的单个模块.过去,我使用Gradle构建jar,并且与Windows和Linux兼容.现在,我配置了Gradle以构建我的模块并创建自定义运行时映像,该映像可以运行,但仅在Linux上有效.我的自定义运行时映像仅包含Linux库.是否有可能在Linux上为Windows构建映像?我知道我可以在Windows上打开我的项目并在其中创建映像,但是我想将我的项目保留在单个OS上. 这是我的Gradle版本:

I have rewritten my little Java 8 project from simple jar to single module in Java 11. In past I was building jar with Gradle and it was compatible with Windows and Linux. Now I configured Gradle to build my module and create custom runtime image and it is working but only on Linux. My custom runtime image contains only Linux libraries. Is there possibility to build image for Windows on Linux? I know I could open my project on Windows and create there image but I would like to keep my project on single OS. Here is my Gradle build:

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

我在fire jlinkWin任务运行清洁任务之前添加到build.gradle行:

I added to build.gradle lines, before fire jlinkWin task I run clean task:

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

Updated code above which creates custom runtime image for Windows but with out JavaFX libs.

推荐答案

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

Download JDK (ex. openJDK) and openjfx jmods archive for platform you are interested (in this example Windows and Linux), extract them somewhere, next modify configuration file of 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的路径:

And add or modify gradle.properties, add paths to JDK's and 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

Finally fire gradle task jlink to build image for Linux or jlinkWin for Windows

这篇关于为其他操作系统构建Java模块运行时映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆