Skip to content

Use Reflection in Jenkins Pipeline

Jenkins Pipeline's class loading is different from the normal Java class loading. It has its own designed class loading within the plugins.

Here is the official introduction dependencies-and-class-loading.

Briefly, if we want to use reflection to dynamic load class defined in pipeline, we need to use Thread.currentThread().getContextClassLoader().

Java
Class<?> cls = Class.forName(className, true, Thread.currentThread().getContextClassLoader())
return cls.getDeclaredConstructors()[0].newInstance()