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 rootProject.name 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:

@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"
  }
  // …
}

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