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

a_s*_*ber 10 java gradle gradlew build.gradle

我在我的项目中使用 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'
}
Run Code Online (Sandbox Code Playgroud)

但是当我运行这个命令时./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
Run Code Online (Sandbox Code Playgroud)

Ani*_* B. 10

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'
}
Run Code Online (Sandbox Code Playgroud)

运行这个命令: ./gradlew build

  • @张丹尼尔;您链接到的文档适用于当前的 Gradle 版本,今天是 6.4.1。您可能使用旧版本的 Gradle,例如在版本 6.1 中,属性名称为 `mainClassname` (https://docs.gradle.org/6.1/userguide/application_plugin.html) (9认同)
  • 但这是为什么呢?我正在关注文档并收到该错误:https://docs.gradle.org/current/userguide/application_plugin.html (3认同)
  • @C0deAttack:以防万一有人像我在 6.1 中那样绊倒这个。正确的属性名称是 mainClassName 而不是 mainClassname ... (2认同)