简体   繁体   中英

Replace a transitive dependency building maven fat jar

I am trying to replace a transitive dependency with a latest one while creating my fat jar. But every time the old dependency gets included in the jar. I have tried both assembly plugin as well as shade plugin. Here is snippet from my pom-

<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>

Shade plugin snippet-

<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>

Any help will be appreciated.

Try reordering the dependency.

Keep the dependency with the version you want on top of the dependency from where it is getting included through transitive dependency.

Do mvn dependency:tree a few times to get it right.

Example:

<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>

Ref:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM