I am trying to writing a Jenkins Shared Library for my CI process. I'd like to reference a class that is in the \src
folder inside a global function defined in the \vars
folder, since it would allow me to put most of the logic in classes instead of in the global functions. I am following the repository structure documented on the official Jenkins documentation:
Jenkins Shared Library structure
Here's a simplified example of what I have:
/src/com/example/SrcClass.groovy
package com.example
class SrcClass {
def aFunction() {
return "Hello from src folder!"
}
}
/vars/classFromVars.groovy
import com.example.SrcClass
def call(args) {
def sc = new SrcClass()
return sc.aFunction()
}
Jenkinsfile
@Library('<lib-name>') _
pipeline {
...
post {
always {
classFromVars()
}
}
}
My goal was for the global classes in the /vars
folder to act as a sort of public facade and to use it in my Jenkinsfile
as a custom step without having to instantiate a class in a script
block (making it compatible with declarative pipelines). It all seems pretty straightforward to me, but I am getting this error when running the classFromVars
file:
<root>\vars\classFromVars.groovy: 1: unable to resolve class com.example.SrcClass
@ line 1, column 1.
import com.example.SrcClass
^
1 error
I tried running the classFromVars
class directly with the groovy
CLI locally and on the Jenkins server and I have the same error on both environments. I also tried specifying the classpath when running the /vars
script, getting the same error, with the following command:
<root>>groovy -cp <root>\src\com\example vars\classFromVars.groovy
Is what I'm trying to achieve possible? Or should I simply put all of my logic in the /vars
class and avoid using the /src
folder?
I have found several repositories on GitHub that seem to indicate this is possible, for example this one: https://github.com/fabric8io/fabric8-pipeline-library, which uses the classes in the /src
folder in many of the classes in the /vars
folder.
As @Szymon Stepniak pointed out, the -cp
parameter in my groovy
command was incorrect. It now works locally and on the Jenkins server. I have yet to explain why it wasn't working on the Jenkins server though.