无法为扩展“应用程序设置未知 属性 'mainClass'

Could not set unknown property 'mainClass' for extension 'application

我在我的项目中使用 Gradle 版本 6.1。

项目的build.gradle如下:

plugins {
    id 'application'
}

application {
    mainClass = 'com.mytestproject.Main'
}

java {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}

group 'com.mytestproject'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

但是当我 运行 这个命令 ./gradlew assemble 时,它给我以下错误:

./gradlew assemble

FAILURE: Build failed with an exception.

* Where:
Build file '/com/myproject/build.gradle' line: 6

* What went wrong:
A problem occurred evaluating root project 'JavaTestProject'.
> Could not set unknown property 'mainClass' for extension 'application' of type org.gradle.api.plugins.internal.DefaultJavaApplication.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 484ms

build.gradle修改为:

apply plugin: "application"

mainClassName = "com.mytestproject.Main"

sourceCompatibility = 1.8
targetCompatibility = 1.8

group 'com.mytestproject'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

运行 这个命令:./gradlew build

你可以试试这个(基于 the source code):

application {
    setMainClassName("some.package.Main")
}

6.4 中引入了弃用。它来自:

 application {
   mainClassName = "com.foo.bar.FooBar"
 }

收件人:

 application {
   mainClass.set("com.foo.bar.FooBar")
 }

文档:

https://docs.gradle.org/current/javadoc/org/gradle/api/plugins/JavaApplication.html#getMainClassName--

实际上只是将 application 块修改为

application {
    mainClassName = 'com.mytestproject.Main'
}

做同样的事情。

问题好像是org.gradle.api.plugins.internal.DefaultJavaApplicationclass上曾经有一个mainClass属性,后来改名为mainClassName,渲染之前的教程无效。