使用Apache Maven Shade Plugin排除所有传递依赖项

Vis*_*kia 7 dependency-management maven-plugin maven transitive-dependency maven-shade-plugin

我使用了Apache Maven Shade Plugin来创建胖jar(包含所有依赖项的所有类的jar).我当前的项目文件夹结构看起来像这样,

> Parent Module (packaging pom)

    > Module 1 (packaging jar)
        > Own classes
        > Dependency-1
        > Dependency-2

    > Module 2 (packaging jar)
        > Own classes
        > Module 1 (I want here only classes written in module-1, not any transitive dependencies)
        > Dependency-1
        > Dependency-2
        > Dependency-3
Run Code Online (Sandbox Code Playgroud)

pom.xml来自父模块的快照,

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <executions>
            <execution>
                <id>common-shade</id>
                <goals>
                    <goal>shade</goal>
                </goals>
                <configuration>
                    <transformers>
                        <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                            <resource>META-INF/spring.handlers</resource>
                        </transformer>
                        <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                            <resource>META-INF/spring.schemas</resource>
                        </transformer>
                    </transformers>
                </configuration>
            </execution>
        </executions>
    </plugin>
Run Code Online (Sandbox Code Playgroud)

pom.xmlModule-1和Module-2 的快照看起来像这样,

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <executions>
        <execution>
            <id>common-shade</id>
            <phase>package</phase>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

在Module-2 pom中为Module-1声明的依赖关系看起来像这样,

<dependency>
    <groupId>my.com.groupId</groupId>
    <artifactId>my.artifactId</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <exclusions>
        <exclusion>
            <groupId>*</groupId>
            <artifactId>*</artifactId>
         </exclusion>
    </exclusions>
</dependency>
Run Code Online (Sandbox Code Playgroud)

注意:目前,如果我看到我的依赖项使用mvn dependency:tree -Dverbose,那么它将按预期显示,但shade-plugin不排除它.

问题:module-1和module-2具有不同版本的相同依赖关系.如果我跳过/禁用Module-1中声明的shade-plugin,那么我可以运行Module-2而没有任何问题,但如果我不禁用它,那么Module-2创建的fat jar包含一些来自jar的jar类-1版本,所以我的Module-2停止运行.

更新: 我使用胖罐来运行这样的单个模块, java -cp module.jar com.mycom.Main

有什么帮助吗?