0

I have a Jenkins job where as part of the build action I am executing a groovy script, say script A. From within script A I am trying to execute method of Test1. Both the files A.groovy and Test1.groovy are in the same directory. I am getting "unable to resolve class" error whenever I run the job.

A.groovy

println "****************** TEST : START  *******************************"

println " Environment Variable value is : "
new Test1().printEnv()

println "****************** TEST : END  *******************************"

I am using Groovy 2.3.6.

Test1.groovy

class Test1{

def printEnv(){

println "****************** TEST inside Test1.groovy : START  *******************************"

println "****************** TEST inside Test1.groovy : END  *******************************"
} 

}

Error :

unable to resolve class Test1 

NOTE : When I execute my script from outside jenkins then this works. Its only when I try to execute it through Jenkins that it is unable to find class Test1.

I am very much confused with this behavior. There are many posts out there that discuss how to resolve the "unable to resolve class Test1" error. but none seem to be applicable to my case as the call is fine when its made from outside the Jenkins.

I believe I have provided all the info I could. Still, please feel free to ask for more info, if the need arises.

Asif Kamran Malick
  • 2,409
  • 3
  • 25
  • 47

1 Answers1

0

If you're using Jenkins pipelines, you'll need to load your secondary groovy file. You can find the documentation on how you can use groovy source files into a pipeline here.

In your case, you could do something like:

A.groovy

def test1 = load 'Test1.groovy'
test1.printEnv()

Test1.groovy

class Test1 {
  def printEnv() {
    // TODO
  }
}
return new Test1()

Note the return in Test1.groovy explained in the documentation.

Hope that helps

segalaj
  • 1,181
  • 12
  • 23
  • I'm not executing a pipeline script. Its not pipeline as code that I'm following. I'm just executing my groovy script from inside my job. I have specified the script file location under the subsection "Execute Groovy Script" subsection of "Build" section. – Asif Kamran Malick Jun 18 '19 at 18:35