2

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!

  • I was trying this `@Grab` https://github.com/jenkinsci/workflow-cps-global-lib-plugin/blob/master/README.md#using-third-party-libraries but it doens't work – Joan Dec 22 '16 at 14:36

2 Answers2

7

The only method I've found so far that works was to run this in the pipeline to find out what directories are being checked:

println System.getProperty("java.ext.dirs")

And in my case, it was looking in

/usr/java/packages/lib/ext

So I put the jar I wanted to load in that location (after having to create the directory), and then restarted Jenkins.

Afterwards I was successfully able to do an import of the library and use it.

Seems very hacky and the sort of thing that might be considered a bug and removed without notice.

Peter McNab
  • 1,031
  • 1
  • 10
  • 11
0

If you are using external library (@Library) in your pipeline, you can define grape dependencies via Grab. Example below from ciinabox-pipelines shared library. This will download jars and load them automatically in groovy script.

@Grab(group='com.amazonaws', module='aws-java-sdk-ec2', version='1.11.198')

import com.amazonaws.services.ec2.* import com.amazonaws.services.ec2.model.* import com.amazonaws.regions.*

What is important that code above probably won't work in pipeline itself, but when loaded as part of shared library, it should with latest plugin versions.

toske
  • 1,744
  • 13
  • 24