Combine and minify CSS/Javascript files with Maven plugin

Posted on 05-01-2015 09:21 by graham
In order to combine and minify multiple Javascript/CSS files into one, you can use the Maven plugin called YUI Compressor.

A sample configuration of the plugin in a POM.xml file looks like this:
pom.xml
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>compress</goal>
</goals>
</execution>
</executions>
<configuration>
<nosuffix>true</nosuffix>
<aggregations>
<aggregation>
<!-- remove files after aggregation (default: false)
<removeIncluded>true</removeIncluded>
-->
<!-- insert new line after each concatenation (default: false) -->
<insertNewLine>true</insertNewLine>
<output>${project.build.directory}/${project.build.finalName}/resources/all.js</output>
<!-- files to include, path relative to output's directory or absolute path-->
<!--inputDir>base directory for non absolute includes, default to parent dir of output</inputDir-->
<includes>
<include>**/*.js</include>
</includes>
<!-- files to exclude, path relative to output's directory
<excludes>
<exclude>**/*.pack.js</exclude>
<exclude>**/compressed.css</exclude>
</excludes>
-->
</aggregation>
<aggregation>
<!-- remove files after aggregation (default: false)
<removeIncluded>true</removeIncluded>
-->
<!-- insert new line after each concatenation (default: false) -->
<insertNewLine>true</insertNewLine>
<output>${project.build.directory}/${project.build.finalName}/resources/all-styles.css</output>
<!-- files to include, path relative to output's directory or absolute path-->
<!--inputDir>base directory for non absolute includes, default to parent dir of output</inputDir-->
<includes>
<include>**/*.css</include>
</includes>
<!-- files to exclude, path relative to output's directory
<excludes>
<exclude>**/*.pack.js</exclude>
<exclude>**/compressed.css</exclude>
</excludes>
-->
</aggregation>
</aggregations>
</configuration>
</plugin>
Comments

 

Add comment

Has this tutorial been helpful to you? Or do you see anything wrong? We appreciate your opinion!
Your comment:
Show formatting hints
HTML is disallowed, but in your text you can use the following markup
  • [code][/code] for a block of code
  • [tt][/tt] for inline code
  • [link]link href|link anchor[/link] for links
  • [b][/b] for bold text
Email:
+ Ask a question
If you have a technical question related to programming and computers, ask it here. Other users will help you solve it!
Unanswered questions
Share your knowledge by helping others solve their problems