Groovy 传递映射和首先使用`body.resolvestracture=Closure.DELEGATE_'有什么区别`

Groovy 传递映射和首先使用`body.resolvestracture=Closure.DELEGATE_'有什么区别`,groovy,jenkins-pipeline,jenkins-groovy,Groovy,Jenkins Pipeline,Jenkins Groovy,这两个封装管道的示例从两种不同的方法获得它们的pipelineParams,但是不清楚为什么一种方法优于另一种方法 使用的后果是什么 def call(body) { // evaluate the body block, and collect configuration into the object def pipelineParams= [:] body.resolveStrategy = Closure.DELEGATE_FIRST body.deleg

这两个封装管道的示例从两种不同的方法获得它们的
pipelineParams
,但是不清楚为什么一种方法优于另一种方法

使用的后果是什么

def call(body) {
    // evaluate the body block, and collect configuration into the object
    def pipelineParams= [:]
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = pipelineParams
    body()

    pipeline {
        echo pipelineParams.name
    }
}
vs使用

def call(Map pipelineParams) {
    pipeline {
        echo pipelineParams.name
    }
}

来自

的示例代码区别在于,在第一种情况下,使用管道类似于声明性配置。这就是所谓的DSL:

myDeliveryPipeline {
    branch = 'master'
    scmUrl = 'ssh://git@myScmServer.com/repos/myRepo.git'
    ...
}
而在第二种情况下,应用管道看起来像命令式代码,即它是一个常规函数调用:

myDeliveryPipeline(branch: 'master', scmUrl: 'ssh://git@myScmServer.com/repos/myRepo.git', ...)
还有以下解释:

还有一个“构建器模式”技巧,首先使用Groovy的Closure.DELEGATE_,它允许Jenkinsfile看起来更像一个配置文件而不是一个程序,但这更复杂,更容易出错,因此不推荐使用

就个人而言,我不能说我不推荐DSL方法。文档不建议这样做,因为它有点复杂,而且容易出错