7

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?

red888
  • 27,709
  • 55
  • 204
  • 392

3 Answers3

9

You can use Groovy Grapes:

@Grab('com.amazonaws:aws-java-sdk:1.11.205')
import ...
Jonatan Ivanov
  • 4,895
  • 2
  • 15
  • 30
  • Is that available in a jenkinsfile? its a DSL – red888 Sep 30 '17 at 20:29
  • 3
    @red888 https://stackoverflow.com/q/42248116/6309 seems to say yes, but https://stackoverflow.com/q/46137474/6309 seems to say no. For a concrete pipeline example using Grab: https://stackoverflow.com/a/44704844/6309 – VonC Sep 30 '17 at 21:12
  • 1
    According to the docs, it should be: https://jenkins.io/doc/book/pipeline/shared-libraries/#using-third-party-libraries – Jonatan Ivanov Sep 30 '17 at 21:19
  • This works but it seems I can't import inside a closure in groovy, not sure how to write a groovy script that imports and uses a java library and then use its methods in a jenkinsfile. – red888 Sep 30 '17 at 23:36
3

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

Example for importing a library:

import org.apache.http.client.utils.URIBuilder;
pipeline{
    
}
Barel elbaz
  • 426
  • 3
  • 8