从 git 导入类 - 无法解析类 [英] Importing class from git - unable to resolve class

查看:24
本文介绍了从 git 导入类 - 无法解析类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个共享库,我希望它具有类定义,例如:

I am constructing a Shared Library which I want to have class definitions, such as:

package com.org.pipeline.aws

String family
JsonObject taskDefinition

class TaskDefinition {

  def getTaskDefinition(family) {
    def jsonSlurper = new groovy.json.JsonSlurper()
    def object = slurper.parseText(
      sh(returnStdout: true,
        script: "aws ecs describe-task-definition --task-definition ${family}"
      )
    )
    return assert object instanceof Map
    taskDefinition = object["taskDefinition"]
  }
}

我正在通过单独的 git 存储库导入它

And I am importing it via a separate git repository

library identifier: 'jenkins-shared-libraries@master', retriever: modernSCM(
  [$class: 'GitSCMSource',
   remote: 'ssh://git@bitbucket.org.net/smar/jenkins-shared-libraries.git',
   credentialsId: 'jenkins-bitbucket-ssh-private-key'])

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                script {
                    def z = new com.org.pipeline.aws.TaskDefinition()
                    withAWS(region: 'ap-southeast-1', credentials: 'awsId') {
                        z.getTaskDefinition("web-taskdef")
                    }
                }
            }
        }
    }
}

但它一直给我这个错误:无法解析类 com.org.pipeline.aws.TaskDefinition().知道为什么吗?

But it keeps giving me this error: unable to resolve class com.org.pipeline.aws.TaskDefinition(). Any idea why?

推荐答案

动态加载共享库 在编译管道之前在类加载器期间使您的预期类可用有点棘手,因为:

Loading Shared library dynamically is a bit tricky to make your expected class available during the class loader before compilation of the pipeline, because of :

(复制自 Jenkins 文档)也可以使用 src/目录中的类,但比较棘手.@Library 注释在编译之前准备脚本的类路径",当遇到库步骤时,脚本已经被编译.因此你不能导入

(copied from Jenkins documentation) Using classes from the src/ directory is also possible, but trickier. Whereas the @Library annotation prepares the "classpath" of the script prior to compilation, by the time a library step is encountered the script has already been compiled. Therefore you cannot import

但是您可以通过这种方式使您的方法静态并从管道访问,如下例所示:

But you can make your method static and access from the pipeline this way, like the below example:

共享库文件夹结构如下:

.
├── src
│   └── net
│       └── samittutorial
│           ├── Math.groovy

Math.groovy

package net.samittutorial
class Math implements Serializable {
    def pipeline 
    Math(def pipeline) {
        this.pipeline = pipeline
    }
    Math() {

    }
    def writeAndDisplayContent(int x, int y) {
        pipeline.sh """
            echo ${x+y} > ${pipeline.env.WORKSPACE}/result.txt
            cat ${pipeline.env.WORKSPACE}/result.txt
        """
    }

    static def substract(int x, int y) {
        return x - y
    }

    static def add(int x, int y) {
        return x + y
    }

    static def info() {
        return "Hello from Math class"
    }
}

Jenkinsfile

def d = library identifier: 'jenkins-shared-libraries@question/stackoverflow', retriever: modernSCM([
        $class: 'GitSCMSource',
        remote: 'https://github.com/samitkumarpatel/jenkins-shared-libs.git'
]) net.samittutorial

pipeline {
    agent any
    stages {
        stage('debug') {
            steps {
                script {
                    //info
                    println d.Math.info()

                    //add
                    println d.Math.add(5,5) 

                    // writeAndDisplayContent(1,2) is non static , it will not work like d.Math(this).writeAndDisplayContent(1,2)
                }
            }
        }
    }
}

但是,如果您像 this Jenkins 管道将没有任何限制或限制,您可以像下面的示例一样继续,并在您的管道流程中的任何地方使用您的类.

But If you load your library like this Jenkins pipeline will have no restriction or limitation, you can go ahead like below example and use your class where ever you want on your pipeline flow.

共享库结构Math.groovy将与上面的示例保持相同

Shared Library structure and Math.groovy will remain the same like above example

Jenkinsfile 将如下所示:

@Library('jenkins-shared-library') _

import net.samittutorial.Math

pipeline {
    agent any;
    stages {
        stage('debug') {
            steps {
                echo "Hello World"
                script {
                    def math = new Math(this)
                    println math.add(4,5)
                    math.writeAndDisplayContent(1,2)
                }
            }
        }
    }
}

这篇关于从 git 导入类 - 无法解析类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆