HiveBrain v1.2.0
Get Started
← Back to all entries
debugMinor

Unable to use the class defined in src in vars of jenkins shared library

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
thevarssharedjenkinsunablesrclibraryuseclassdefined

Problem

Here is the folder structure in my shared library repo:

$ tree src vars
src
├── org
    └── common.groovy

vars
├── testSrc.groovy


src has:

$ cat src/org/common.groovy
package org.common

class CommonFuncs {
    def sayHi() {
        echo "Hi from CommonFuncs!"
    }

    def sayHello(str) {
        echo "Hello ${str}"
    }
}


and vars:

$ cat vars/testSrc.groovy
def call() {
    def tst = new org.common.CommonFuncs()
    return tst.sayHi()
}


pipeline script in Jenkins has:

@Library('jenkins-library') _
pipeline {
    agent { label 'my_servers' }

    stages {
        stage('test') {
            steps {
                testSrc()
            }
        }
    }
}


When I run job, I get below error:

org.jenkinsci.plugins.workflow.cps.CpsCompilationErrorsException: startup failed:
/var/jenkins_home/jobs/Test/jobs/mave/jobs/test_src/builds/8/libs/jenkins-library/vars/testSrc.groovy: 2: unable to resolve class org.common.CommonFuncs 
 @ line 2, column 15.
       def tst = new org.common.CommonFuncs()
                 ^


What is wrong in here? I am new to groovy, and this is my first attempt to create a class in shared library src/, any help is appreciated.

Solution

It looks like Groovy can't find your class at the path specified.

Try renaming the file common.groovy to match the class name and move it to the common folder:

src/org/common/CommonFuncs.groovy

Code Snippets

src/org/common/CommonFuncs.groovy

Context

StackExchange DevOps Q#11592, answer score: 3

Revisions (0)

No revisions yet.