Exclude JARs from final WAR doesn't work with spring-boot-maven-plugin

416 Views Asked by At

I have a Spring Boot application that is packaged as WAR file and I run it on embedded Apache Tomcat. Using this dependency

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>

and I want to exclude the Apache Tomcat JARs from the final WAR, so I used the maven WAR plugin as follows

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.3.2</version>
        <configuration>
            <packagingExcludes>WEB-INF/lib/tomcat-*.jar,WEB-INF/lib/spring-boot-starter-*.jar</packagingExcludes>
        </configuration>
    </plugin>

Note: I know that I can make the dependency scope provided to generate the war without the tomcat jars, but I make lots of builds and I don't want to remove then add the scope each time I want to make a new war.

The above configuration works fine, the issue is the project is using spring-boot-maven-plugin and when I add it, it seems that the maven war plugin stops working

Question 1 : Is there's a way to make both spring-boot-maven-plugin and maven-war-plugin works together?

File pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.2</version>
        <relativePath/>
    </parent>
    <groupId>test.com</groupId>
    <artifactId>storage</artifactId>
    <version>1</version>
    <name>storage</name>
    <description>Storage API</description>
    <properties>
        <java.version>11</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
    </properties>
    <packaging>war</packaging>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-ui</artifactId>
            <version>1.6.9</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.11.0</version>
        </dependency>
        <!-- Need Tomcat to run project from intellij, but I need to exclude tomcat jars from final war -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
    </dependencies>
    <build>
        <finalName>storage</finalName>
        <plugins>
            <!--
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                 <! -- other config here
        </configuration>
    </plugin>
    -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.2</version>
                <configuration>
                    <packagingExcludes>WEB-INF/lib/tomcat-*.jar,WEB-INF/lib/spring-boot-starter-*.jar
                    </packagingExcludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
1

There are 1 best solutions below

1
Vy Do On
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artufactId>
   <exclusions>
      <exclusion>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-tomcat</artifactId>
      </exclusion>
   </exclusions>

remove all dependencies has tomcat.