How do I import a Java library in Jenkins pipeline

Viewed 8601

I have a Java library pushed on GitHub, and I want to use it in a pipeline.

I tried:

@Library('test') _

@Library('test') _
import com.bla.bla.Bla

@Library('test') import com.bla.bla.Bla

I always get:

unable to resolve class Bla.

1 Answers

Please refer to https://jenkins.io/doc/book/pipeline/shared-libraries/

As said in the section "Using third-party libraries":

It is possible to use third-party Java libraries, typically found in Maven Central, from trusted library code using the @Grab annotation. Refer to the Grape documentation for details, but simply put:

@Grab('org.apache.commons:commons-math3:3.4.1')
import org.apache.commons.math3.primes.Primes
void parallelize(int count) {
   if (!Primes.isPrime(count)) {
       error "${count} was not prime"
   }
   // …
}

Grape documentation: http://docs.groovy-lang.org/latest/html/documentation/grape.html#_quick_start

If you want to use a local library or from github the only thing you have to do is to build the project as a maven project and then use @Grab accordingly.

Related