我正在将Gradle构建脚本从Groovy迁移到Kotlin DSL,其中没有真正记录的事情之一是如何填充额外的属性。
在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}" }
这看起来很重复。
在Kotlin中实现ExtraPropertiesExtension有点复杂,但最后,它只是简单的 old 。Map<String, Object>
所以,我的问题是:是否可以extra
更容易地填充具有多个属性的对象,而不仅仅是重复val myProp by extra { "myValue"}
?