向全局共享库传递参数——构造函数

Passing parameters to global shared library - constructor

我有一个简单的问题: 我有一个全局(非沙盒)/src 库 - utils 我需要在构建时将参数传递给 utils

@Library("Woop")
def utils = new a.b.c.d.Utils(Script::this)

有效,脚本参考存放于:

public class Utils implements Serializable {
    Script script

如果我尝试添加一个参数:

@Library("Woop")
def utils = new a.b.c.d.Utils(Script::this, 3)

并更改 Utils 以添加 int

public class Utils implements Serializable {
    Script script
    int three

我得到:

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException:
unclassified new a.b.c.d.Utils java.util.LinkedHashMap java.lang.Integer

我不确定如何传递多个参数,以及如何接受多个参数。我所能做的就是猜测,这是非常低效和令人沮丧的。

奖金问题

给定文件夹库 \vars\Abc.Groovy,通过 @Library('Project') _ 访问,我如何将此库传递到共享全局库? 假设我们解决了最初的问题,我只是调用

new a.b.c.d.Utils(Script::this, Abc)

new a.b.c.d.Utils(Script::this, Abc.class)? 它应该如何在 Utils 中定义 class?

我不清楚,因为 Abc 应该是 class Abc 的静态实例,所以我在这里很困惑...:-(

Jenkins 的文档太糟糕了,令人尴尬。通常,将参数传递给构造函数是您希望记录的内容...

如果你改变它可能会起作用:

int three

Integer three

在您的实用程序中 class。否则我会尝试显式添加构造函数,如下所示:

Utils(Script s, Integer i) { 
    script = s
    three = i
}

这不是管道或 cpsLibrary 问题。要使用命名参数调用 groovy 中的构造函数,您必须使用:

new a.b.c.d.Utils(script: this, three: Abc)

尽管到目前为止我从未在 cps 代码中使用过命名参数,但我只是尝试过并且它有效。 也可以看看: Groovy could not find matching constructor?