我正在使用 shadowJar
com.github.johnrengelman.shadow 提供的 Gradle 目标用于构建应用程序的 Gradle 插件,需要 org.apache.hive.jdbc.HiveDriver
使用 Impala 连接到 Kudu。
问题是当我使用标准方法在 Scala 中导入驱动程序时:
Class.forName("org.apache.hive.jdbc.HiveDriver")
java.lang.ClassNotFoundException: org.apache.hadoop.hive.jdbc.HiveDriver
. 我的
build.gradle
包含:dependencies {
implementation {
"org.apache.hive:hive-jdbc:1.2.1"
}
}
如何指示影子插件不要删除通过字符串注入(inject)的所需依赖项?
最佳答案
我发现解决方案是不通过 Class.forName
包含静态类型,但通过导入来使用代码:
import java.sql.DriverManager
import org.apache.hive.jdbc.{HiveConnection, HiveDriver}
class Foo {
// Register Hive Driver this way to prevent shadowing to cut it off
new HiveDriver()
DriverManager.getConnection(connectionUrl, user, password) match {
case connection: HiveConnection =>
...
}
}
这样,影子插件就会被正式告知实际上需要驱动程序这一事实。
关于Gradle shadowJar 删除所需的 SQL 驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64562468/