简体   繁体   English

jenkins scriptler的groovy脚本不起作用。 类路径问题

[英]groovy script from jenkins scriptler not working. classpath issue

Trying to connect to mysql db using groovy script in jenkins via scriptler plugin. 尝试通过脚本编辑器插件在jenkins中使用groovy脚本连接到mysql db。 Using jdbc mysql connector for the same. 使用jdbc与mysql连接器相同。

mysql connector jar file is present in classpath of machine where jenkins is running. 运行jenkins的计算机的类路径中存在mysql连接器jar文件。 running only 1 machine for jenkins. 詹金斯只运行一台机器。

below is the code snippet: 下面是代码片段:

import java.sql.*
import groovy.sql.Sql
DriverManager.registerDriver(new com.mysql.jdbc.Driver())

try{
    def dbURL = 'jdbc:mysql://x.x.x.x:3306/Employee_DB'
    def dbUserName = 'root'
    def dbPassword = 'root'
    def dbDriver = 'com.mysql.jdbc.Driver'
    def db = Sql.newInstance(dbURL,dbUserName,dbPassword,dbDriver)
    List authors = db.rows('SELECT name FROM Employee')
    print authors

}catch(Exception e){
    print "exception"
}finally{
    //db.close()
}

But getting exception saying: 但得到异常说:

Script1.groovy: 5: unable to resolve class com.mysql.jdbc.Driver 
@ line 5, column 30.
DriverManager.registerDriver(new com.mysql.jdbc.Driver())

Same piece of code if run like 如果像这样运行,则相同的代码

groovy 1.groovy

Execution is successful. 执行成功。

But jenkins scriptler is unable to detect this jar file. 但是jenkins脚本编辑器无法检测到此jar文件。

in manager jenkins -> system information, i could see system environment variables set as well. 在经理詹金斯->系统信息中,我也可以看到设置了系统环境变量。

Need help. 需要帮忙。

there is a bug from the Jenkins that it does not read the environment variables nor the Jenkins defined variables it takes into account the java native path and it is /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.65-3.b17.el7.x86_64/jre/lib/ext Jenkins的一个错误是它不读取环境变量也不是Jenkins定义的变量,它没有考虑到Java本机路径,而是/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.65- 3.b17.el7.x86_64 / JRE / lib / ext目录

so add the Jdbc jar in the above path and restart the jenkins 因此,在上述路径中添加Jdbc jar并重新启动jenkins

hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM