A Maven plugin that compresses static resources using Brotli, Gzip, and Zstandard.
If your web application serves static resources (JavaScript, CSS, SVG, etc.) and your web server supports serving pre-compressed files, this plugin generates .br, .gz, and .zst variants at build time so the server can serve them directly without compressing on every request.
This plugin is designed to work with Spring Framework's ResourceResolver chain. The default resource directories and file extensions match Spring Boot's conventions out of the box. See Spring Boot integration for setup details.
Add the plugin to your pom.xml:
<plugin>
<groupId>am.ik.maven</groupId>
<artifactId>compression-maven-plugin</artifactId>
<version>0.2.1</version>
<executions>
<execution>
<goals>
<goal>compress</goal>
</goals>
</execution>
</executions>
</plugin>By default, this compresses all text-based static resource files under Spring Boot's standard static resource directories (META-INF/resources, resources, static, public) in ${project.build.outputDirectory} during the compile phase. For each file, it creates:
<filename>.br-- Brotli compressed (quality 11)<filename>.gz-- Gzip compressed<filename>.zst-- Zstandard compressed (level 22)
Note: Spring Framework's
EncodedResourceResolverdoes not yet serve pre-compressed.zstfiles. Support is pending in spring-projects/spring-framework#36647. If you serve assets via Spring's resource chain, set<zstdEnabled>false</zstdEnabled>until that change ships in a Spring Framework release.
<configuration>
<fileExtensions>
<fileExtension>js</fileExtension>
<fileExtension>css</fileExtension>
<fileExtension>svg</fileExtension>
<fileExtension>html</fileExtension>
<fileExtension>json</fileExtension>
</fileExtensions>
<resourceDirectories>
<resourceDirectory>static</resourceDirectory>
<resourceDirectory>public</resourceDirectory>
</resourceDirectories>
</configuration><configuration>
<gzipEnabled>false</gzipEnabled>
</configuration><configuration>
<brotliEnabled>false</brotliEnabled>
</configuration><configuration>
<zstdEnabled>false</zstdEnabled>
</configuration><configuration>
<zstdLevel>3</zstdLevel>
</configuration><configuration>
<brotliQuality>6</brotliQuality>
</configuration>Or using Maven properties:
<properties>
<compression.brotli.quality>6</compression.brotli.quality>
</properties>All configuration parameters can be set either in the plugin <configuration> block or as Maven properties.
| Parameter | Property | Default | Description |
|---|---|---|---|
outputDirectory |
compression.outputDirectory |
${project.build.outputDirectory} |
Base directory containing the resource directories |
resourceDirectories |
compression.resourceDirectories |
META-INF/resources, resources, static, public |
Subdirectories under the output directory to scan |
fileExtensions |
compression.fileExtensions |
(see below) | File extensions to compress (without dots) |
brotliQuality |
compression.brotli.quality |
11 |
Brotli compression quality (0-11) |
brotliEnabled |
compression.brotli.enabled |
true |
Enable Brotli compression |
gzipEnabled |
compression.gzip.enabled |
true |
Enable Gzip compression |
zstdEnabled |
compression.zstd.enabled |
true |
Enable Zstandard compression (see Spring Framework note above) |
zstdLevel |
compression.zstd.level |
22 |
Zstandard compression level (practical range 1-22) |
skip |
compression.skip |
false |
Skip the plugin |
The following text-based file extensions are compressed by default:
css, csv, htm, html, js, json, map, mjs, mts, svg, ts, txt, wasm, webmanifest, xml, yaml, yml
These are common web resource types that benefit significantly from compression. Binary formats (images, fonts, etc.) are already compressed and should not be included.
The brotliQuality parameter controls the trade-off between compression ratio and build time:
| Quality | Speed | Compression ratio |
|---|---|---|
| 0-4 | Fast | Lower |
| 5-9 | Moderate | Good |
| 10-11 | Slow | Best |
The default value of 11 gives the best compression. For faster builds (e.g., during development), a lower value like 4-6 may be preferable.
The zstdLevel parameter controls the trade-off between compression ratio and build time:
| Level | Speed | Compression ratio |
|---|---|---|
| 1-3 | Fast | Lower |
| 4-15 | Moderate | Good |
| 16-19 | Slow | Better |
| 20-22 | Very slow ("ultra" mode) | Best |
The default value of 22 gives the best compression. Level 3 matches libzstd's own default and produces noticeably faster builds at the cost of a modest reduction in compression ratio. Note that zstd's decompression speed is, by design, largely independent of the compression level used, so a higher level does not slow down clients that serve the resulting .zst files.
This plugin generates pre-compressed files that Spring Framework's EncodedResourceResolver can serve automatically. To enable this, add the following property to your application.properties:
spring.web.resources.chain.enabled=true
spring.web.resources.chain.compressed=trueWhen this property is enabled, Spring Boot will look for .br or .gz variants of the requested resource and serve the compressed version if the client supports it (via the Accept-Encoding header).
.zst support in EncodedResourceResolver is pending upstream (see spring-projects/spring-framework#36647). Until that change is released, set <zstdEnabled>false</zstdEnabled> if you are serving assets via Spring's resource chain.
For more details, see the Spring documentation:
- Java 17+
- Maven 3.9+
Licensed under the Apache License, Version 2.0.