我使用TeamCity Kotlin DSL 2018.1来设置构建配置。我的settings.kts文件如下:
version = "2018.1"
project {
buildType {
id("some-id")
name = "name"
steps {
ant {
name = "Step1"
targets = "target1"
mode = antFile { path = "/some/path" }
workingDir = "/some/dir"
jdkHome = "some_jdk"
}
ant {
name = "Step2"
targets = "target2"
mode = antFile { path = "/some/path" }
workingDir = "/some/dir"
jdkHome = "some_jdk"
}
...
}
}
}
它按预期工作,但我希望避免反复编写相同的重复参数。
我尝试编写函数,它将构建步骤填充默认值:
fun customAnt(init: AntBuildStep.() -> kotlin.Unit): AntBuildStep {
val ant_file = AntBuildStep.Mode.AntFile()
ant_file.path = "/some/path"
val ant = AntBuildStep()
ant.mode = ant_file
ant.workingDir = "/some/dir"
ant.jdkHome = "some_jdk"
return ant
}
project {
buildType {
id("some-id")
name = "name"
steps {
customAnt {
name = "Step1"
targets = "target1"
}
customAnt {
name = "Step2"
targets = "target2"
}
...
}
}
}
代码编译成功,但无法正常工作:TeamCity忽略了以这种方式定义的构建步骤。
不幸的是,官方文档中没有关于自定义和扩展DSL的任何信息。可能我在使用Kotlin的() -> Unit
结构时出了问题,但无法确定具体出了什么问题。