This project provides Java classes to do one thing: safely "minify" web resources. The key word is "safely": the aim is not to produce maximal size reduction, nor introduce any morphological changes, but to provide reasonable file size reduction in most cases.
At launch, we will provide classes to minify Javascript and CSS.
There are two minifier classes:
CSSMinifier
for CSS; andJSMinifier
for Javascript.
They both implement Minifier
, which declares a single method:
void minify(Writer writer) throws MinificationException;
Further, they both extend AbstractMinifier
, which provides a
constructor taking a Reader
object. So you need a Reader
and a
Writer
to minify a resource, and that's it. For example:
Reader input = new FileReader("basic.css");
Writer output = new FileWriter("basic-min.css");
Minifier min = new CSSMinifier(input);
try {
min.minify(output);
} catch (MinificationException e) {
// Handle exception
}
And that's it. A MinificationException
will usually wrap some other
exception, such as CSSMinifier.UnterminatedCommentException
. If you
need to know why minification failed, you can call getCause()
on the
exception you catch.
You can use Minifier in your projects by including it as a Maven dependency:
<dependency>
<groupId>net.logicsquad</groupId>
<artifactId>minifier</artifactId>
<version>1.3</version>
</dependency>
If you're building a Maven project, and just want to minify project resources during the build, check out Minifier Maven Plugin, which uses Minifier to do the work.
By all means, open issue tickets and pull requests if you have something to contribute.
Javascript minification is based on Douglas Crockford's original C-based JSMin, via a translation from C to Java by John Reilly. Code from these projects is used here under license.
CSS minification is based on the CSSMin project by Barry van Oudtshoorn. Code from this project is used here under license.