如何在Gradle 6.6中使用模块化Java设置mainClassName

huangapple go评论70阅读模式
英文:

How to set mainClassName in Gradle 6.6 with modular Java

问题

在Gradle 6.3中,在build.gradle文件中,可以像这样设置mainClassName而不会出现问题:

mainClassName = 'mod/app.Main'

然而,在Gradle 6.61中,上述代码会导致以下错误:

java.lang.module.InvalidModuleDescriptorException: 模块中未找到包mod.app

可以通过移除mainClassName中的模块部分来解决这个问题:

mainClassName = 'app.Main'

虽然错误得到了解决,但Gradle仍然会提示:

未为主类提供模块,假定使用当前模块。建议以以下格式提供'mainClassName':
'$moduleName/a.b.Main'

我尝试过以下变化:

ext.moduleName = 'mod'
mainClassName = '${ext.moduleName}/app.Main'

到目前为止,我无法使其中任何一个起作用。虽然我可以使用'app.Main'的设置,但我更倾向于使用Gradle推荐的格式。应该如何解决这个问题?

英文:

In Gradle 6.3 within build.gradle, mainClassName may be set like this with no complaints:

mainClassName = 'mod/app.Main'

In Gradle 6.61, the above line results in this:

> java.lang.module.InvalidModuleDescriptorException: Package mod.app not
> found in module

This can be resolved by removing the module portion of mainClassName:

mainClassName = 'app.Main'

While the exception is resolved, Gradle still states:

> No module was provided for main class, assuming the current module.
> Prefer providing 'mainClassName' in the following format:
> '$moduleName/a.b.Main'

I have experimented with variations of this:

ext.moduleName = 'mod'
mainClassName = '${ext.moduleName}/app.Main'

So far I have been unable to get any of these to work. I could work with the setting as 'app.Main', but I would prefer to use the variation that Gradle prefers. How should this be done?

答案1

得分: 1

Gradle 6.3不支持JPMS,但6.4及更高版本支持。因此我假设您正在使用第三方的gradle-modules-plugin。对于这个插件,您需要使用版本1.7.0或更新版本以支持较新版本的Gradle。

或者,您可以移除该插件并使用Gradle中的原生支持。目前看起来您同时混合了这两种方式。

英文:

Gradle 6.3 doesn't support JPMS, but 6.4+ does. So I assume you are using the 3rd party gradle-modules-plugin. For this plugin, you need to use version 1.7.0 or later for supporting newer versions of Gradle.

Alternatively, remove the plugin and use the native support in Gradle. Right now it looks like you are a mix of the two at the same time.

答案2

得分: 1

6.3版本中的工作 mainClassName = "$moduleName/app.Main"

6.4版本及更高版本中的工作


run {
    main = "$moduleName/app.Main";
}

英文:

work in 6.3 mainClassName = "$moduleName/app.Main"

work in 6.4+


run {
    main = "$moduleName/app.Main"
}

huangapple
  • 本文由 发表于 2020年9月17日 17:13:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63934886.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定