gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 04:32:12 24 4
gpt4 key购买 nike

如果这个问题看起来很迷茫,但我很抱歉。我创建了一个简单的 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

模块信息.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 block 中以克服这两个错误(参见 mergedModule docs ):

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

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

    jarExclude('jOpenDocument', 'JDOMAbout*')

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

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

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

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

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

关于java - 如何使用 gradle 和 jlink 构建 Java 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71699840/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com