Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-6068

Classloader for actual System Groovy script and plugin "Execute system Groovy script" is not the same

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Major Major
    • groovy-plugin
    • None
    • Hudson 1.352
      Groovy plugin 1.4

      The following code works if you execute it on the Hudson script page (hudsonserver/script), but not if you execute it as a "Execute system Groovy script" build step:

      import hudson.scm.SubversionSCM
      println(SubversionSCM.class)

      Running under my hudson, it produces a ClassNotFoundException when run as a "Execute system Groovy script" build step. SubversionSCM is a class that's supplied by the Subversion plugin.

      It turns out that the classloader handed to the Groovy script from the script page is not the same as that which it gets from the build step. The script page appears to use PluginManager.uberClassLoader, which knows where all of the plugin classes are. The Groovy plugin build step appears to use a different classloader, that only knows about the core libraries.

      Right now, to use any classes delivered by plugins, you have to do the following (in this case for SubversionSCM):
      Class.forName("hudson.scm.SubversionSCM", true, hudson.model.Hudson.instance.getPluginManager().uberClassLoader)
      You can then use the class, but it requires all kinds of ugly reflection code and is generally a pain compared to the normal way.

      I think it would be better if the build step offered the same classpath as the script page, so that anything that you develop on the script page can be used in your build without modification.

      I haven't dug into the code, but I'm assuming the fix for this involves handing PluginManager.uberClassLoader to the "Execute system Groovy script" build step.

            vjuranek vjuranek
            lotus118 lotus118
            Votes:
            3 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: