Search code examples
javamavenmaven-resources-plugin

Maven Resources plugin ignoring exclusion list


TL; DR

Maven's Resources plugin doesn't seem to respect excludes elements in the resource configuration.

Setting

I have a large Java/Dart project where I need to deploy a WAR file that has both my UI and my backend in separate JARs. I want to cut down on the size of the deployed file, and I want to drop certain folders from the WAR. Based on the plugin documentation, I thought I could simply set excludes in my plugin configuration, and it won't copy over the unnecessary folders. However, it seems the Resources plugin is outright ignoring these, despite, the Maven model package including a setExcludes function.

Current Attempts

So far, I've tried two main approaches. My configuration is as follows:

<configuration>
    <outputDirectory>${project.build.directory}/${project.build.finalName}</outputDirectory>
    <resources>
        <resource>
            <directory>src/main/webapp</directory>
            <excludes>
                <exclude>web.xml</exclude>
                <exclude>appengine-web.xml</exclude>
                <exclude>**/web/_el/*</exclude>
                <exclude>WEB-INF/pages/frontend/**</exclude>
                <exclude>**/_el/js/frontend/**</exclude>
                <exclude>**/_el/dart/app/dashboard/lib/**</exclude>
                <exclude>**/_el/dart/app/dashboard/.dart_tool/**</exclude>
            </excludes>
        </resource>
    </resources>
</configuration>

I tried to use this config inside the execution element, as well as outside from directly under the plugin element, but both times it was ignored, and everything in the webapp directory was copied over mindlessly.
On a hunch, I did try setting filtering to true, but that just ate up all the memory in my computer, and it didn't even work - what it did process was copied over.

I also tried using the Shade plugin, but gave up on that pretty quickly, as the DontIncludeResourceTransformer only permits suffix-filtering, which is not adequate for my use case.

Question

So what am I doing wrong? Based on the docs, I believe the plugin should respect my excludes list and skip the vast majority of files, but it's evidently not doing that.


Solution

  • You need to use apache **maven war plugin**. The WAR Plugin is responsible for collecting all artifact dependencies, classes and resources of the web application and packaging them into a web application archive.

    It is possible to include or exclude certain files from the WAR file, by using the and configuration parameters. They each take a comma-separated list of Ant file set patterns. You can use wildcards such as ** to indicate multiple directories and * to indicate an optional part of a file or directory name.

    Here is an example where we exclude all JAR files from WEB-INF/lib:

    <project>
      ...
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.3.1</version>
            <configuration>
              <packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>
            </configuration>
          </plugin>
        </plugins>
      </build>
      ...
    </project>
    

    https://maven.apache.org/plugins/maven-war-plugin/examples/including-excluding-files-from-war.html