Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins Shared Library src class - unable to resolve class

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
like image 564
Schlumpf Avatar asked Jan 10 '19 09:01

Schlumpf


1 Answers

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()
like image 190
thomas.st Avatar answered Sep 20 '22 14:09

thomas.st