When it comes to optimizing web performance, reducing file sizes of CSS and JavaScript assets is a critical step. The YUI Compressor Maven Plugin offers a streamlined integration path for developers working within the Apache Maven ecosystem. This plugin leverages the power of the YUI Compressor tool to minify JavaScript and CSS files automatically during the build process, ensuring your production code is lean and fast.
Understanding the YUI Compressor Maven Plugin
The YUI Compressor Maven Plugin is a utility designed to integrate Yahoo!'s YUI Compressor directly into the Maven build lifecycle. The YUI Compressor itself is a well-known tool that uses the javax.script API or a standalone JAR to strip unnecessary characters—comments, whitespace, and even short variable names (in safe, optional modes)—from your source files. By wrapping this functionality in a Maven plugin, developers can trigger minification automatically when running standard Maven phases like compile or package.
Setting Up the Plugin in Your pom.xml
The first step to using the plugin is declaring it in your project’s pom.xml file. This involves specifying the plugin's group and artifact ID along with the version. Most common setups pull the plugin from Maven Central, making integration straightforward. Here is a standard declaration block you can adapt to your project’s needs:

<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yui-compressor-plugin</artifactId>
<version>1.5.1</version>
</plugin>
Core Configuration and Common Options
Once the plugin is declared, it must be configured within the <configuration> tag or via properties. Key options include specifying the inputDirectory, outputDirectory, and whether to jswarn for JavaScript strict warnings, which helps catch common errors. Defining the nosuffix property allows you to control whether minified files receive a -min suffix. Setting nosuffix to false is recommended for most workflows to easily distinguish between source and minified files. Additionally, setting aggregate to true can be beneficial to combine multiple files into fewer HTTP requests for better performance.
Important Configuration Parameters
- jswarn: Checks JavaScript syntax and warns on ambiguous or non-strict code.
- nosuffix: When
false, appends-minto output filenames. - aggregate: Reduces HTTP requests by combining files.
Binding to Maven Lifecycle Phases
The plugin binds by default to the process-resources phase or can be bound to any other phase. For example, to run during package, you can configure a <executions> block to call the compress goal. During execution, the plugin processes the files in the configured inputDirectory, minifies them, and places the results in your configured outputDirectory. The goal defaults to compress, making it simple to invoke via command line mvn yui-compressor:compress or during a standard mvn clean install.
YUI Compressor Maven Plugin Example
Consider a scenario where you have a mix of JavaScript and CSS files in the src/main/webapp/resources directory. With the plugin correctly configured to output to a target/webapp/resources directory, the build process will create minified versions of both .js and .css files. Furthermore, setting aggregate to true will merge multiple files into single entities for better network performance. The following table summarizes a typical project file layout and minification outcome:

| Original File Path | Minified Output Path | Note |
|---|---|---|
| src/main/webapp/resources/js/app.js | target/webapp/resources/js/app-min.js | Standard Minification |
| src/main/webapp/resources/css/style.css | target/webapp/resources/css/style-min.css | Whitespace/Comment Removal |
| src/main/webapp/resources/js/*.js | target/webapp/resources/js/combined-min.js | Aggregation Enabled |
This structured approach ensures that your final deployment contains highly optimized assets. The combination of aggregate streams your JavaScript and CSS into fewer files. Browser caching becomes more effective, and download times for end-users—especially those on mobile networks—improve significantly. By integrating YUI Compressor through Maven, the minification step becomes a predictable, repeatable component of your CI/CD pipeline.