Jenkins共享库:无法导入包`没有此类属性`

Jenkins共享库:无法导入包`没有此类属性`,jenkins,groovy,jenkins-pipeline,Jenkins,Groovy,Jenkins Pipeline,我有一个像这样动态加载库的文件 def lib=library(identifier: 'pipeline-core@master', retriever: modernSCM( [$class: 'GitSCMSource', remote: 'https://scm.intra/scm/jenins/pipeline-core.git', credentialsId: 'bitbucket.service.user' ])).ch.swisscard.pipe

我有一个像这样动态加载库的文件

def lib=library(identifier: 'pipeline-core@master', retriever: modernSCM(
  [$class: 'GitSCMSource',
       remote: 'https://scm.intra/scm/jenins/pipeline-core.git',
       credentialsId: 'bitbucket.service.user'
])).ch.swisscard.pipeline.util.Utils

defaultCdPipeline {}
defaultCdPipeline
是一个使用
Utils
类的jenkins管道定义

import ch.mycompany.jenkins.pipeline.util.*
...
Utils.isRunning()
...
文件结构如下所示:

+- src
|  +- mycompany
|     +- jenkins
|        +- pipeline
|           +- util
|              +- Utils.groovy
|              +- Commons.groovy
+- vars
   +- defaultCdPipeline.groovy
到目前为止,这是可行的。当我查看动态导入时,我的理解是使用
lib
so
lib.isRunning()
而不是
Utils.isRunning()
,但这会产生以下错误

为什么??接下来,我将同时使用
Utils.groovy
Commons.groovy
。我将“预选包”,如中所示,因此仅使用
.ch.swiscard.pipeline.util

def lib=library(identifier: 'pipeline-core@master', retriever: modernSCM(
  [$class: 'GitSCMSource',
       remote: 'https://scm.intra/scm/jenins/pipeline-core.git',
       credentialsId: 'bitbucket.service.user'
])).ch.swisscard.pipeline.util

defaultCdPipeline {}
但是,这也不能作为调用
lib.Uils.isRunning()

抛出与上述相同的异常

内容如果
Utils

package ch.mycompany.jenkins.pipeline.util

class Utils implements Serializable {

    @NonCPS
    def static String isRunning() {
        return "isRunning()"
    }
}

有没有人能解释一下这个问题,告诉我如何正确加载一个包/多个类?

您应该只声明库加载,如下所示:

def lib = library(
    identifier: 'pipeline-core@master', retriever: modernSCM
    (
        [
            $class: 'GitSCMSource',
            remote: 'https://scm.intra/scm/jenins/pipeline-core.git',
            credentialsId: 'bitbucket.service.user'
        ]
    )
)
lib.ch.mycompany.jenkins.pipeline.util.Utils.isRunning();
// call the constructor (you can also call a constructor with parameters)
def utils = lib.ch.mycompany.jenkins.pipeline.util.Utils.new();

// call your instance method
utils.isItReallyRunning();
假设这是groovy类:

package ch.mycompany.jenkins.pipeline.util

class Utils implements Serializable
{
    @NonCPS
    def static String isRunning()
    {
        return "isRunning()";
    }

    def String isItReallyRunning()
    {
        return "isItReallyRunning()";
    }
}
然后调用如下静态方法:

def lib = library(
    identifier: 'pipeline-core@master', retriever: modernSCM
    (
        [
            $class: 'GitSCMSource',
            remote: 'https://scm.intra/scm/jenins/pipeline-core.git',
            credentialsId: 'bitbucket.service.user'
        ]
    )
)
lib.ch.mycompany.jenkins.pipeline.util.Utils.isRunning();
// call the constructor (you can also call a constructor with parameters)
def utils = lib.ch.mycompany.jenkins.pipeline.util.Utils.new();

// call your instance method
utils.isItReallyRunning();
还有这样一个实例方法:

def lib = library(
    identifier: 'pipeline-core@master', retriever: modernSCM
    (
        [
            $class: 'GitSCMSource',
            remote: 'https://scm.intra/scm/jenins/pipeline-core.git',
            credentialsId: 'bitbucket.service.user'
        ]
    )
)
lib.ch.mycompany.jenkins.pipeline.util.Utils.isRunning();
// call the constructor (you can also call a constructor with parameters)
def utils = lib.ch.mycompany.jenkins.pipeline.util.Utils.new();

// call your instance method
utils.isItReallyRunning();