为什么gradle init生成mainClass变量,而mainClassName是需要的?



当我用Gradle 6.7.1运行gradle init时,构建了一个具有mainClass变量的gradle.build文件。然而,这需要是mainClassName,如果使用mainClass,它就会失败。

为什么gradle init会产生不可用的代码?

gradle.build相关部分:

application {
// Define the main class for the application.
mainClassName = 'com.mycompany.MyFileKt'
}
jar {
manifest {
attributes 'Main-Class': mainClassName
}
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
}

如果用mainClass替换出现的mainClassName,会报错。

FAILURE: Build failed with an exception.
* Where:
Build file '/Users/joshua/dev/multicloud/app/build.gradle' line: 47
* What went wrong:
A problem occurred evaluating project ':app'.
> Could not get unknown property 'mainClass' for object of type org.gradle.api.java.archives.internal.DefaultManifest.

"生成不可用的代码"是什么意思?jar { ... }部分是gradle init生成的吗?在我看来,它像是从遗留脚本复制粘贴的。application对象的mainClassName属性已弃用,应该使用mainClass。不同之处在于它不能再作为根级属性访问了。

下面的代码应该可以工作:

application {
// Define the main class for the application.
mainClass = 'com.mycompany.MyFileKt'
}
jar {
manifest {
attributes 'Main-Class': application.mainClass
}
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
}

最新更新