fkoehler / play-html-compressor   2.8.0


Google's HTML Compressor for Play Framework 2

Scala versions: 2.13 2.12 2.11

Google's HTML (and XML) Compressor for Play Framework 2 Maven Central Build Status

This project is a fork of


In your project/Build.scala:

libraryDependencies ++= Seq(
  "com.github.fkoehler" %% "play-html-compressor" % "2.8.0"


  • For Play Framework 2.8 use version 2.8.0
  • For Play Framework 2.7 use version 1.0.0
  • For Play Framework 2.6 use version 0.7.1
  • For Play Framework 2.5 use version 0.6.3
  • For Play Framework 2.4 use version 0.5.0
  • For Play Framework 2.3 use version 0.3.1
  • For Play Framework 2.2 use version 0.2.1
  • For Play Framework 2.1 use version 0.1-SNAPSHOT

How to use

The filter comes with built-in HtmlCompressor and XmlCompressor configurations, but it can also be used with user-defined configurations. The following two examples shows how to define the filters with the default and the user-defined configurations.

To provide the filters for your application you must define it as described in the Play Documentation (Scala, Java).

Provide filters

For Scala users

import javax.inject.Inject

import play.api.http.HttpFilters
import play.api.mvc.EssentialFilter

class Filters @Inject() (
  htmlCompressorFilter: HTMLCompressorFilter,
  xmlCompressorFilter: XMLCompressorFilter)
  extends HttpFilters {

  override def filters: Seq[EssentialFilter] = Seq(

For Java users

import play.mvc.EssentialFilter;
import play.http.HttpFilters;

import javax.inject.Inject;

public class DefaultFilter implements HttpFilters {

    private HTMLCompressorFilter htmlCompressorFilter;
    private XMLCompressorFilter xmlCompressorFilter;

    public DefaultFilter(
        HTMLCompressorFilter htmlCompressorFilter,
        XMLCompressorFilter xmlCompressorFilter) {

        this.htmlCompressorFilter = htmlCompressorFilter;
        this.xmlCompressorFilter = xmlCompressorFilter;

    public EssentialFilter[] filters() {
        return new EssentialFilter[] {

Default filter

For the default filters we provide DI modules which will be automatically enabled if you pull in the dependency. You must only provide your instance of HttpFilters as described above.

User-defined filter

For user defined filters there is a little bit mor to do. First you must create your instances of the filter. As next you must provide your instance of HttpFilters as described above. At last you must provide the bindings for you created filter and disable the default DI modules.

Implement filters

For Scala users
import javax.inject.Inject

import com.googlecode.htmlcompressor.compressor.HtmlCompressor
import play.api.{Configuration, Environment, Mode}

class CustomHTMLCompressorFilter @Inject() (
  val configuration: Configuration, environment: Environment, val mat: Materializer)
  extends HTMLCompressorFilter {

  override val compressor: HtmlCompressor = {
    val c = new HtmlCompressor()
    if (environment.mode == Mode.Dev) {

For Java users
import com.googlecode.htmlcompressor.compressor.HtmlCompressor;
import play.Environment;
import play.Mode;
import play.api.Configuration;

import javax.inject.Inject;

public class CustomHTMLCompressorFilter extends HTMLCompressorFilter {

    private Configuration configuration;
    private Environment environment;
    private Materializer mat;

    public CustomHTMLCompressorFilter(
        Configuration configuration, Environment environment, Materializer mat) {

        this.configuration = configuration;
        this.environment = environment;
        this.mat = mat;

    public Configuration configuration() {
        return configuration;

    public HtmlCompressor compressor() {
        HtmlCompressor compressor = new HtmlCompressor();
        if (environment.mode() == Mode.DEV) {


        return compressor;

    public Materializer mat() {
        return mat;


Provide bindings

To provide your bindings for your user defined filter you must either create a new module or you can add the binding to your default DI module. This process is detailed documented for Scala and Java users. So please refer to this documentation.

Disable default modules

To disable the default modules you must append the modules to the play.modules.disabled property in application.conf:

play.modules.disabled += ""
play.modules.disabled += ""

Customize filter behaviour

You have the possibility to customize filter behaviour without using class inheritance. For that, you could adding the following keys on your application.conf file :

play.filters {

  # Play Html Compressor
  # ~~~~~
  compressor {
    html {
      preserveLineBreaks = false
      removeComments = true
      removeIntertagSpaces = false
      removeHttpProtocol = true
      removeHttpsProtocol = true

    xml {
      removeComments = true
      removeIntertagSpaces = true

Release a new version

Follow the steps listed here: but basically it is

; + publishSigned; sonatypeBundleRelease

First the proper gnup keys need to be set locally