[英]Shaded/Repackaged jar as a dependency
我們有一種情況,我們需要一個應用程序能夠連接到兩個版本的kafka(0.7.2和0.10.0+)並充當路由器。 我試圖省略在這里使用兩個運行時因為我們需要快速愚蠢,所以想要在運行時之間發送數據時防止額外的序列化/反序列化。
為此,我嘗試將舊的kafka驅動程序從包kafka重新打包到old.kafka,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>kafka-router</artifactId>
<groupId>org.deer</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>old-kafka</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<kafka.version>0.7.2</kafka.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>unpack</id>
<phase>compile</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.9.2</artifactId>
<version>${kafka.version}</version>
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<includes>**/*.class,**/*.xml</includes>
</artifactItem>
</artifactItems>
<includes>**/*.java</includes>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<relocations>
<relocation>
<pattern>kafka.</pattern>
<shadedPattern>old.kafka.</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
我正在使用依賴插件將kafka類解包到target / classes和shade插件來重新打包它們。 原因是最終的jar應該就像是一個kafka驅動程序jar(它沒有其他傳遞依賴,因此它不會導致使用kafka而不是old.kafka的一些不匹配。但這不是真正的重點在這里,只是試圖阻止題外話題。
這里的主要問題是,當我查看已安裝到.m2的jar時,它看起來是正確的(具有old.kafka包):
但是當我嘗試使用這個jar作為依賴時,就像這樣......
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>kafka-router</artifactId>
<groupId>org.deer</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>router-app</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.deer</groupId>
<artifactId>old-kafka</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
...並在類似的類中引用它...
package org.deer.test;
import old.kafka.producer.ProducerData;
public class TwoKafkaDriversExample {
public static void main(String[] args) {
new ProducerData();
}
}
......它自己進口不起作用。 我懷疑陰影罐子里缺少與maven相關的東西,但沒有注意到任何東西。 另一種可能的方式是,shade插件或asm不喜歡scala類生成的字節碼。
好的,所以我已經能夠解決這個問題了。 導入錯誤是intelij的問題,由於某種原因它沒有看到重新打包的類。 但是maven確實如此,使用正確的構造函數並添加了scala-lang依賴(它抱怨缺少Seq類)我能夠構建它。
在github上傳的完整示例 - https://github.com/Marssmart/kafka-router
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.