How to exclude resource file from spring boot jar?

Instead of using maven-spring-boot plugin use maven-resource plugin and maven profiles:

<profiles>
  <profile>
    <id>prod</id>
    <build>
      <resources>
        <resource>
          <filtering>true</filtering>
          <directory>[your directory]</directory>
          <excludes>
            <exclude>[non-resource file #1]</exclude>
            <exclude>[non-resource file #2]</exclude>
            <exclude>[non-resource file #3]</exclude>
            ...
            <exclude>[non-resource file #n]</exclude>
          </excludes>
        </resource>
      </resources>
    </build>
  </profile>
</profiles>

Make sure you specify <filtering>true</filtering> option inside resource element.

Create one profile for each environment and filter those files.

Make sure to execute maven with the proper profile:

mvn clean install -P prod

To view more examples of maven-resource plugin take a look at maven-resource

If you want to learn more about profiles, take a look at profiles


The Spring Boot Maven plugin repackages the JAR file created by the Maven JAR Plugin. So you also have the option to simply exclude files when the JAR is first built, which keeps the Spring Boot Maven plugin from finding them in the first place:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.1.0</version>
    <configuration>
        <excludes>
             <exclude>application-dev*</exclude>
             <exclude>application-test*</exclude>
        </excludes>
    </configuration>
</plugin>