工作中的maven-shade-plugin插件高级用法小记
问题背景
flink-table 库中使用了org.apache.calcite 1.12版本,我在开发flink-cep的过程中引入了org.apache-calcite 1.6版本,服务端要同时引入这两个版本的包,如何解决这个版本冲突问题呢?
maven-shade-plugin
使用maven-shade-plugin的relocate功能可以将包名打包成一个别名
1 | <plugin> |
看起来一切都ok了,不过思考一个问题,maven-shade-plugin插件修改包名后项目中引用的包名怎么找到的呢?Class.forName(“xxxx.xxx.xxx”)去生成类的方式还奏不奏效呢?Stack Overflow上关于shade插件的描述也提到了对这种类加载的方式是否奏效的疑问
然而当应用一跑起来之后发现,并不奏效!!!报出异常 No suitable driver found for jdbc:calcite 也就是DriverManager生成相应的driver的时候没找到相应的类。
相关的issue
https://github.com/xerial/sqlite-jdbc/issues/145
在里面我们看到sqlite数据库有这样的配置文件指定了相应driver的类名
https://github.com/xerial/sqlite-jdbc/blob/master/src/main/resources/java.sql.Driver
查看calcite也有相应的配置文件指定
那是不是在shade的过程中将这个文件内容也进行相应的变更就可以了呢? shade也确实有这样的transformation
JAR files providing implementations of some interfaces often ship with a META-INF/services/ directory that maps interfaces to their implementation classes for lookup by the service locator. To relocate the class names of these implementation classes, and to merge multiple implementations of the same interface into one service entry, the ServicesResourceTransformer can be used
最后一个问题: maven-shade-plugin 要在3.0以上才能生效
效果: