Hello,
When writing classes in groovy and adding them as groovy libraries in my jenkins instance. How can i make sure that the file has the necessary dependencies?
Let’s say I
import java.util.Date
I suppose something like java.util.Date will be feasible since groovy is based on java but what if i want to import something like: GitHub - jgritman/httpbuilder
I never used it, but I think you should be using @Grab annotation for such imports. To quote the sharedlib docs (which are really scarce on this):
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.
Note the emphasis on trusted – that likely means that you should be doing this solely inside a sharedlib code, and said sharedlib should be loaded as Global Trusted library in Jenkins config/