How can i use java libraries in a jenkins pipeline?

Viewed 8931

Is it possible to import and use regular java libraries in a jenkins pipeline Shared Library? I want to import the AWS SDK for java and using it directly in the pipeline code. Do I have to add it to the src dir of my Shared Library?

3 Answers

You can use Groovy Grapes:

@Grab('com.amazonaws:aws-java-sdk:1.11.205')
import ...

From this answer, check the output of println System.getProperty("java.ext.dirs")

Then try and put our jar in that folder (as seen in this other answer).

This is at best a workaround though, and the "Extending with Shared Libraries" only mentions classpath as part of an official shared library directory structure:

The src directory should look like standard Java source directory structure. This directory is added to the classpath when executing Pipelines.

Example for importing a library:

import org.apache.http.client.utils.URIBuilder;
pipeline{
    
}
Related