4

In my Spring Boot project, I want to exclude provided and transitive dependencies from the provided dependency when packaging a jar or war using the spring-boot-maven-plugin

enter image description here

As in image above, I want to exclude all the transitive dependency & dependency Itself.

But according to spring boot maven plugin:

Spring boot maven plugin docs

By default, both the repackage and the run goals will include any provided dependencies that are defined in the project. A Spring Boot project should consider provided dependencies as container dependencies that are required to run the application. Some of these dependencies may not be required at all and should be excluded from the executable jar.

spring-boot-maven-plugin includes "provided" scope dependency in JAR : https://github.com/spring-projects/spring-boot/issues/413

I have tried the following approach to exclude provided dependencies mentioned inside spring-boot-maven-plugin doc.

SOLUTION 1A

Need to specify all explicitly: commons-cli, org.jasypt, tomcat-vault which is tedious and inefficient

    <project> 
    <build> 
        <plugins> 
            <plugin> 
                <groupId>org.springframework.boot</groupId> 
                <artifactId>spring-boot-maven-plugin</artifactId> 
                <configuration> 
                    <excludes> 
                        <exclude>
                            <groupId>org.apache.tomcat</groupId>
                            <artifactId>tomcat-vault</artifactId>
                        </exclude>
                         <exclude>
                            <groupId>commons-cli</groupId>
                            <artifactId>commons-cli</artifactId>
                        </exclude>
                         <exclude>
                            <groupId>org.jasypt</groupId>
                            <artifactId>jasypt</artifactId>
                        </exclude>
                </excludes> 
                </configuration> 
            </plugin> 
        </plugins> 
    </build> 
</project> 

SOLUTION 1B

It will exclude tomcat-vault dependency and Its transitive dependency with groupId commons-cli, org.jasypt


<project> 
    <build> 
        <plugins> 
            <plugin> 
                <groupId>org.springframework.boot</groupId> 
                <artifactId>spring-boot-maven-plugin</artifactId> 
               <configuration> 
         <excludeGroupIds> 
           <!-- Tomcat vault transitive dependencies exclusion-->
                         commons-cli,
                         org.jasypt
         </excludeGroupIds> 
         <excludes> 
             <exclude>
                 <groupId>org.apache.tomcat</groupId>
                 <artifactId>tomcat-vault</artifactId>
             </exclude>
        </excludes> 
         </configuration> 
            </plugin> 
        </plugins> 
    </build> 
</project> 

Problem with this approach: if in future I add new dependencies with groupID org.jasypt, It will exclude from It from packaging

Is there any better solution to achive this?

Working Example:

  • Created a sample spring boot application using following configuration

Main.java

package com.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
        System.out.println("Sample Application Running....");
    }
}

pom.xml configuration

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.test</groupId>
    <artifactId>sb-sample-maven</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.17</version>
        <relativePath/>
    </parent>
    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- Starting of Spring Boot related dependency -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jersey</artifactId>
        </dependency>
        <!-- End of Spring Boot related dependency -->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.7.3</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>sample</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Here postgresql dependency added as a "provided" scoped, when I am packaging jar using "mvn clean compile install -DskipTests" It will package postgresql & its transtive dependency checker-qual. Provided scoped postgessql dependency

package lib inside sample.jar, By plugin it will package the provided jars also Sample.jar libraries

For excluding these provided scoped jars from packaging, need to do either SOLUTION1A or SOLUTION1B mention above.

It there any better solution to achive this?

7
  • You are trying to exclude dependencies from the spring-boot-maven-plugin? Why would you like todo that? The question why you like to exclude deps which are defined provided etc. The Maven plugin is the wrong place... Commented Apr 16, 2024 at 6:22
  • No, I don't want to exclude dependencies from the spring-boot-maven-plugin. In my pom.xml there are some dependencies which are defined provided scope and pom.xml using spring-boot-maven-plugin for package an executable jar, By default spring-boot-maven-plugin will package provided scope dependencies and I don't want to package those dependencies. what should I need to do If using closed source dependencies provided scope and don't want to package it and its transitive dependencies? Commented Apr 16, 2024 at 13:12
  • Checked, maven plugin is the right place: docs.spring.io/spring-boot/docs/current/maven-plugin/reference/… Commented Apr 16, 2024 at 13:13
  • But you have configured it that way... you might meant something different (as described). The spring-boot-maven-plugin will package "provided" scoped deps in your executable jar? Please make an full working example of that... Commented Apr 16, 2024 at 19:32
  • Yes,spring-boot-maven-plugin package "provided" scoped dependencies inside my executable jar. Sure,I will provide example for that Commented Apr 17, 2024 at 4:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.