Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No suitable ClassLoader found for grab in Jenkins pipeline script

I'm running into a problem while writing a Jenkins pipeline script.

This is the code in my Jenkins pipeline script:

@Grab(group='org.postgresql', module='postgresql', version='42.1.4')
import groovy.sql.Sql;
import java.util.ServiceLoader;
import java.sql.Driver;

ServiceLoader<Driver> loader = ServiceLoader.load(Driver.class);
print("Go Checkout")
def dbUrl = "jdbc:postgresql://10.10.100.86:5432/qa"
def dbUser = "myDB"
def dbPassword = "myDB"
def dbDriver = "org.postgresql.Driver"   
def sql = Sql.newInstance(dbUrl, dbUser, dbPassword, dbDriver)

I have also added this code:

@GrabConfig(systemClassLoader=true)

But it doesn't work - I receive this error message:

Jenkins version : 2.73.3

Obtained Jenkinsfile.groovy from git git@localhost:myg/myproject.git
java.lang.RuntimeException: No suitable ClassLoader found for grab
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:83)
    at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:105)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:247)
    at groovy.grape.GrapeIvy.chooseClassLoader(GrapeIvy.groovy:182)
    at groovy.grape.GrapeIvy$chooseClassLoader.callCurrent(Unknown Source)
    at groovy.grape.GrapeIvy.grab(GrapeIvy.groovy:249)
    at groovy.grape.Grape.grab(Grape.java:167)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
    at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.invoke(StaticMetaMethodSite.java:46)
    at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.callStatic(StaticMetaMethodSite.java:102)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:56)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:194)
    at org.kohsuke.groovy.sandbox.impl.Checker$2.call(Checker.java:188)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedStaticCall(Checker.java:190)
    at org.kohsuke.groovy.sandbox.impl.Checker$checkedStaticCall.callStatic(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:56)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:194)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:222)
    at WorkflowScript.<clinit>(WorkflowScript)
Caused: java.lang.ExceptionInInitializerError
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at java.lang.Class.newInstance(Class.java:442)
    at org.codehaus.groovy.runtime.InvokerHelper.createScript(InvokerHelper.java:434)
    at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:129)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:123)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:517)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:480)
    at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:269)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:421)
Finished: FAILURE
like image 833
안성민 Avatar asked Dec 13 '17 04:12

안성민


1 Answers

The following code uses the system classloader and will load the Postgres driver and attempt to connect to it:

@Grapes([
 @Grab(group='org.postgresql', module='postgresql', version='42.1.4'),
 @GrabConfig(systemClassLoader = true)    // magic here
])
import groovy.sql.Sql;
import java.util.ServiceLoader;
import java.sql.Driver;
import java.sql.DriverManager;

ServiceLoader<Driver> loader = ServiceLoader.load(Driver.class);
print("Go Checkout")
def dbUrl = "jdbc:postgresql://10.10.100.86:5432/qa"
def dbUser = "myDB"
def dbPassword = "myDB"
def dbDriver = "org.postgresql.Driver"   
def sql = Sql.newInstance(dbUrl, dbUser, dbPassword, dbDriver)

Running this results in:

Caused by: java.net.SocketTimeoutException: connect timed out

Without the systemClassLoader property the driver won't be found.

like image 73
Erik Pragt Avatar answered Nov 19 '22 01:11

Erik Pragt