shaded/将jar重新打包为依赖项

x759pob2  于 2021-06-06  发布在  Kafka
关注(0)|答案(1)|浏览(454)

我们需要一个应用程序连接到两个版本的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>

我使用dependency plugin将kafka类解包到target/classes,并使用shade plugin重新打包它们。这样做的原因是,最终的jar应该像kafka驱动程序jar一样工作(它没有其他可传递的依赖项,因此使用kafka而不是old.kafka不会导致某些不匹配)。但这并不是真正的重点,只是试图阻止离题的问题。
这里的主要问题是,当我查看已安装到.m2的jar时,它看起来是正确的(有旧的.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();
    }
}

... 导入本身不起作用。我怀疑这个有阴影的jar遗漏了一些与maven相关的东西,但是没有注意到任何东西。另一个可能的原因是shade插件或asm不喜欢scala类生成的字节码。

bxfogqkk

bxfogqkk1#

好吧,所以我已经想清楚了。导入错误是intelij的一个问题,由于某些原因,它看不到重新打包的类。但是maven做到了,通过使用正确的构造函数并添加scala语言依赖项(它抱怨缺少seq类),我能够构建这个。



在github上上载的完整示例-https://github.com/marssmart/kafka-router

相关问题