【问题标题】:Replace a transitive dependency building maven fat jar替换传递依赖构建maven fat jar
【发布时间】:2019-08-03 06:43:01
【问题描述】:

在创建我的胖 jar 时,我正在尝试用最新的传递依赖项替换一个传递依赖项。但是每次旧的依赖项都包含在 jar 中。我已经尝试过组装插件和阴影插件。这是我的 pom 中的 sn-p-

<dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-streaming-kafka-0-10_2.11</artifactId>
        <version>2.3.1</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <groupId>org.apache.kafka</groupId>
                <artifactId>kafka_2.11</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka_2.11</artifactId>
        <version>0.10.2.0</version>
    </dependency>

阴影插件sn-p-

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
                <artifactSet>
                    <excludes>
                        <exclude>org.apache.kafka:kafka-clients:*</exclude>
                    </excludes>
                </artifactSet>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

任何帮助将不胜感激。

【问题讨论】:

  • 您是否尝试过dependency:tree 命令查看谁链接到了错误的版本?这是追踪这些事情的最简单方法。然后你可以exclude传递依赖。
  • 是的,我做到了! Spark-streaming-kafka-0-10_2.11 正在添加该依赖项,但即使在从该依赖项中排除传递依赖项之后,传递依赖项仍会被打包到胖 jar 中。我已经展示了我是如何做到的。
  • 请将dependency:tree 添加到您的问题中。

标签: java maven maven-assembly-plugin maven-shade-plugin


【解决方案1】:

尝试重新排序依赖项。

将依赖项与您想要的版本保持在通过传递依赖项包含的依赖项之上。

多做几次mvn dependency:tree 就可以了。

例子:

<dependency>
    <groupId>org.apache.kafka</groupId>
    <artifactId>kafka_2.11</artifactId>
    <version>0.10.2.0</version>
</dependency>
<dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-streaming-kafka-0-10_2.11</artifactId>
    <version>2.3.1</version>
    <scope>compile</scope>
    <!-- <exclusions>
    <exclusion>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka_2.11</artifactId>
        </exclusion>
    </exclusions> -->
</dependency>

参考:

1. https://stackoverflow.com/questions/31740785/why-order-of-maven-dependencies-matter 2. https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Transitive_Dependencies

【讨论】:

    猜你喜欢
    • 2010-11-08
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    • 1970-01-01
    • 2019-04-02
    • 1970-01-01
    • 2018-01-20
    • 1970-01-01
    相关资源
    最近更新 更多