How to have a library of Groovy scripts callable within Jenkins?

I have built a simple jar with Gradle. It’s in my classpath and approved under the userContent directory, but I can’t figure out how to import it with @Grab. I can specify the group and version in the Gradle config, but I can’t figure out the module name. Online resources say it’s in settings.gradle, but that doesn’t work. I am losing my mind trying to figure out the module name.

Another line of attack has been as a shared library. I can get @Library to resolve, but I can’t seem to import with and without the package directive in the Groovy file. I just want to be able to build a library from Groovy that I can use on the Jenkins side, and I can’t find any clean tutorials. My Reddit post was a dead end, so I’m kind of at my wits end.

Hello @kp5160 and welcome to this community :wave:

I’m not so sure @Grab is made for your use case.
From the documentation:

Using third-party libraries
While possible, accessing third-party libraries using @Grab from trusted libraries has various issues and is not recommended. Instead of using @Grab, the recommended approach is to create a standalone executable in the programming language of your choice (using whatever third-party libraries you desire), install it on the Jenkins agents that your Pipelines use, and then invoke that executable in your Pipelines using the bat or sh step.

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:

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

Third-party libraries are cached by default in ~/.groovy/grapes/ on the Jenkins controller.

Hi @kp5160 . I am one of the Active Choice maintainers and I’ve read with interest your Reddit post and this question. I have faced similar issues as I am a heavy user of Groovy scripting in Jenkins particularly with Active Choices but also with external Groovy modules I have developed For my work in life sciences. First the @Grab annotation is no longer supported in more recent versions of Jenkins (I’m currently working with v2.364.2 which is by now over a year old…) It was useful but I had to abandon it, and use other approaches for importing 3rd party libraries in the class path of Jenkins. My favorite way for reusing Groovy scripts across projects it’s still using the Scriptler plugin, that although has fallen out of favor for pipeline use it is still critical for freestyle projects (the majority of my work). We’re you able to identify another approach? By the way I was confused about the Reddit comment that the Active Choices repo does not allow reporting issues? Have you tried JIRA Loading...