【发布时间】:2019-08-04 14:37:59
【问题描述】:
我们有一种情况,我们需要一个应用程序能够连接到两个版本的 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 类解包到目标/类和阴影插件以重新打包它们。这样做的原因是最终的 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 类生成的字节码。
【问题讨论】:
-
“不工作”是什么意思?您能否将命令行中的错误消息添加到您的问题中?
-
您使用的是什么 IDE,您的类路径是什么样的?
-
当您在新项目的命令行中运行
mvn install时会发生什么情况,可能是您的IDE 不理解翻译后的类 -
你试过没有 -SNAPSHOT 吗?
-
当你只有 143 声望时,你如何拥有 300 声望的赏金@ján Srincek
标签: java maven apache-kafka maven-shade-plugin