4

I am trying to run below piece of groovy script with ExtendedChoiceParameter plugin but i see an empty list.

@GrabConfig(systemClassLoader = true)
@Grab(group = 'org.postgresql', module = 'postgresql', version = '9.4-1205-jdbc42')
import groovy.sql.Sql

def url = 'jdbc:postgresql://localhost:5432/mydb'
def user = 'postgres'
def password = 'postgres'
def driver = 'org.postgresql.Driver'
def sql = Sql.newInstance(url, user, password, driver)
try {
    def output = []
    sql.eachRow("select env from customers_customer") { row ->
        output.push(row[0])
    }

   return output.sort()
} finally {
    sql.close()
}

Above code throws below exception (as checked in jenkins.log file)

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
General error during conversion: No suitable ClassLoader found for grab

java.lang.RuntimeException: No suitable ClassLoader found for grab

When i remove the @Grab It throw below error ( which is usual as there is not postgres jar in classpath)

2021-06-24 08:18:18.194+0000 [id=170]   SEVERE  c.c.h.p.e.ExtendedChoiceParameterDefinition#executeGroovyScriptAndProcessGroovyValue: org.postgresql.Driver
java.lang.ClassNotFoundException: org.postgresql.Driver

I have no idea how to add the jar to classpath or how to make @Grab work with the plugin. I have also tried the same script with a freestyle job (where i can select a groovy version but there is no option with the plugin) and it works fine.

Any idea?

Update 1: As a workaround, I have created a wrapper java program to run given query(as argument) and created jar from that. I am running the jar as command from grooy script and that works. Here is the groovy script -


def command = ["java", "-jar", "/mnt/d/repos/java-postgres-cli/target/java-postgres-cli-0.1-jar-with-dependencies.jar",
               "localhost", "5432", "postgres", "postgres", "thedb", "select * from thetable"]
def sout = new StringBuilder()
def serr = new StringBuilder()
def proc = command.execute()
proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(1000)
if (serr.length() != 0) {
    return serr
}
return sout
Krishnom
  • 1,348
  • 12
  • 39

1 Answers1

0

For adding jar in classpath you can copy jar file into jdk which used in running jenkins.

path to place jar - {JDK_PATH}/jre/lib/ext/

mayurssoni
  • 66
  • 5