1

Jenkins seams to ignore the system CLASSPATH parameter from /etc/environment or /etc/profile.d/jdk.sh. I try to run a Groovy script in Scriptler but i get this error:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321

I have installed the MySQL Database Plugin in Jenkins already (https://wiki.jenkins-ci.org/display/JENKINS/MySQL+Database+Plugin) but still the MySQL driver can not by loaded found in Jenkins Console nor Scriptler:

import groovy.sql.Sql
Sql.newInstance("jdbc:mysql://HOST:3306/DB", "USER", "PASS", "com.mysql.jdbc.Driver")

After i set the CLASSPATH under Jenkis "Global properties" > "Environment variables" to

Name CLASSPATH
Value .:/var/lib/jenkins/plugins/database-mysql/WEB-INF/lib/mysql-connector-java-5.1.21.jar

Jenkins is able to run Jobs but still Jenkins Script Console / Scriptler can not find the jar file.

panticz
  • 2,135
  • 25
  • 16

2 Answers2

12

Finaly i figured out how to use MySQL JDBC driver with Scriptler:

  1. Find out the default JAVA classpath dirs (run in Jenkins Script Console):

println System.getProperty("java.ext.dirs")

/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/ext:/usr/java/packages/lib/ext

  1. Download an add mysql-connector-java-*.jar to default Java classpath:

cp mysql-connector-java-*.jar /usr/java/packages/lib/ext/

  1. Restart Jenkins

Jenkins jobs and Scriptler / Groovy scripts should now work without any additional parameter like CLASSPATH.

panticz
  • 2,135
  • 25
  • 16
0

Modifying the Jenkins classLoader should also work:

this.class.classLoader.addURL(new URL("file://${System.getProperty('user.home')}/plugins/database-mysql/WEB-INF/lib/mysql-connector-java-5.1.21.jar"))
import groovy.sql.Sql

someapp = '[A-Z]+SOME_APP[0-9]*'
someenv = 'Production'
somestatus = '1'

def sql = Sql.newInstance("jdbc:mysql://foo.com/somedb", "user", "pass", "com.mysql.jdbc.Driver")
query = "SELECT somefield FROM sometable WHERE somefield REGEXP '$somevar' AND environment='$someenv' AND status='$somestatus'"
def result = sql.rows(query).collect{ it.name }.sort{ it.find(/\d+$/) as int }
sql.close()
return result
chown
  • 51,908
  • 16
  • 134
  • 170