这是Jenkins共享库中的新类.
共享库通过Manage Jenkins> Configure System下的标准方法加载
package com.mycorp.core;
@Grab(group='com.microsoft.sqlserver', module='mssql-jdbc', version='6.4.0.jre8')
import com.microsoft.sqlserver.jdbc.SQLServerDriver
import groovy.sql.Sql
class MySQLClass implements Serializable {
def dbconnection
def dbURL
def dbUsername
def dbPassword
def dbDriver
MySQLClass(databaseConfig) {
//Set any instance variables required.
this.dbURL = databaseConfig.sql.url
this.dbUsername = databaseConfig.sql.username
this.dbPassword = databaseConfig.sql.password
this.dbDriver = databaseConfig.sql.driver
}
def getConnection() {
return Sql.newInstance(dbURL,dbUsername,dbPassword,dbDriver)
}
}
databaseConfig对象配置为:
def databaseConfig = [
url: 'jdbc:sqlserver://myhost:1433;databaseName=mydatabase',
user: 'user',
password: 'password',
driver: 'com.microsoft.sqlserver.jdbc.SQLServerDriver'
]
当调用MyClass.getConnection()时,它失败并:
java.sql.SQLException: No suitable driver found for jdbc:sqlserver://myhost:1433;databaseName=mydatabase at java.sql.DriverManager.getConnection(DriverManager.java:689) at java.sql.DriverManager.getConnection(DriverManager.java:247)
不确定为什么'@Grab'和'import'没有提供驱动程序.
我试过添加:
@GrabConfig(systemClassLoader = true)
正如其他地方的建议但这会导致类失败编译 - Jenkins开始将其视为脚本而不是类,因此会抛出重复的类名错误.
任何想法Stackoverflow专家?