我试图从myList(如下)的一个全局位置使用一个称为settings.kts的字符串(选项)列表,因为我试图使其更具可维护性,但我不断收到此错误Expression is inaccessible from a nested class 'MySubProject'。我一直在阅读有关Kotlin以及如何使用它的信息,但我一直无法弄清楚。也许有Kotlin或TeamCity DSL经验的人可以帮助我?

import jetbrains.buildServer.configs.kotlin.v2019_2.project
import jetbrains.buildServer.configs.kotlin.v2019_2.Project

var myList = listOf("Option 1", "Option 2")

project {

        params {
            select("system.MyOption",
                    myList[0],
                    label = "My Option",
                    description = "It's an option.",
                    options = myList)
    }

    subProject(MySubProject)
}

object MySubProject : Project ({
    params {
        select("system.MySubOption",
                myList[0],
                label = "My Sub Option",
                description = "It's another option.",
                options = myList)
    }
})

最佳答案

因此,我最终要做的是像这样进行对象声明:

object MyGlobalConfig {
    var myList = listOf("Option 1", "Option 2")
}

然后像这样在脚本的其他部分中引用该值:
project {

        params {
            select("system.MyOption",
                    MyGlobalConfig.myList[0],
                    label = "My Option",
                    description = "It's an option.",
                    options = MyGlobalConfig.myList)
    }

    subProject(MySubProject)
}

object MySubProject : Project ({
    params {
        select("system.MySubOption",
                MyGlobalConfig.myList[0],
                label = "My Sub Option",
                description = "It's another option.",
                options = MyGlobalConfig.myList)
    }
})

它有效,这是我能想到的最好的方法。

08-05 18:36