9

I was using some global methods in the /var directory of the shared library, and everything worked fine. Now I need to keep the state of the process, so I'm writting a groovy class. Basically I have a class called 'ClassTest.groovy' in '/src' which is something like this;

class ClassTest {
    String testString
    def method1() { ... }
    def method2() { ... }
}

and at the begining of the pipeline

library 'testlibrary@'
import ClassTest

with result:

WorkflowScript: 2: unable to resolve class ClassTest @line 2, column 1.
import ClassTest

before, I was just goind

library 'testlibrary@' _

and using the methods as

script {
    libraryTest.method1()
    ...
    libraryTest.method2()
}

where the methods were in a file '/var/libraryTest.groovy' and everything worked. So I know that the shared library is there, but I'm confused with the way groovy / Jenkins handle classes / shared libraries.

What's the correct way to import a class? I cannot find a simple example (with groovy file, file structure and pipeline) in the documentation.

EDIT: I moved the file to 'src/com/company/ClassTest.groovy' and modified the pipeline as

@Library('testlibrary@') import com.company.ClassTest
def notification = new ClassTest()

but now the error is

unexpected token: package @ line 2

the first two lines of the groovy file are:

// src/com/company/ClassTest.groovy
package com.company;
mkobit
  • 43,979
  • 12
  • 156
  • 150
cauchi
  • 1,463
  • 2
  • 17
  • 45
  • Documentation on what you are trying to do: https://jenkins.io/doc/book/pipeline/shared-libraries/#loading-libraries-dynamically – Matthew Schuchard May 23 '18 at 16:23
  • Try changing `@Library('testlibrary@')` to `@Library('testlibrary')` - `@` is a special separator for name+version for library dependency declaration. – mkobit May 23 '18 at 21:04
  • I need the "@" to specify the version. I don't remember what is the name of the option, but the sysadmin configured the shared library so that I need to specify the version. Since I'm just using trunk in svn for historical reasons of the project I'm in "@" with no version is what I need. – cauchi May 24 '18 at 09:29

3 Answers3

7

So far this is what I've found.

To load the library in the pipeline I used:

@Library('testlibrary@') import com.company.ClassTest
def notification = new ClassTest()

In the class file, no package instruction. I guess that I don't need one because I don't have any other files or classes, so I don't really need a package. Also, I got an error when using the same name for the class and for the file where the class is. The error specifically complained and asked for one of them to be changed. I guess this two things are related to Jenkins.

That works, and the library is loaded.

cauchi
  • 1,463
  • 2
  • 17
  • 45
0

(Maybe it can help someone else)

I was having the same issue. Once I added a package-info.java inside the folder com/lib/, containing

/**
 * com.lib package 
 */
package com.lib;

and adding package com.lib at the first line of each file, it started to work.

maufarinelli
  • 285
  • 3
  • 7
0

I had the same problem. After some trial and error with the docs of Jenkins.(https://www.jenkins.io/doc/book/pipeline/shared-libraries/#using-libraries)

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.
// MUST have the '@' at the beginning, other wise it will not work.
@Library('my-shared-library@BRANCH') _ 

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

// then call methods or attributes from the class.
exampleObject.runExample()

Dor
  • 11
  • 3