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

Why does gradle init generate variable mainClass, while mainClassName is needed?

当我 运行 gradle init 使用 Gradle 6.7.1 时,会生成一个 gradle.build 文件,其中包含一个 mainClass 变量。然而,这需要是 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)
        }
    }
}

如果我们将 mainClassName 的两次出现都替换为 mainClass,则会出现错误消息。

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)
        }
    }
}