如何使用 gradle 和 jlink 构建 Java 应用程序?

How to build a Java app with gradle and jlink?

抱歉,如果这个问题看起来很迷茫,但我确实很迷茫。我创建了一个简单的 Java 18 Swing 应用程序,它可以编译成一个 jar 以供分发。但是,我了解到这不再是首选的分发方式,因此我将 gradle 添加到我的项目中并尝试使用 jlink 进行构建。但是每当我执行 jlink 任务时,我都会收到一个对我来说毫无意义的错误。有人可以告诉我我做错了什么吗?

build.gradle:

plugins {
    id 'application'
    id 'java'
    id 'idea'
    id "org.beryx.jlink" version "2.25.0"
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'com.google.guava:guava:30.1.1-jre'
    implementation 'org.jopendocument:jOpenDocument:1.3'
    implementation 'org.odftoolkit:odfdom-java:0.8.7'
}

application {
    mainClass = 'miq.converter.App'
}

task wrapper(type: Wrapper){
    gradleVersion = '7.2'
}

jar {
    manifest {
        attributes(
                'Main-Class': 'miq.converter.App'
        )
    }
}
jlink {
    launcher {
        name = 'MIQ-Converter'
    }
}

文件夹结构:

│   .gitattributes
│   .gitignore
│   gradlew
│   gradlew.bat
│   settings.gradle
│
├───.gradle
│
├───app
│   │   build.gradle
│   │   gradlew
│   │   gradlew.bat
│   │
│   ├───gradle
│   │   └───wrapper
│   │           gradle-wrapper.jar
│   │           gradle-wrapper.properties
│   │
│   └───src
│       └───main
│           ├───java
│           │   └───miq
│           │       └───converter
│           │               App.java
│           │
│           └───resources
└───gradle
    └───wrapper
            gradle-wrapper.jar
            gradle-wrapper.properties

模块-info.java

open module miq.converter.merged.module {
    exports com.google.common.annotations;
    exports com.google.common.base;
    exports com.google.common.base.internal;
    exports com.google.common.cache;
    exports com.google.common.collect;
    exports com.google.common.escape;
    exports com.google.common.eventbus;
    exports com.google.common.graph;
    exports com.google.common.hash;
    exports com.google.common.html;
    exports com.google.common.io;
    exports com.google.common.math;
    exports com.google.common.net;
    exports com.google.common.primitives;
    exports com.google.common.reflect;
    exports com.google.common.util.concurrent;
    exports com.google.common.util.concurrent.internal;
    exports com.google.common.xml;
    exports com.google.errorprone.annotations;
    exports com.google.errorprone.annotations.concurrent;
    exports com.google.j2objc.annotations;
    exports com.google.thirdparty.publicsuffix;
    exports com.sun.script.ognl;
    exports edu.umd.cs.findbugs;
    exports edu.umd.cs.findbugs.annotations;
    exports javax.annotation;
    exports javax.annotation.concurrent;
    exports javax.annotation.meta;
    exports javax.xml;
    exports javax.xml.datatype;
    exports javax.xml.namespace;
    exports javax.xml.parsers;
    exports javax.xml.transform;
    exports javax.xml.transform.dom;
    exports javax.xml.transform.sax;
    exports javax.xml.transform.stream;
    exports javax.xml.validation;
    exports javax.xml.xpath;
    exports net.jcip.annotations;
    exports ognl;
    exports org.apache.commons.collections;
    exports org.apache.commons.collections.bag;
    exports org.apache.commons.collections.bidimap;
    exports org.apache.commons.collections.buffer;
    exports org.apache.commons.collections.collection;
    exports org.apache.commons.collections.comparators;
    exports org.apache.commons.collections.functors;
    exports org.apache.commons.collections.iterators;
    exports org.apache.commons.collections.keyvalue;
    exports org.apache.commons.collections.list;
    exports org.apache.commons.collections.map;
    exports org.apache.commons.collections.set;
    exports org.apache.html.dom;
    exports org.apache.wml;
    exports org.apache.wml.dom;
    exports org.apache.xerces.dom;
    exports org.apache.xerces.dom.events;
    exports org.apache.xerces.dom3.as;
    exports org.apache.xerces.impl;
    exports org.apache.xerces.impl.dtd;
    exports org.apache.xerces.impl.dtd.models;
    exports org.apache.xerces.impl.dv;
    exports org.apache.xerces.impl.dv.dtd;
    exports org.apache.xerces.impl.dv.util;
    exports org.apache.xerces.impl.dv.xs;
    exports org.apache.xerces.impl.io;
    exports org.apache.xerces.impl.msg;
    exports org.apache.xerces.impl.validation;
    exports org.apache.xerces.impl.xpath;
    exports org.apache.xerces.impl.xpath.regex;
    exports org.apache.xerces.impl.xs;
    exports org.apache.xerces.impl.xs.identity;
    exports org.apache.xerces.impl.xs.models;
    exports org.apache.xerces.impl.xs.opti;
    exports org.apache.xerces.impl.xs.traversers;
    exports org.apache.xerces.impl.xs.util;
    exports org.apache.xerces.jaxp;
    exports org.apache.xerces.jaxp.datatype;
    exports org.apache.xerces.jaxp.validation;
    exports org.apache.xerces.parsers;
    exports org.apache.xerces.util;
    exports org.apache.xerces.xinclude;
    exports org.apache.xerces.xni;
    exports org.apache.xerces.xni.grammars;
    exports org.apache.xerces.xni.parser;
    exports org.apache.xerces.xpointer;
    exports org.apache.xerces.xs;
    exports org.apache.xerces.xs.datatypes;
    exports org.apache.xml.serialize;
    exports org.apache.xmlcommons;
    exports org.checkerframework.checker.builder.qual;
    exports org.checkerframework.checker.calledmethods.qual;
    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.initializedfields.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.iso_relax.verifier.jaxp.validation;
    exports org.jaxen;
    exports org.jaxen.dom;
    exports org.jaxen.dom4j;
    exports org.jaxen.expr;
    exports org.jaxen.expr.iter;
    exports org.jaxen.function;
    exports org.jaxen.function.ext;
    exports org.jaxen.function.xslt;
    exports org.jaxen.javabean;
    exports org.jaxen.jdom;
    exports org.jaxen.pattern;
    exports org.jaxen.saxpath;
    exports org.jaxen.saxpath.base;
    exports org.jaxen.saxpath.helpers;
    exports org.jaxen.util;
    exports org.jaxen.xom;
    exports org.jdom;
    exports org.jdom.adapters;
    exports org.jdom.filter;
    exports org.jdom.input;
    exports org.jdom.output;
    exports org.jdom.transform;
    exports org.jdom.xpath;
    exports org.jopendocument.dom;
    exports org.jopendocument.dom.spreadsheet;
    exports org.jopendocument.dom.style;
    exports org.jopendocument.dom.style.data;
    exports org.jopendocument.dom.template;
    exports org.jopendocument.dom.template.engine;
    exports org.jopendocument.dom.template.statements;
    exports org.jopendocument.dom.text;
    exports org.jopendocument.io;
    exports org.jopendocument.link;
    exports org.jopendocument.model;
    exports org.jopendocument.model.chart;
    exports org.jopendocument.model.draw;
    exports org.jopendocument.model.form;
    exports org.jopendocument.model.number;
    exports org.jopendocument.model.office;
    exports org.jopendocument.model.presentation;
    exports org.jopendocument.model.script;
    exports org.jopendocument.model.style;
    exports org.jopendocument.model.table;
    exports org.jopendocument.model.text;
    exports org.jopendocument.panel;
    exports org.jopendocument.print;
    exports org.jopendocument.renderer;
    exports org.jopendocument.renderer.text;
    exports org.jopendocument.sample;
    exports org.jopendocument.tools;
    exports org.jopendocument.ui;
    exports org.jopendocument.util;
    exports org.jopendocument.util.cache;
    exports org.jopendocument.util.cc;
    exports org.jopendocument.util.convertor;
    exports org.jopendocument.util.i18n;
    exports org.jopendocument.util.io;
    exports org.jopendocument.util.protocol;
    exports org.jopendocument.util.protocol.jarjar;
    exports org.mozilla.classfile;
    exports org.mozilla.javascript;
    exports org.mozilla.javascript.continuations;
    exports org.mozilla.javascript.debug;
    exports org.mozilla.javascript.jdk11;
    exports org.mozilla.javascript.jdk13;
    exports org.mozilla.javascript.jdk15;
    exports org.mozilla.javascript.optimizer;
    exports org.mozilla.javascript.regexp;
    exports org.mozilla.javascript.serialize;
    exports org.mozilla.javascript.tools;
    exports org.mozilla.javascript.tools.idswitch;
    exports org.mozilla.javascript.tools.jsc;
    exports org.mozilla.javascript.tools.shell;
    exports org.mozilla.javascript.xml;
    exports org.mozilla.javascript.xml.impl.xmlbeans;
    exports org.mozilla.javascript.xmlimpl;
    exports org.odftoolkit.odfdom;
    exports org.odftoolkit.odfdom.doc;
    exports org.odftoolkit.odfdom.doc.presentation;
    exports org.odftoolkit.odfdom.doc.table;
    exports org.odftoolkit.odfdom.dom;
    exports org.odftoolkit.odfdom.dom.attribute.anim;
    exports org.odftoolkit.odfdom.dom.attribute.chart;
    exports org.odftoolkit.odfdom.dom.attribute.config;
    exports org.odftoolkit.odfdom.dom.attribute.db;
    exports org.odftoolkit.odfdom.dom.attribute.dr3d;
    exports org.odftoolkit.odfdom.dom.attribute.draw;
    exports org.odftoolkit.odfdom.dom.attribute.fo;
    exports org.odftoolkit.odfdom.dom.attribute.form;
    exports org.odftoolkit.odfdom.dom.attribute.grddl;
    exports org.odftoolkit.odfdom.dom.attribute.meta;
    exports org.odftoolkit.odfdom.dom.attribute.number;
    exports org.odftoolkit.odfdom.dom.attribute.office;
    exports org.odftoolkit.odfdom.dom.attribute.presentation;
    exports org.odftoolkit.odfdom.dom.attribute.script;
    exports org.odftoolkit.odfdom.dom.attribute.smil;
    exports org.odftoolkit.odfdom.dom.attribute.style;
    exports org.odftoolkit.odfdom.dom.attribute.svg;
    exports org.odftoolkit.odfdom.dom.attribute.table;
    exports org.odftoolkit.odfdom.dom.attribute.text;
    exports org.odftoolkit.odfdom.dom.attribute.xforms;
    exports org.odftoolkit.odfdom.dom.attribute.xhtml;
    exports org.odftoolkit.odfdom.dom.attribute.xlink;
    exports org.odftoolkit.odfdom.dom.attribute.xml;
    exports org.odftoolkit.odfdom.dom.element;
    exports org.odftoolkit.odfdom.dom.element.anim;
    exports org.odftoolkit.odfdom.dom.element.chart;
    exports org.odftoolkit.odfdom.dom.element.config;
    exports org.odftoolkit.odfdom.dom.element.db;
    exports org.odftoolkit.odfdom.dom.element.dc;
    exports org.odftoolkit.odfdom.dom.element.dr3d;
    exports org.odftoolkit.odfdom.dom.element.draw;
    exports org.odftoolkit.odfdom.dom.element.form;
    exports org.odftoolkit.odfdom.dom.element.math;
    exports org.odftoolkit.odfdom.dom.element.meta;
    exports org.odftoolkit.odfdom.dom.element.number;
    exports org.odftoolkit.odfdom.dom.element.office;
    exports org.odftoolkit.odfdom.dom.element.presentation;
    exports org.odftoolkit.odfdom.dom.element.script;
    exports org.odftoolkit.odfdom.dom.element.style;
    exports org.odftoolkit.odfdom.dom.element.svg;
    exports org.odftoolkit.odfdom.dom.element.table;
    exports org.odftoolkit.odfdom.dom.element.text;
    exports org.odftoolkit.odfdom.dom.element.xforms;
    exports org.odftoolkit.odfdom.dom.style;
    exports org.odftoolkit.odfdom.dom.style.props;
    exports org.odftoolkit.odfdom.incubator.doc.draw;
    exports org.odftoolkit.odfdom.incubator.doc.number;
    exports org.odftoolkit.odfdom.incubator.doc.office;
    exports org.odftoolkit.odfdom.incubator.doc.style;
    exports org.odftoolkit.odfdom.incubator.doc.text;
    exports org.odftoolkit.odfdom.incubator.meta;
    exports org.odftoolkit.odfdom.incubator.search;
    exports org.odftoolkit.odfdom.pkg;
    exports org.odftoolkit.odfdom.pkg.manifest;
    exports org.odftoolkit.odfdom.type;
    exports org.w3c.dom;
    exports org.w3c.dom.bootstrap;
    exports org.w3c.dom.css;
    exports org.w3c.dom.events;
    exports org.w3c.dom.html;
    exports org.w3c.dom.ls;
    exports org.w3c.dom.ranges;
    exports org.w3c.dom.stylesheets;
    exports org.w3c.dom.traversal;
    exports org.w3c.dom.views;
    exports org.w3c.dom.xpath;
    exports org.xml.sax;
    exports org.xml.sax.ext;
    exports org.xml.sax.helpers;
    requires java.logging;
    requires java.scripting;
    requires java.sql;
    requires java.desktop;
    requires java.compiler;
    requires java.sql.rowset;
    requires jdk.unsupported;
    provides javax.xml.parsers.DocumentBuilderFactory with org.apache.xerces.jaxp.DocumentBuilderFactoryImpl;
    provides javax.script.ScriptEngineFactory with com.sun.script.ognl.OgnlScriptEngineFactory;
    provides javax.xml.datatype.DatatypeFactory with org.apache.xerces.jaxp.datatype.DatatypeFactoryImpl;
    provides javax.xml.validation.SchemaFactory with org.apache.xerces.jaxp.validation.XMLSchemaFactory,
                org.iso_relax.verifier.jaxp.validation.RELAXNGSchemaFactoryImpl;
    provides org.w3c.dom.DOMImplementationSourceList with org.apache.xerces.dom.DOMXSImplementationSourceImpl;
    provides org.xml.sax.driver with org.apache.xerces.parsers.SAXParser;
    provides javax.xml.parsers.SAXParserFactory with org.apache.xerces.jaxp.SAXParserFactoryImpl;
}

错误:

C:\Users\Leheess\Desktop\miq-converter\app\build\jlinkbase\tmpjars\miq.converter.merged.module\module-info.java:291: error: cannot find symbol
    provides org.w3c.dom.DOMImplementationSourceList with org.apache.xerces.dom.DOMXSImplementationSourceImpl;
                        ^
  symbol:   class DOMImplementationSourceList
  location: package org.w3c.dom
C:\Users\Leheess\Desktop\miq-converter\app\build\jlinkbase\tmpjars\miq.converter.merged.module\module-info.java:292: error: cannot find symbol
    provides org.xml.sax.driver with org.apache.xerces.parsers.SAXParser;
                        ^
  symbol:   class driver
  location: package org.xml.sax
2 errors

Execution failed for task ':app:createMergedModule'.
> Process 'command 'C:\Users\Leheess\.jdks\openjdk-18/bin/javac'' finished with non-zero exit value 1

将以下内容添加到您的 jlink 块中以克服这两个错误(参见 mergedModule docs):

    mergedModule {
        // Don't use any of the suggested module info.
    }

当我尝试使用您的 build.gradle 文件时,我还必须稍后将以下内容添加到 jlink 块中:

    jarExclude('jOpenDocument', 'JDOMAbout*')

我还添加了一个src/main/java/module-info.java文件;示例:

module com.example.miq {
    exports miq.converter;
}

最后,我用以下内容扩展了 build.gradle 文件的 application 块:

    // Use the same module name as in your src/main/java/module-info.java file.
    mainModule = 'com.example.miq'

这些更改使 jlink 任务适用于您的构建配置。