14

I am looking for some guidance on the design of a Jenkins Shared Library class. Using global vars as shared library is working fine but everytime I define a class in src/ and I want to import it, I get the error unable to resolve class.

This is my shared library structure:

src
  - de
   - schlumpf
     - Tester.groovy
vars
  - sayHello.groovy

Class Tester.groovy

Here is the code of my class which I want to initialize inside a pipeline job.

package de.schlumpf

public class Tester implements Serializable {
  public String name = "test"

  Tester(String pName) {
    this.name = pName
  }

  def sayHi() {
    echo "Hello, ${this.name}."
  }

  def sayHi(String name) {
    echo "Hello, ${name}."
  }
}

Var sayHello.groovy

#!/usr/bin/env groovy

def call(String name = 'human') {
  echo "Hello, ${name}."
}

Pipeline Job

@Library('pipeline-library-demo')
import de.schlumpf.Tester //de.schlumpf doesn't work as well

stage('Demo') {
    echo 'Hello world'
    sayHello 'test'

    def t = new Tester('Alice')
    t.sayHi()
}

In line 2 I get the error: Unable to resolve class de.schlumpf.Tester. The global variable sayHello works like a charm... Does anyone know what I am doing wrong here?

The Shared Libary is imported in the System settings: enter image description here

I know this looks similar to this one, but I can't find a typo or something in my path... Using Jenkins Shared Libraries as classes

The official documentation is here: https://jenkins.io/doc/book/pipeline/shared-libraries/

Version

  • Jenkins: ver. 2.150.1
  • Pipeline 2.6
  • Pipeline: Groovy 2.61.1
  • Pipeline: Shared Groovy Libraries 2.12
Schlumpf
  • 358
  • 1
  • 3
  • 13
  • It is generally easier to use `src` code in your `vars` code and then use that within your Pipeline. – Matthew Schuchard Jan 10 '19 at 16:36
  • I have read about that several times. But then I have to use static methods. In the vars folder I can't use classes with objects right? Well one can do that, but I don't understand why it is so complex to use the src folder. I mean it is implemented so why it is not working as it is supposed to be... Generally I would like to use objects instead of static code. – Schlumpf Jan 11 '19 at 09:00
  • I encounter the same issue. Did you find a solution already? – jaques-sam Feb 18 '20 at 13:49
  • No not yet, I have used the Var solution instead. But I have not tested it anymore since I have implemented it – Schlumpf Feb 19 '20 at 08:31
  • @MattSchuchard what if there is enum (in src) that should be passed as parameter to the function (in var) from the Jenkinsfile? – Ewoks Oct 21 '20 at 18:25
  • @Ewoks Global var (var) methods invoke library (src) class methods, so it sounds like a reverse problem and re-architecture solution. – Matthew Schuchard Oct 21 '20 at 19:49
  • ok, I didn't quite understand that, probably my explanation was bad. If there is enum parameter of the library functions, where that enum should be and how to pass it to library function from the Jenkinsfile? – Ewoks Oct 21 '20 at 20:38
  • Any news on this? – Matheus Lemos Jul 26 '22 at 16:35
  • @MatheusLemos does this help you https://stackoverflow.com/a/66517188/1586660 ? – thomas.st Dec 15 '22 at 19:13

3 Answers3

2

I had a similar issue calling a static function, when I loaded the library dynamically: https://www.jenkins.io/doc/book/pipeline/shared-libraries/#loading-libraries-dynamically

This should work for you:

def myLib = library 'pipeline-library-demo'
def t = myLib.de.schlumpf.Tester.new('Alice')
t.sayHi()
thomas.st
  • 393
  • 2
  • 5
  • 17
1

it throwing an error because you have created an object of a class outside script block. try below code and it should work.

@Library('pipeline-library-demo')
import de.schlumpf.*;

stages{
    stage('Demo') {  
      steps{
        echo 'Hello world'
        sayHello 'test'
       script{
        def t = new Tester('Alice')
        t.sayHi()
       }
      }
   }
 }  
0

I found that when I wanted to import a class from the shared library I have, I needed to do it like this:

//thanks to '_', the classes are imported automatically.
@Library('my-shared-library@BRANCH') _ 

// only by calling them you can tell if they exist or not.
def exampleObject = new example.GlobalVars() 

Dor
  • 11
  • 3