如何在 Gradle Kotlin DSL 中高效地填充额外属性?
How to efficiently populate extra properties in the Gradle Kotlin DSL?
我正在将 Gradle 构建脚本从 Groovy 迁移到 Kotlin DSL 并且没有真正记录的事情之一是如何填充 extra properties.
在Groovy中,我可以写:
ext {
comp = 'node_exporter'
compVersion = '0.16.0'
compProject = 'prometheus'
arch = 'linux-amd64'
tarball = "v${compVersion}/${comp}-${compVersion}.${arch}.tar.gz"
downloadSrc = "https://github.com/${compProject}/${comp}/releases/download/${tarball}"
unzipDir = "${comp}-${compVersion}.${arch}"
}
我发现在 Kotlin DSL 中,我可以通过以下方式实现相同的功能:
val comp by extra { "filebeat" }
val compVersion by extra { "6.4.0" }
val arch by extra { "linux-x86_64" }
val tarball by extra { "${comp}-${compVersion}-${arch}.tar.gz" }
val downloadSrc by extra { "https://artifacts.elastic.co/downloads/beats/${comp}/${tarball}" }
val unzipDir by extra { "${comp}-${compVersion}-${arch}" }
看起来很重复。
ExtraPropertiesExtension 在 Kotlin 中的实现有点复杂,但最后,它只是简单的旧 Map<String, Object>
.
所以,我的问题是:是否可以更轻松地使用多个属性填充 extra
对象,而不只是重复 val myProp by extra { "myValue"}
?
不用委托,直接写extra.set("propertyName", "propertyValue")
即可。如果需要,您可以使用 apply
块来执行此操作:
extra.apply {
set("propertyName", "propertyValue")
set("propertyName2", "propertyValue2")
}
根据当前 (5.2.1
) 文档:
All enhanced objects in Gradle’s domain model can hold extra user-defined properties. This includes, but is not limited to, projects, tasks, and source sets.
Extra properties can be added, read and set via the owning object’s extra
property. Alternatively, they can be addressed via Kotlin delegated properties using by extra
.
这是在 Project
和 Task
对象上使用额外属性的示例:
val kotlinVersion by extra { "1.3.21" }
val kotlinDslVersion by extra("1.1.3")
extra["isKotlinDsl"] = true
tasks.register("printExtProps") {
extra["kotlinPositive"] = true
doLast {
// Extra properties defined on the Project object
println("Kotlin version: $kotlinVersion")
println("Kotlin DSL version: $kotlinDslVersion")
println("Is Kotlin DSL: ${project.extra["isKotlinDsl"]}")
// Extra properties defined on the Task object
// this means the current Task
println("Kotlin positive: ${this.extra["kotlinPositive"]}")
}
}
我正在将 Gradle 构建脚本从 Groovy 迁移到 Kotlin DSL 并且没有真正记录的事情之一是如何填充 extra properties.
在Groovy中,我可以写:
ext {
comp = 'node_exporter'
compVersion = '0.16.0'
compProject = 'prometheus'
arch = 'linux-amd64'
tarball = "v${compVersion}/${comp}-${compVersion}.${arch}.tar.gz"
downloadSrc = "https://github.com/${compProject}/${comp}/releases/download/${tarball}"
unzipDir = "${comp}-${compVersion}.${arch}"
}
我发现在 Kotlin DSL 中,我可以通过以下方式实现相同的功能:
val comp by extra { "filebeat" }
val compVersion by extra { "6.4.0" }
val arch by extra { "linux-x86_64" }
val tarball by extra { "${comp}-${compVersion}-${arch}.tar.gz" }
val downloadSrc by extra { "https://artifacts.elastic.co/downloads/beats/${comp}/${tarball}" }
val unzipDir by extra { "${comp}-${compVersion}-${arch}" }
看起来很重复。
ExtraPropertiesExtension 在 Kotlin 中的实现有点复杂,但最后,它只是简单的旧 Map<String, Object>
.
所以,我的问题是:是否可以更轻松地使用多个属性填充 extra
对象,而不只是重复 val myProp by extra { "myValue"}
?
不用委托,直接写extra.set("propertyName", "propertyValue")
即可。如果需要,您可以使用 apply
块来执行此操作:
extra.apply {
set("propertyName", "propertyValue")
set("propertyName2", "propertyValue2")
}
根据当前 (5.2.1
) 文档:
All enhanced objects in Gradle’s domain model can hold extra user-defined properties. This includes, but is not limited to, projects, tasks, and source sets.
Extra properties can be added, read and set via the owning object’s
extra
property. Alternatively, they can be addressed via Kotlin delegated properties usingby extra
.
这是在 Project
和 Task
对象上使用额外属性的示例:
val kotlinVersion by extra { "1.3.21" }
val kotlinDslVersion by extra("1.1.3")
extra["isKotlinDsl"] = true
tasks.register("printExtProps") {
extra["kotlinPositive"] = true
doLast {
// Extra properties defined on the Project object
println("Kotlin version: $kotlinVersion")
println("Kotlin DSL version: $kotlinDslVersion")
println("Is Kotlin DSL: ${project.extra["isKotlinDsl"]}")
// Extra properties defined on the Task object
// this means the current Task
println("Kotlin positive: ${this.extra["kotlinPositive"]}")
}
}