Jenkins选择忽略/ etc / environment或/etc/profile.d/jdk.sh中的系统CLASSPATH参数。我尝试在Scriptler中运行Groovy脚本,但是我收到了这个错误:
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
我已经在Jenkins中安装了MySQL数据库插件(https://wiki.jenkins-ci.org/display/JENKINS/MySQL+Database+Plugin)但是仍然无法通过在Jenkins控制台和Scriptler中找到的MySQL驱动程序加载:
import groovy.sql.Sql
Sql.newInstance("jdbc:mysql://HOST:3306/DB", "USER", "PASS", "com.mysql.jdbc.Driver")
在Jenkis" Global properties"下设置CLASSPATH之后> "环境变量"到
Name CLASSPATH
Value .:/var/lib/jenkins/plugins/database-mysql/WEB-INF/lib/mysql-connector-java-5.1.21.jar
Jenkins能够运行Jobs,但Jenkins Script Console / Scriptler仍无法找到jar文件。
答案 0 :(得分:11)
最后我想出了如何在Scriptler中使用MySQL JDBC驱动程序:
println System.getProperty("java.ext.dirs")
/ usr / lib中/ JVM / JAVA -7-的openjdk-AMD64 / JRE / lib / ext目录:在/ usr / JAVA /包/ lib / ext目录
cp mysql-connector-java-*.jar /usr/java/packages/lib/ext/
Jenkins jobs和Scriptler / Groovy脚本现在可以在没有CLASSPATH之类的附加参数的情况下工作。
答案 1 :(得分:0)
修改Jenkins classLoader
也应该有效:
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