通过Gradle(7.5.1)构建一个Kotlin JavaFX应用程序,并try 使用jlink/jPackage进行打包/安装.该应用程序有一个module-info.java:

module com.foo.bar {
    requires javafx.controls;
    requires javafx.fxml;
    requires de.jensd.fx.glyphs.fontawesome;
    requires kotlin.stdlib;
    requires com.github.ajalt.clikt;
    requires com.google.gson;
    requires java.sql;

    opens com.foo.bar to javafx.fxml, com.google.gson;
    exports com.foo.bar;
}

gradle jlink运行时没有任何问题,但在执行脚本时,发生以下错误:

Error occurred during initialization of boot layer
java.lang.LayerInstantiationException: Package kotlin.* in both module com.foo.merged.module and module kotlin.stdlib

其中*是某个库(它每次都会改变).build.gradle条如下:

plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.6.20'
    id 'org.openjfx.javafxplugin' version '0.0.13'
    id 'org.beryx.jlink' version '2.25.0'
    id 'application'
}

group 'com.foo'
version '1.0'

repositories {
    mavenCentral()
}

dependencies {
    implementation platform('org.jetbrains.kotlin:kotlin-bom')
    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'

    implementation 'com.github.ajalt.clikt:clikt:3.5.0'
    implementation 'com.google.code.gson:gson:2.10'
    implementation 'de.jensd:fontawesomefx-fontawesome:4.7.0-9.1.2'
    implementation 'org.postgresql:postgresql:42.5.0'
    implementation 'org.xerial:sqlite-jdbc:3.39.3.0'

    testImplementation 'org.jetbrains.kotlin:kotlin-test'
    testImplementation 'org.jetbrains.kotlin:kotlin-test-junit'
}

application {
    mainModule = 'com.foo.bar'
    mainClass = 'com.foo.bar.MainKt'
}

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

jlink {
    imageZip = project.file("${buildDir}/distributions/bar-${javafx.platform.classifier}.zip")
    options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
    launcher {
        name = 'bar'
    }

    jpackage {
        outputDir = 'packages'
        installerType = 'dmg'
    }
}

在查看建议的合并模块(gradle suggestMergedModuleInfo)时,没有提到任何kotlin个库

> Task :suggestMergedModuleInfo
mergedModule {
    requires 'java.management';
    requires 'java.naming';
    requires 'java.logging';
    requires 'java.security.sasl';
    requires 'java.sql';
    requires 'java.xml';
    requires 'java.desktop';
    requires 'java.transaction.xa';
    requires 'java.security.jgss';
    provides 'java.sql.Driver' with 'org.postgresql.Driver',
                                'org.sqlite.JDBC';
}

Edit:,因此实际合并的模块似乎没有任何对任何Kotlin库的引用:

open module com.foo.merged.module {
    exports com.github.ajalt.clikt.completion;
    exports com.github.ajalt.clikt.core;
    exports com.github.ajalt.clikt.internal;
    exports com.github.ajalt.clikt.mpp;
    exports com.github.ajalt.clikt.output;
    exports com.github.ajalt.clikt.parameters.arguments;
    exports com.github.ajalt.clikt.parameters.groups;
    exports com.github.ajalt.clikt.parameters.internal;
    exports com.github.ajalt.clikt.parameters.options;
    exports com.github.ajalt.clikt.parameters.types;
    exports com.github.ajalt.clikt.parsers;
    exports com.github.ajalt.clikt.sources;
    exports org.checkerframework.checker.compilermsgs.qual;
    exports org.checkerframework.checker.fenum.qual;
    exports org.checkerframework.checker.formatter;
    exports org.checkerframework.checker.formatter.qual;
    exports org.checkerframework.checker.guieffect.qual;
    exports org.checkerframework.checker.i18n.qual;
    exports org.checkerframework.checker.i18nformatter;
    exports org.checkerframework.checker.i18nformatter.qual;
    exports org.checkerframework.checker.index.qual;
    exports org.checkerframework.checker.initialization.qual;
    exports org.checkerframework.checker.interning.qual;
    exports org.checkerframework.checker.lock.qual;
    exports org.checkerframework.checker.nullness;
    exports org.checkerframework.checker.nullness.qual;
    exports org.checkerframework.checker.optional.qual;
    exports org.checkerframework.checker.propkey.qual;
    exports org.checkerframework.checker.regex;
    exports org.checkerframework.checker.regex.qual;
    exports org.checkerframework.checker.signature.qual;
    exports org.checkerframework.checker.signedness;
    exports org.checkerframework.checker.signedness.qual;
    exports org.checkerframework.checker.tainting.qual;
    exports org.checkerframework.checker.units;
    exports org.checkerframework.checker.units.qual;
    exports org.checkerframework.common.aliasing.qual;
    exports org.checkerframework.common.reflection.qual;
    exports org.checkerframework.common.returnsreceiver.qual;
    exports org.checkerframework.common.subtyping.qual;
    exports org.checkerframework.common.util.report.qual;
    exports org.checkerframework.common.value.qual;
    exports org.checkerframework.dataflow.qual;
    exports org.checkerframework.framework.qual;
    exports org.checkerframework.framework.util;
    exports org.intellij.lang.annotations;
    exports org.jetbrains.annotations;
    exports org.postgresql;
    exports org.postgresql.copy;
    exports org.postgresql.core;
    exports org.postgresql.core.v3;
    exports org.postgresql.core.v3.adaptivefetch;
    exports org.postgresql.core.v3.replication;
    exports org.postgresql.ds;
    exports org.postgresql.ds.common;
    exports org.postgresql.fastpath;
    exports org.postgresql.geometric;
    exports org.postgresql.gss;
    exports org.postgresql.hostchooser;
    exports org.postgresql.jdbc;
    exports org.postgresql.jdbc2;
    exports org.postgresql.jdbc2.optional;
    exports org.postgresql.jdbc3;
    exports org.postgresql.jre7.sasl;
    exports org.postgresql.largeobject;
    exports org.postgresql.osgi;
    exports org.postgresql.plugin;
    exports org.postgresql.replication;
    exports org.postgresql.replication.fluent;
    exports org.postgresql.replication.fluent.logical;
    exports org.postgresql.replication.fluent.physical;
    exports org.postgresql.shaded.com.ongres.saslprep;
    exports org.postgresql.shaded.com.ongres.scram.client;
    exports org.postgresql.shaded.com.ongres.scram.common;
    exports org.postgresql.shaded.com.ongres.scram.common.bouncycastle.base64;
    exports org.postgresql.shaded.com.ongres.scram.common.bouncycastle.pbkdf2;
    exports org.postgresql.shaded.com.ongres.scram.common.exception;
    exports org.postgresql.shaded.com.ongres.scram.common.gssapi;
    exports org.postgresql.shaded.com.ongres.scram.common.message;
    exports org.postgresql.shaded.com.ongres.scram.common.stringprep;
    exports org.postgresql.shaded.com.ongres.scram.common.util;
    exports org.postgresql.shaded.com.ongres.stringprep;
    exports org.postgresql.ssl;
    exports org.postgresql.ssl.jdbc4;
    exports org.postgresql.sspi;
    exports org.postgresql.translation;
    exports org.postgresql.util;
    exports org.postgresql.util.internal;
    exports org.postgresql.xa;
    exports org.postgresql.xml;
    exports org.sqlite;
    exports org.sqlite.core;
    exports org.sqlite.date;
    exports org.sqlite.javax;
    exports org.sqlite.jdbc3;
    exports org.sqlite.jdbc4;
    exports org.sqlite.util;
    requires java.management;
    requires java.naming;
    requires java.logging;
    requires java.security.sasl;
    requires java.sql;
    requires java.xml;
    requires java.desktop;
    requires java.transaction.xa;
    requires java.security.jgss;
    provides java.sql.Driver with org.postgresql.Driver, org.sqlite.JDBC;
}

推荐答案

不确定为什么需要这样做,但在jlink块中指定forceMerge 'kotlin'可以解决这个问题.见https://badass-jlink-plugin.beryx.org/releases/latest/#_methods

Kotlin相关问答推荐

如何在 Big Data 中使用Inc过滤器?

Microronaut Data 4和JDbi

Scala与Kotlin中的迭代

Spring Boot kotlin协程不能并行运行

如何在不基于数据 map 的情况下将图例添加到lets plot kotlin

如何在 Kotlin 中为类方法调用传递变量

从字符串列表构建字符串

Android 导航组件 - 向上导航打开相同的片段

interface扩展

将 Gradle 子元素与 Kotlin 多平台一起使用

Kotlin 中的数据类

在 Koin 中提供一个 Instance 作为其接口

Kotlin 具体化的泛型不会按计划保持类型

Kotlin协程处理错误和实现

android Room 将 Null 作为非 Null 类型返回

内联 Kotlin 方法没有覆盖报告

Kotlin Flow 收集后无法执行代码

Kotlin - 如何获取注释属性值

Kotlin反射不可用

可见性不适用于 Kotlin