plugins {
id 'java'
id 'application'
}
application {
mainClass = "org.example.Main"
}
group = 'org.example'
version = '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation platform('org.junit:junit-bom:5.10.0')
testImplementation 'org.junit.jupiter:junit-jupiter'
implementation project(":api")
}
test {
useJUnitPlatform()
}
代码没有问题,但我知道所有已定义的变量都来自项目对象
mainClass 变量来自插件应用程序,但为什么我只能在应用程序块中分配该变量,而不能在其他任何地方分配?这个变量来自哪个类?
mainClass
在这种情况下不是变量。mainClass
是扩展上的一个属性,通过应用程序插件application
贡献给项目当你这样做时:
这实际上是 Groovy DSL(或 Kotlin DSL)在起作用,可以将其视为 Gradle 提供的底层 API 的语法糖。例如,在一个纯 Java 插件中: