Difference between the 'library' step and the @Library annotation

Issue

In my pipeline, I want to load a shared library. I read the official documentation but I have a problem. When using the annotation @Library, I don’t have any problem but when using the step library, I cannot get my Pipeline to work.

Environment

  • Jenkins
  • CloudBees Jenkins Enterprise - Managed Master (CJEMM)
  • CloudBees Jenkins Enterprise- Operations Center (CJEOC)
  • CloudBees Jenkins Team (CJT)
  • CloudBees Jenkins Platform - Client Master (CJPCM)
  • CloudBees Jenkins Platform - Operations Center (CJPOC)

Resolution

When trying to load a shared library into a Pipeline, you can use both @Library annotation on the import, or the library step. However, they don’t behave exactly the same way.

The annotation @Library will use the ClassLoader of the Pipeline to load the classes that you want to use in your Pipeline Script.

When you are using the step library, it doesn’t use the ClassLoader. So it has to guess whether you are loading a class or a package. If you are not following the Java Naming Convention, this can be problematic. For example, with:

def lib = library('foo').bar

it will assume that bar is a package name in the foo library.

In the end, it is important to follow the Java convention when naming your shared library classes.

References

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.