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

Cannot get Plugin to honor Java Jar added via "Additional groovy classpath"

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Major Major
    • Windows 7, Jenkins 1.523, Groovy Postbuild: 1.8, SQL Server 2008, JDBC 4.0

      Jenkins: 1:523, PostBuild: 1.8

      I have a Groovy script . . . that will eventually be a post build script . . . for now the script runs on its own and connects to a Database, but with this CLASSPATH set:

      SET CLASSPATH=C:\Users\fmerrow\Downloads\Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\enu\sqljdbc4.jar

      However, when I try to get the working script to run as a post build . . . I cannot find the class in that file.

      I've tried to specify as Directory using "Additional groovy classpath" and when that didn't work as full path to the Jar file. I also wondered if spaces would be an issue . . . Quoting gave odd results (pretended C:\Program Files(x85)\Jenkins to the path) so I moved it into a directory with no spaces (again tried both file and directory) . . . I cannot seem to get it to find the class in this jar file.

      Works fine "in the clear" with CLASSPATH set, but I cannot get it to work inside the Jenkins JVM.

      Frank

          [JENKINS-18989] Cannot get Plugin to honor Java Jar added via "Additional groovy classpath"

          Frank Merrow created issue -

          Brian Brooks added a comment - - edited

          I'm having a similar struggle. I tried to add my dependency JAR files via the "Additional groovy classpath" mechanism but its not working. I'm stuck doing horrible things like

          mypostbuild.groovy
          this.class.classLoader.rootLoader.addURL(new URL("file:///C:\\dependencies\\xws-security.jar"))
          def cls15 = Class.forName("com.sun.xml.wss.impl.callback.PasswordCallback").newInstance()
          

          Other unsuccessful attempts include

          The code involved with handling the "Additional groovy classpath" seems like it should support adding dependency JARs... https://github.com/jenkinsci/groovy-postbuild-plugin/blob/master/src/main/java/org/jvnet/hudson/plugins/groovypostbuild/GroovyPostbuildRecorder.java

          @Frank Please let me know if you find a better workaround.

          Brian Brooks added a comment - - edited I'm having a similar struggle. I tried to add my dependency JAR files via the "Additional groovy classpath" mechanism but its not working. I'm stuck doing horrible things like mypostbuild.groovy this . class. classLoader.rootLoader.addURL( new URL( "file: ///C:\\dependencies\\xws-security.jar" )) def cls15 = Class .forName( "com.sun.xml.wss.impl.callback.PasswordCallback" ).newInstance() Other unsuccessful attempts include Putting the files in ${WORKSPACE}/.groovy/lib ( http://groovy.codehaus.org/Running ) Launching using GroovyShell with a custom CompilerConfiguration but then I lost the "manager" property that the Groovy Postbuild plugin binds. ( http://stackoverflow.com/questions/8721971/how-to-create-a-groovyshell-object-with-context-apps-classpath/10588798#10588798 ) The code involved with handling the "Additional groovy classpath" seems like it should support adding dependency JARs... https://github.com/jenkinsci/groovy-postbuild-plugin/blob/master/src/main/java/org/jvnet/hudson/plugins/groovypostbuild/GroovyPostbuildRecorder.java @Frank Please let me know if you find a better workaround.

          Frank Merrow added a comment -

          No work around at all . . . I'll have to take a look at yours - THANK YOU.

          My goal is to make ODBC work from a Java Post Build Plugin . . . so far, no luck . . . I'll try your solution soon and report.

          But it will be a few days . . . I got side tracked by other high priority work.

          Frank Merrow added a comment - No work around at all . . . I'll have to take a look at yours - THANK YOU. My goal is to make ODBC work from a Java Post Build Plugin . . . so far, no luck . . . I'll try your solution soon and report. But it will be a few days . . . I got side tracked by other high priority work.

          Frank Merrow added a comment -

          When I actually got around to trying this, I could not make it work.

          While the code worked fine when run as a Groovy script on its own, once it was inside the Jenkins JVM, for some reason "rootLoader" was null. I'm not enough of a Groovy Guru to understand the difference between the two loaders, but I am still stumped and have no workaround.

          Frank Merrow added a comment - When I actually got around to trying this, I could not make it work. While the code worked fine when run as a Groovy script on its own, once it was inside the Jenkins JVM, for some reason "rootLoader" was null. I'm not enough of a Groovy Guru to understand the difference between the two loaders, but I am still stumped and have no workaround.

          Frank Merrow added a comment -

          Cool . . . just made it to my SQL server from inside the plugin with the following change instead of "this":

          manager.hudson.class.classLoader.addURL(new URL("file:///" + filePath))

          Frank Merrow added a comment - Cool . . . just made it to my SQL server from inside the plugin with the following change instead of "this": manager.hudson.class.classLoader.addURL(new URL("file:///" + filePath))

          Viktor Berke added a comment -

          Frank, how did that work? For me that says:

          groovy.lang.MissingPropertyException: No such property: manager for class: Script1

          Viktor Berke added a comment - Frank, how did that work? For me that says: groovy.lang.MissingPropertyException: No such property: manager for class: Script1
          Jesse Glick made changes -
          Labels Original: classpath, groovy plugin, New: classpath groovy

          Torben Knerr added a comment -

          fmerrow, bviktor would be higly interested in that, too

          Torben Knerr added a comment - fmerrow , bviktor would be higly interested in that, too

          joel Young added a comment -

          I've been having the same problem. I'm using v2.2 of the plugin. I've tried using the "Additional Classpath" fields, gone into the Jenkins file in /etc/sysconfig and added a "-cp" options to the JENKINS_JAVA_OPTIONS setting, and tried using the classload in the form: manager.hudson.class.classLoader.addURL(new URL("file:///" + filePath))

          None of it seemed to work. In the end I had to create a "Trigger parameterized build on other project" then specify my script in a Build step. The classpath variables appeared to work there.

          joel Young added a comment - I've been having the same problem. I'm using v2.2 of the plugin. I've tried using the "Additional Classpath" fields, gone into the Jenkins file in /etc/sysconfig and added a "-cp" options to the JENKINS_JAVA_OPTIONS setting, and tried using the classload in the form: manager.hudson.class.classLoader.addURL(new URL("file:///" + filePath)) None of it seemed to work. In the end I had to create a "Trigger parameterized build on other project" then specify my script in a Build step. The classpath variables appeared to work there.

          Zach Auclair added a comment - - edited

          I was able to get the following to work

          in my postbuild task

          this.class.classLoader.parseClass("/home/jenkins/GitlabPostbuildReporter.groovy")
          GitlabPostbuildReporter.newInstance(manager).report()
          

          in my file on disk at /home/jenkins/GitlabPostbuildReporter.groovy

          class GitlabPostbuildReporter {
            def manager
            public GitlabPostbuildReporter(manager){
              if(manager == null) {
                throw new RuntimeException("Manager object musn't be null")
              }
              this.manager = manager
            }
            public def report() {
              // do work with manager object
            }
          }
          

          Zach Auclair added a comment - - edited I was able to get the following to work in my postbuild task this . class. classLoader.parseClass( "/home/jenkins/GitlabPostbuildReporter.groovy" ) GitlabPostbuildReporter.newInstance(manager).report() in my file on disk at /home/jenkins/GitlabPostbuildReporter.groovy class GitlabPostbuildReporter { def manager public GitlabPostbuildReporter(manager){ if (manager == null ) { throw new RuntimeException( "Manager object musn't be null " ) } this .manager = manager } public def report() { // do work with manager object } }

            wolfs Stefan Wolf
            fmerrow Frank Merrow
            Votes:
            3 Vote for this issue
            Watchers:
            10 Start watching this issue

              Created:
              Updated: