Exclude transitive maven dependencies from generated war file

193 Views Asked by At

I am in the process of converting a monolithic legacy enterprise application to use maven instead of ant. I created a list of dependencies that ant was using for the builds and accordingly created a maven pom file to list those dependencies in.

The problem I'm currently facing is to try to include only the direct dependencies listed in the pom file, so that when an artifact is generated (war file in my case) only the direct dependencies are included in the WEB-INF/lib folder of the packaged war file for the maven project.

Example dependency used:

    <!-- https://mvnrepository.com/artifact/org.apache.xmlgraphics/batik-all -->
    <dependency>     
        <groupId>org.apache.xmlgraphics</groupId>     
        <artifactId>batik-all</artifactId>     
        <version>1.16</version>     
        <type>pom</type> 
    </dependency>

Executing: mvn clean -U install

Results in downloading and packaging approximately 30 transitive dependencies just for batik-all-1.16.jar (The transitive dependencies for batik-all can be seen listed here batik-all-1.16.pom)

After doing a lot of research, I came across this as a possible solution but it made no difference (didn't resolve my problem - still included all the transitive jars).

    <!-- https://mvnrepository.com/artifact/org.apache.xmlgraphics/batik-all -->
    <dependency>     
        <groupId>org.apache.xmlgraphics</groupId>     
        <artifactId>batik-all</artifactId>     
        <version>1.16</version>     
        <type>pom</type>
        <exclusions>         
            <exclusion>             
                <groupId>*</groupId>             
                <artifactId>*</artifactId>         
            </exclusion>     
        </exclusions> 
    </dependency>

Anyone have an idea to resolve this issue?

I would prefer not to use the tag <packagingExclusions> available under the maven-war-plugin only because I would have to exclude almost 80 specific transitive dependencies total for all the dependencies I need for the project, from being copied into the generated WEB-INF/lib folder of the war file.

Using maven version: 3.3.9 and maven-war-plugin-pinned version: 3.2.2

2

There are 2 best solutions below

0
J Fabian Meier On

The reason for this:

batik-all is a fat JAR. It already contains its dependencies. So you can only take it completely or leave it.

The alternative is to use other batik JARs that are not fat JARs.

0
ďąяЌąɲǥeℓ On

After considering everyone's response to my question I decided to take a step back in approaching the maven transitive dependencies for my project.

I removed all the dependencies, re-ran a mvn clean install command, and for all the build errors that returned I identified each and every dependency that would resolve the build errors to find out which dependency definitions were copying transitive jar files and applied the below specifically to those:

    <exclusions>      
        <exclusion>
            <groupId>*</groupId>       
            <artifactId>*</artifactId>
        </exclusion>
    </exclusions>

Running mvn dependency:tree also helped to visually see the relationships between various dependencies and their transitive dependencies.

Starting over from the beginning helped me identify which maven dependency definitions were truly causing the transitive jar files to be copied into the final deployable artifact for my project.