gradleshadowjar删除所需的sql驱动程序

vyu0f0g1  于 2021-06-24  发布在  Hive
关注(0)|答案(1)|浏览(297)

我用的是 shadowJar gradle目标由com.github.johnrengelman.shadow gradle插件提供,用于构建需要 org.apache.hive.jdbc.HiveDriver 使用 Impala 连接到Kudu。
问题是,当我使用标准方法在scala中导入驱动程序时:

Class.forName("org.apache.hive.jdbc.HiveDriver")

shadow插件将其从结果jar中删除,这意味着运行时错误为: java.lang.ClassNotFoundException: org.apache.hadoop.hive.jdbc.HiveDriver .
我的 build.gradle 包含:

dependencies {
  implementation {
    "org.apache.hive:hive-jdbc:1.2.1"
  }
}

如何指示shadow插件不删除通过字符串注入的所需依赖项?

0h4hbjxa

0h4hbjxa1#

我发现解决方案是包含静态类型而不是via 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 =>
       ...
  }
}

通过这种方式,shadow插件被正式告知驱动程序实际上是必需的。

相关问题