You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Moditect为Maven依赖加module-info仍遇jlink自动模块错误

问题现象

执行mvn clean javafx:jlink时触发错误,提示自动模块无法用于jlink,涉及的依赖包括:

  • org.apache.commons.io
  • org.apache.commons.commons-lang3
  • commons-codec.commons-codec
    已尝试通过jdeps生成模块信息并使用Moditect插件注入到对应Jar中,但问题仍未解决。

排查与解决步骤

1. 确保Moditect插件覆盖所有非自动模块

手动编写moduleInfoSource比jdeps自动生成的更可靠,要保证所有涉及的非自动模块都被处理,且导出包、依赖模块声明正确。示例配置:

<plugin>
    <groupId>org.moditect</groupId>
    <artifactId>moditect-maven-plugin</artifactId>
    <version>1.0.0.Final</version>
    <executions>
        <execution>
            <id>add-module-info-to-commons</id>
            <phase>package</phase>
            <goals>
                <goal>add-module-info</goal>
            </goals>
            <configuration>
                <modules>
                    <!-- commons-io -->
                    <module>
                        <artifact>
                            <groupId>commons-io</groupId>
                            <artifactId>commons-io</artifactId>
                            <version>2.12.0</version>
                        </artifact>
                        <moduleInfoSource>
                            module org.apache.commons.io {
                                exports org.apache.commons.io;
                                exports org.apache.commons.io.file;
                                exports org.apache.commons.io.filefilter;
                                requires java.base;
                                requires java.logging;
                            }
                        </moduleInfoSource>
                    </module>
                    <!-- commons-lang3 -->
                    <module>
                        <artifact>
                            <groupId>org.apache.commons</groupId>
                            <artifactId>commons-lang3</artifactId>
                            <version>3.12.0</version>
                        </artifact>
                        <moduleInfoSource>
                            module org.apache.commons.lang3 {
                                exports org.apache.commons.lang3;
                                exports org.apache.commons.lang3.builder;
                                exports org.apache.commons.lang3.math;
                                requires java.base;
                            }
                        </moduleInfoSource>
                    </module>
                    <!-- commons-codec -->
                    <module>
                        <artifact>
                            <groupId>commons-codec</groupId>
                            <artifactId>commons-codec</artifactId>
                            <version>1.15</version>
                        </artifact>
                        <moduleInfoSource>
                            module org.apache.commons.codec {
                                exports org.apache.commons.codec;
                                exports org.apache.commons.codec.binary;
                                exports org.apache.commons.codec.digest;
                                exports org.apache.commons.codec.language;
                                exports org.apache.commons.codec.net;
                                requires java.base;
                            }
                        </moduleInfoSource>
                    </module>
                </modules>
            </configuration>
        </execution>
    </executions>
</plugin>

2. 检查javafx-maven-plugin的jlink配置

确保插件正确引用Moditect处理后的模块目录,配置示例:

<plugin>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-maven-plugin</artifactId>
    <version>0.0.8</version>
    <configuration>
        <mainClass>你的主模块名/你的主类全路径</mainClass>
        <jlinkImageName>target/image</jlinkImageName>
        <jlinkZipName>target/image-zip</jlinkZipName>
        <modulePaths>
            <modulePath>${project.build.directory}/modules</modulePath> <!-- Moditect生成的模块存放目录 -->
            <modulePath>${maven.dependency.classpath}</modulePath>
        </modulePaths>
    </configuration>
    <executions>
        <execution>
            <id>default-cli</id>
            <phase>package</phase>
            <goals>
                <goal>jlink</goal>
            </goals>
        </execution>
    </executions>
</plugin>

3. 清理本地Maven缓存

执行命令强制更新依赖,同时手动删除本地仓库中commons-io、commons-lang3、commons-codec的对应目录,避免使用旧的未模块化Jar:

mvn clean install -U

4. 修正项目自身的module-info.java

确保项目的模块声明中正确引入处理后的模块:

module 你的主模块名 {
    requires javafx.controls;
    requires javafx.fxml;
    requires org.apache.commons.io;
    requires org.apache.commons.lang3;
    requires org.apache.commons.codec;

    opens 你的主包路径 to javafx.fxml;
    exports 你的主包路径;
}

5. 显式指定jlink需要的模块

如果以上步骤无效,在javafx-maven-plugin中添加jlinkOptions,强制指定所有需要的模块:

<configuration>
    <!-- 其他已有配置 -->
    <jlinkOptions>
        <option>--add-modules</option>
        <option>你的主模块名,org.apache.commons.io,org.apache.commons.lang3,org.apache.commons.codec</option>
        <option>--strip-debug</option>
        <option>--compress=2</option>
        <option>--no-header-files</option>
        <option>--no-man-pages</option>
    </jlinkOptions>
</configuration>

内容的提问来源于stack exchange,提问作者Conor Egan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 07:47:37