Replace classpath of a running Groovy Script (Jenkins Pipeline script)

1.1k views Asked by At

In Jenkins Pipeline we have a CpsGroovyScript executed by CPSGroovyShell with a custom classpath which is appended with path to Global Library location.

In order to be able to test my changes before making them available for all the Pipelines on my Jenkins instance I want to override classpath by unloading the classes and modifying the script's classpath from itself in runtime.

My idea is to prepend current classpath with a new location so that changed classes found first. this.class.classLoader.addURL() is great but the new path goes to the end of classpath.

.../src - this is root of current production code, it's in classpath by default .../branch-src - this is root of my changed code, I want it to be used instead of .../src

For instance I want class .../src/com/foo/Bar.groovy to be overridden by .../branch-src/com/foo/Bar.groovy when I call import com.foo.Bar or new com.foo.Bar()

If this can't work at all is there any other hack, i.e. to completely replace original classpath, or to replace the whole classloader, whatever.

0

There are 0 answers