Skip to content

making/compression-maven-plugin

Repository files navigation

Compression Maven Plugin

A Maven plugin that compresses static resources using Brotli, Gzip, and Zstandard.

When to use this plugin

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.

Usage

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 EncodedResourceResolver does not yet serve pre-compressed .zst files. 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.

Example: Custom extensions and directories

<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>

Example: Brotli only

<configuration>
    <gzipEnabled>false</gzipEnabled>
</configuration>

Example: Gzip only

<configuration>
    <brotliEnabled>false</brotliEnabled>
</configuration>

Example: Disable Zstandard

<configuration>
    <zstdEnabled>false</zstdEnabled>
</configuration>

Example: Lower Zstandard level for faster builds

<configuration>
    <zstdLevel>3</zstdLevel>
</configuration>

Example: Lower Brotli quality for faster builds

<configuration>
    <brotliQuality>6</brotliQuality>
</configuration>

Or using Maven properties:

<properties>
    <compression.brotli.quality>6</compression.brotli.quality>
</properties>

Configuration

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

Default file extensions

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.

Brotli quality

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.

Zstandard level

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.

Spring Boot integration

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=true

When 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:

Requirements

  • Java 17+
  • Maven 3.9+

License

Licensed under the Apache License, Version 2.0.

About

A Maven plugin that compresses static resources using Brotli and Gzip for serving pre-compressed files with Spring Boot

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors