如何使用Gradle设置JPMS模块的ModuleMainClass属性? [英] How can I set the ModuleMainClass attribute of a JPMS module using Gradle?

查看:347
本文介绍了如何使用Gradle设置JPMS模块的ModuleMainClass属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Gradle(6.0.1)与 moduleplugin 一起使用使用JDK 13从 JPMS模块中构建应用程序.

I'm using Gradle (6.0.1) with the moduleplugin to build an application out of JPMS modules using JDK 13.

但是,即使使用了 application插件,并且&它的mainClassName设置不会在module-info.class中设置ModuleMainClass属性,因此当我将其链接到独立的JVM并运行java -m mymodule时,会显示以下消息:

However, even with the application plugin applied & its mainClassName set it does not set the ModuleMainClass attribute in module-info.class, so when I jlink it up into a standalone JVM and run java -m mymodule I get this message:

module mymodule does not have a ModuleMainClass attribute, use -m <module>/<main-class>

在后台进行挖掘看起来好像moduleplugin根本不会更改jar任务,并且开箱即用的gradle jar任务实际上并没有使用

Digging under the hood it looks like the moduleplugin doesn't change the jar task at all, and the out of the box gradle jar task does not actually use the JDK's jar command, it just zips everything up itself.

据我所知,在module-info.class中设置ModuleMainClass属性的唯一方法是使用JDK的jar命令:jar --main-class=CLASSNAME -C <classes dir>.

As far as I can tell the only way to set the ModuleMainClass attribute in module-info.class is to use the JDK's jar command as so: jar --main-class=CLASSNAME -C <classes dir>.

除了编写自己的gradle任务之外,还有其他方法可以做到吗?如果不是,有没有人举过用调用JDK命令的替换gradle jar任务的示例?

Is there a way to do this, short of writing my own gradle task? And if not, has anyone got an example of replacing the gradle jar task with one that calls the JDK command?

(请注意,关于在jar的MANIFEST.MF中设置Main-Class的问题不是 ,这很容易,但在调用java -m <modulename>时却不受尊重.)

(Please note this question is not about setting the Main-Class in the jar's MANIFEST.MF - that's easy, but isn't respected when calling java -m <modulename>.)

推荐答案

似乎gradle中的jar任务无法正确完成构建jar的工作,以在module-info.class中也包含module-main-class.实际上,它看起来根本没有调用jar命令,这有点误导.因此,这是执行此操作的更新版本:

It looks like the jar task in gradle does not do a proper job of building the jar to also include the module-main-class in the module-info.class. In fact it doesn't look like it calls the jar command at all which is a bit misleading. So here is an updated version that does this:

jar {
    doLast {
        project.exec {
            workingDir destinationDirectory.get()
            executable 'jar'
            args '--update'
            args '--file', archiveFileName.get()
            args '--main-class', mainClassName
            args '.'
        }
    }
}

Jar命令语法部分来自于这看起来有些古怪,但是可以正常工作,希望有一天Jar任务将包括最后一部分.

This looks janky, but it works and hopefully some day the Jar task will include that last part.

这篇关于如何使用Gradle设置JPMS模块的ModuleMainClass属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆