Shaded/Repackaged jar 作为依赖

Shaded/Repackaged jar as a dependency

我们遇到这样一种情况,我们需要一个应用程序能够连接到两个版本的 kafka(0.7.2 和 0.10.0+)并充当路由器。我试图在这里省略使用两个运行时,因为我们需要它很快变得愚蠢,所以想要在运行时之间发送数据时防止额外的 serialization/deserialization。

为此,我尝试将旧的 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 classes 解压到 target/classes 并使用 shade 插件重新打包它们。这样做的原因是最终的 jar 应该像 kafka 驱动程序 jar 一样(它没有其他传递依赖性,因此它不会导致使用 kafka[=47 的一些不匹配=] 而不是 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>

... 并像这样在 class 中引用它 ...

package org.deer.test;

import old.kafka.producer.ProducerData;

public class TwoKafkaDriversExample {

    public static void main(String[] args) {
        new ProducerData();
    }
}

...它自己的导入不起作用。我怀疑阴影罐子缺少与 maven 相关的东西,但没有注意到任何东西。另一种可能是 shade 插件或 asm 不喜欢 scala classes 生成的字节码。

好的,所以我已经弄明白了。导入错误是 intelij 的问题,由于某种原因它没有看到重新打包的 classes。但是 maven 确实如此,通过使用正确的构造函数并添加 scala-lang 依赖项(它抱怨缺少 Seq class)我能够构建它。

完整示例已上传至 github - https://github.com/Marssmart/kafka-router