Tag Archives: YUI Compressor Maven

YUI Compressor Maven: Compressing and obfuscating JavaScript and CSS files

Core functions

Compressing and obfuscating JavaScript and CSS files

It can be used to compress webapp directory (SRC/main/webapp) and JS and CSS files in jar

The compressed file is renamed with – min as the suffix by default, so the compressed version and the original version can exist at the same time in the application. But if you don’t want to keep the original file and want to override it, set the option ‘nosuffix’ to ‘true’

Usage

1. Minimum requirements

Maven:3.0.4

JDK: Maven compiler plugin version is 2.5.1

2. In the project pom.xml Configure plugin

<project&>
  ...
  <build&>
    <!-- To define the plugin version in your parent POM --&>
    <pluginManagement&>
      <plugins&>
        <plugin&>
          <groupId&>net.alchim31.maven</groupId&>
          <artifactId&>yuicompressor-maven-plugin</artifactId&>
          <version&>1.5.1</version&>
        </plugin&>
        ...
      </plugins&>
    </pluginManagement&>
    <!-- To use the plugin goals in your POM or parent POM --&>
    <plugins&>
      <plugin&>
        <groupId&>net.alchim31.maven</groupId&>
        <artifactId&>yuicompressor-maven-plugin</artifactId&>
        <version&>1.5.1</version&>
      </plugin&>
      ...
    </plugins&>
  </build&>
  ...
</project&>

Common configuration items

aggregations

Merge files, you can merge multiple small JS, CSS into a file

disableOptimizations

Only for JS, disable optimization, default is false

encoding

The encoding format used for reading files is UTF-8 by default

excludeResources

Exclude files in resources directory

excludeWarSourceDirectory

Exclude files in webapp directory

excludes

Exclude file, use with exclude

failOnWarning

Specifies whether the plug-in fails when it encounters warning. The default is false

force

When true, the compressed file is forced to be compressed; when false, if there is a compressed file, it will first compare whether the source file has been changed. If there is a change, it will be compressed, and if there is no change, it will not be compressed

gzip

Package file or not, default to fasle

includes

Add additional files

jswarn

Only applicable to JS, whether to display the possible errors in JS. Default true

linebreakpos

Insert a new row after the specified column, default – 1

nocompress

Do not compress, default is false

nomunge

For JS only. Compression only, no confusion. Default false

nosuffix

The compressed file has no suffix. Default false

outputDirectory

Compressed file save directory

preProcessAggregates

Merge files before compression. Default false

preserveAllSemiColons

For JS only. Keep unnecessary semicolons. Default false

resources

What resources do you want to convert?

skip

Whether to skip execution. Default false

sourceDirectory

Javascript source directory, compressed directory in the output directory. ${ project.build.sourceDirectory }/../js

statistics

Display statistics (compression ratio)

suffix

Compressed file suffix, default – min

useProcessedResources

If available, the processed resources are used. The default value is false

useSmallestFile

Use smaller files. When the compressed file is larger than the original file, use the original file as the output. Default true

warSourceDirectory

You need to include a single directory to war. ${basedir}/src/main/webapp

webappDirectory

The directory of the web application. ${ project.build.directory }/${ project.build.finalName }

Examples

<configuration&>
	<!-- Read js,css files with UTF-8 encoding --&>
	<encoding&>UTF-8</encoding&>
	<! -- do not show js possible errors --&>
	<jswarn&>false</jswarn&>
	<! -- If a compressed file exists, it will first compare the source file for changes. If there are changes, it will be compressed, if there are no changes, it will not be compressed --&>
	<force&>true</force&>
	<linebreakpos&>-1</linebreakpos&>
	<! -- Inserts a new row after the specified column number --&>
	<linebreakpos&>-1</linebreakpos&>
	<! -- Perform aggregate file operations before compressing --&>
	<preProcessAggregates&>true</preProcessAggregates&>
	<! -- Save file suffix after compression No suffix --&>
	<nosuffix&>true</nosuffix&>
	<! -- source directory, i.e. the root directory to be compressed --&>
	<sourceDirectory&>src/main/webapp/js/full_js</sourceDirectory&>
	<! -- Compress js and css files --&>
	<includes&>
		<includes&>*.js</include&>
	</includes&>
	<outputDirectory&>src/main/webapp/js/min_js</outputDirectory&>
	<! -- The following directories and files will not be compressed --&>
        <excludes&>
	    <exclude&>min_js/*.js</exclude&>
	</excludes&>
</configuration&>