Eliminate all transitive dependencies with Apache Maven Shade Plugin

I used the Apache Maven Shade Plugin to create a thick jar (a jar that includes all classes from all dependencies). My current project folder structure looks something like this:

> 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

Snapshot pom.xmlfrom the parent module,

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

Capture pom.xmlfrom modules-1 and module-2 is as follows:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <executions>
        <execution>
            <id>common-shade</id>
            <phase>package</phase>
        </execution>
    </executions>
</plugin>

The dependency declared for module-1 in module-2 looks like this:

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

NOTE. Currently, if I see my dependencies using mvn dependency:tree -Dverbose, then it displays as expected, but shade-plugindoes not exclude it.

: -1 -2 . / -, -1, -2 , , , -2, Module-1, -2 .

: , , java -cp module.jar com.mycom.Main

?

+4

Source: https://habr.com/ru/post/1674981/


All Articles