为其他操作系统建立Java模块运行时镜像

 2023-02-16    400  

问题描述

我已经在Java 11中重写了我的小Java 8项目.现在,我配置了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行,在fire jlinkwin任务之前,我运行干净的任务:

为其他操作系统建立Java模块运行时镜像

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 Libs创建.

推荐答案

下载JDK(ex.OpenJDK)和OpenJFX JMODS档案,您感兴趣的平台(在此示例Windows和Linux中),将它们提取到某个地方,下一个gradle build.gradle.gradle.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 JMOD:

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/

最终Fire Gradle Task Jlink为Linux或Windows jlinkwin构建图像

以上所述是小编给大家介绍的为其他操作系统建立Java模块运行时镜像,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!

原文链接:https://77isp.com/post/33912.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。