First, I´m new to Java, Groovy and Jenkins so please be patient with me ;)
I´m preparing a Jenkins server with Pipeline support for future use in our build environment. We use a special inhouse scripting language for which i have to write a wrapper in java. There is no option to do the work only in Groovy, we have to use this special language.
I have tried many methods of referencing the java lib to this jenkins project but neither worked. Mainly i´ve used the documentation on https://github.com/jenkinsci/workflow-cps-global-lib-plugin to implement this but also tried several approaches searching google or stackoverflow. Following the documentation, this include should be possible.
I´ve reduced the process to a test setup for testing purposes.
Assume the following...
I have a multibranch project in Jenkins named 'MultibranchTestProject01'.
The Jenkinsfile:
@Library('DeltaJenkinsScripts@develop')
def runStageCollect = true
if (runStageCollect)
{
stage("Collect")
{
helloWorld("Joe")
}
}
The referenced library is referenced globally via 'Global Pipeline Libraries' in the Jenkins settings but also explicitly here to clarify things. It´s hosted in a git environment and the referencing seems to work. The file structure of this library:
/vars/helloWorld.groovy
package de.dcomp.prod
def call(name) {
def tt = new Test()
tt.testText()
}
/src/de/dcomp/prod/Test.groovy
package de.dcomp.prod
import de.dcomp.ftel.*
def testText()
{
def sRetVal = ""
echo "testText - START"
//sRetVal = ScriptRunner.GetStaticSampleText()
def oSR = new ScriptRunner()
sRetVal = oSR.GetInstanceSampleText()
echo "ReturnValue: ${sRetVal}"
}
I have a java lib called ScriptRunner-0.0.1-SNAPSHOT.jar. This library has a single class:
package de.dcomp.ftel;
public class ScriptRunner
{
public String GetInstanceSampleText()
{
return "ScriptRunner.GetInstanceSampleText() called...";
}
public static String GetStaticSampleText()
{
return "ScriptRunner.GetStaticSampleText() called...";
}
}
I have no problem in referencing and using this library in a standalone java project.
I´ve tried several ways to include it:
- Put the jar file to 'C:\Users\cr.groovy\lib'
- Setting the Classpath in a testing linux environment.
- Using the plugin "Pipeline: Classpath Steps" to add the library to the classpath in different notations, e.g. 'C:\Users\cr.groovy\lib', C:/Users/cr/.groovy/lib', 'C:\Users\cr.groovy\lib\ScriptRunner-0.0.1-SNAPSHOT.jar', 'C:/Users/cr/.groovy/lib/ScriptRunner-0.0.1-SNAPSHOT.jar', 'file:///C:/Users/cr/.groovy/lib/ScriptRunner-0.0.1-SNAPSHOT.jar'
- adding the lib to a local maven repository and referencing per @GrabResolver and @Grab, though this is not the solution i would like to have
or dynamic loading with:
this.class.classLoader.rootLoader.addURL(new URL("file:///C:/Users/cr/.groovy/lib/ScriptRunner-0.0.1-SNAPSHOT.jar"));
def srClass = Class.forName("de.dcomp.ftel.ScriptRunner")
def sr = srClass.newInstance()
The result is always something like this.
groovy.lang.MissingPropertyException: No such property: ScriptRunner for class: de.dcomp.prod.Test
or this:
de/dcomp/prod/Test.groovy: 10: unable to resolve class ScriptRunner
@ line 10, column 12.
def oSR = new ScriptRunner()
The error messages point always in the direction that the process cannot find the Java library. The same thing happened if i try to use some other library, e.g. from Apache Commons.
I would like to avoid writig it as a plugin if this is possible.
Thanks in advance!