使用Maven插件压缩js/css

在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包里

参考:https://huangpengpeng.iteye.com/blog/2092189

posted @ 2019-06-12 15:54  Null-Point  阅读(2248)  评论(0编辑  收藏  举报