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
As in image above, I want to exclude all the transitive dependency & dependency Itself.
But according to spring boot maven plugin:
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.

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

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?
