在pom.xml中加入如下配置
<build>
<plugins>
...
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>1.5.1</version>
<executions>
<execution>
<!-- 在真正的打包之前,執行一些准備打包壓縮操作的操作 -->
<phase>prepare-package</phase>
<goals>
<goal>compress</goal>
</goals>
</execution>
</executions>
<configuration>
<encoding>UTF-8</encoding>
<!-- 忽視 js 錯誤警告 -->
<jswarn>false</jswarn>
<nosuffix>true</nosuffix>
<linebreakpos>-1</linebreakpos>
<!-- 壓縮的文件 工程里面所有的 js css 后綴的都會壓縮 -->
<includes>
<include>**/*.js</include>
<include>**/*.css</include>
</includes>
<!-- 不需要壓縮的文件 -->
<excludes>
<exclude>**/min.css</exclude>
<exclude>**/min.js</exclude>
</excludes>
<failOnWarning>false</failOnWarning>
</configuration>
</plugin>
<!-- 當壓縮沒有填寫輸出目錄 或者 輸出目錄和壓縮目錄是同一路徑時 一定要配合下面的使用 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<!--
如果不增加此配置 src/main/webapp 下面的內容 會重新復制到target輸出目錄 覆蓋掉編譯后的內容
這樣編譯的還是未壓縮過的內容,增加上此過濾 打war包就不會內容覆蓋
-->
<warSourceExcludes>**/*.js,**/*.css</warSourceExcludes>
</configuration>
</plugin>
...
</plugins>
</build>
附:插件的一些參數說明:
Name: YUI Compressor Maven Mojo
Description: To compress (Minify + Ofuscate) Javascript files and CSS files
(using YUI Compressor from Julien Lecomte) and/or to check Javascript files
with jslint.
Group Id: net.alchim31.maven
Artifact Id: yuicompressor-maven-plugin
Version: 1.5.1
Goal Prefix: yuicompressor
This plugin has 2 goals:
yuicompressor:compress
Description: Apply compression on JS and CSS (using YUI Compressor).
Implementation: net_alchim31_maven_yuicompressor.YuiCompressorMojo
Language: java
Bound to phase: process-resources
Available parameters:
aggregations
a list of aggregation/concatenation to do after processing, for example
to create big js files that contain several small js files. Aggregation
could be done on any type of file (js, css, ...).
disableOptimizations (Default: false)
User property: maven.yuicompressor.disableOptimizations
[js only] disable all micro optimizations.
encoding (Default: UTF-8)
User property: file.encoding
Read the input file using 'encoding'.
excludeResources (Default: false)
Excludes files from resources directories.
excludes
list of additional excludes
excludeWarSourceDirectory
Excludes files from webapp directory
failOnWarning (Default: false)
User property: maven.yuicompressor.failOnWarning
define if plugin must stop/fail on warnings.
force (Default: false)
User property: maven.yuicompressor.force
force the compression of every files, else if compressed file already
exists and is younger than source file, nothing is done.
gzip (Default: false)
User property: maven.yuicompressor.gzip
request to create a gzipped version of the yuicompressed/aggregation
files.
includes
list of additional includes
jswarn (Default: true)
User property: maven.yuicompressor.jswarn
[js only] Display possible errors in the code
linebreakpos (Default: -1)
User property: maven.yuicompressor.linebreakpos
Insert line breaks in output after the specified column number.
nocompress (Default: false)
User property: maven.yuicompressor.nocompress
[js only] No compression
nomunge (Default: false)
User property: maven.yuicompressor.nomunge
[js only] Minify only, do not obfuscate.
nosuffix (Default: false)
User property: maven.yuicompressor.nosuffix
If no 'suffix' must be add to output filename (maven's configuration
manage empty suffix like default).
outputDirectory
User property: project.build.outputDirectory
The output directory into which to copy the resources.
preProcessAggregates (Default: false)
User property: maven.yuicompressor.preProcessAggregates
aggregate files before minify
preserveAllSemiColons (Default: false)
User property: maven.yuicompressor.preserveAllSemiColons
[js only] Preserve unnecessary semicolons.
resources
User property: project.resources
The list of resources we want to transfer.
skip (Default: false)
User property: maven.yuicompressor.skip
Whether to skip execution.
sourceDirectory
Expression: ${project.build.sourceDirectory}/../js
Javascript source directory. (result will be put to outputDirectory).
This allow project with 'src/main/js' structure.
statistics (Default: true)
User property: maven.yuicompressor.statistics
show statistics (compression ratio).
suffix (Default: -min)
User property: maven.yuicompressor.suffix
The output filename suffix.
useProcessedResources (Default: false)
Use processed resources if available
useSmallestFile (Default: true)
User property: maven.yuicompressor.useSmallestFile
use the input file as output when the compressed file is larger than the
original
warSourceDirectory
Expression: ${basedir}/src/main/webapp
Single directory for extra files to include in the WAR.
webappDirectory
Expression: ${project.build.directory}/${project.build.finalName}
The directory where the webapp is built.
需要注意的是:被排除的不壓縮的文件不會被放到war包里