2

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.

4
  • is it possible to have the class in src/ and use it in vars/?
    – rodee
    May 13, 2020 at 13:42
  • Try renaming common.groovy in CommonFuncs.groovy and moving it to src/org/common/. May 13, 2020 at 16:36
  • That worked, renamed the groovy file to match class name. thank you.
    – rodee
    May 13, 2020 at 17:06
  • @GuidoVaccarella, can you add it as answer?
    – rodee
    May 13, 2020 at 18:48

1 Answer 1

3

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
1
  • I was creating a filename with lowercase style, but remembering what u mentioned, helped me a lot, specially the words highlighted in the jenkins documentation : The basename of each .groovy file should be a Groovy (~ Java) identifier, conventionally camelCased. , reference : jenkins.io/doc/book/pipeline/shared-libraries/… Feb 20 at 3:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.