Maven使用yuicompressor-maven-plugin打包压缩css、js文件

最近项目想使用在maven打包的时间压缩js,css文件,采用yuicompressor-maven-plugin插件进行压缩,但只是压缩减小大小,提高请求速度,并没有对js进行混淆。下面就写一下这个插件的使用。

假设项目包含如下结构


需要把项目中设计的css和js全部压缩,但是需要配出已经压缩过的css,js文件,不然会报错。

 

  1. <build>  
  2.     <finalName>shop</finalName>  
  3.     <plugins>  
  4.         <plugin>  
  5.             <groupId>net.alchim31.maven</groupId>  
  6.             <artifactId>yuicompressor-maven-plugin</artifactId>  
  7.             <version>1.5.1</version>  
  8.             <executions>  
  9.                 <execution>  
  10.                     <phase>prepare-package</phase>  
  11.                     <goals>  
  12.                         <goal>compress</goal>  
  13.                     </goals>  
  14.                 </execution>  
  15.             </executions>  
  16.             <configuration>  
  17.                 <encoding>UTF-8</encoding>  
  18.                 <jswarn>false</jswarn>  
  19.                 <nosuffix>true</nosuffix>  
  20.                 <linebreakpos>-1</linebreakpos>  
  21.                 <sourceDirectory>src/main/webapp</sourceDirectory>  
  22.                 <includes>  
  23.                     <include>js/*.js</include>  
  24.                     <include>wapjs/*.js</include>  
  25.                     <include>activity/**/*.js</include>  
  26.                     <include>admin/adminjs/*.js</include>  
  27.                     <include>css/*.css</include>  
  28.                     <include>activity/**/*.css</include>  
  29.                 </includes>  
  30.                 <excludes>  
  31.                     <exclude>**/*min.js</exclude>  
  32.                     <exclude>**/jweixin-1.0.0.js</exclude>  
  33.                     <exclude>**/*min.css</exclude>  
  34.                 </excludes>  
  35.             </configuration>  
  36.         </plugin>  
  37.         <plugin>  
  38.             <groupId>org.apache.maven.plugins</groupId>  
  39.             <artifactId>maven-surefire-plugin</artifactId>  
  40.             <version>2.4.2</version>  
  41.             <configuration>  
  42.                 <skipTests>true</skipTests>  
  43.             </configuration>  
  44.         </plugin>  
  45.         <plugin>  
  46.             <groupId>org.apache.maven.plugins</groupId>  
  47.             <artifactId>maven-javadoc-plugin</artifactId>  
  48.             <version>2.9.1</version>  
  49.             <configuration>  
  50.                 <javadocDirectory>target/javadoc</javadocDirectory>  
  51.                 <reportOutputDirectory>target/javadoc</reportOutputDirectory>  
  52.                 <charset>UTF-8</charset>  
  53.                 <encoding>UTF-8</encoding>  
  54.                 <docencoding>UTF-8</docencoding>  
  55.                 <show>private</show>  
  56.             </configuration>  
  57.         </plugin>  
  58.         <plugin>  
  59.             <groupId>org.apache.maven.plugins</groupId>  
  60.             <artifactId>maven-war-plugin</artifactId>  
  61.             <version>2.6</version>  
  62.             <configuration>  
  63.                 <warSourceExcludes> %regex[^.+(?:(?<!(?:-|\.)min)\.js)], %regex[^.+(?:(?<!(?:-|\.)min)\.css)]</warSourceExcludes>  
  64.                 <webXml>src/main/webapp/WEB-INF/web.xml</webXml>  
  65.             </configuration>  
  66.         </plugin>  
  67.     </plugins>  
  68.     <resources>  
  69.         <resource>  
  70.             <directory>src/main/resources</directory>  
  71.             <includes>  
  72.                 <include>**/*.properties</include>  
  73.                 <include>**/*.xml</include>  
  74.             </includes>  
  75.             <filtering>true</filtering>  
  76.         </resource>  
  77.         <resource>  
  78.             <directory>src/main/java</directory>  
  79.             <includes>  
  80.                 <include>**/*.xml</include>  
  81.             </includes>  
  82.             <filtering>true</filtering>  
  83.         </resource>  
  84.     </resources>  
  85. </build>  
  86. lt;/project>  

打包遇到的问题以及说明:

 1、include标签中配置的是需要压缩的文件,exclude配置的是在include文件中排除需要压缩的文件。

 2、include和exclude中可以使用通配符,*表示一个文件或路径名,**表示多个文件或路径。

 3、jswarn代表是否忽略警告, nosuffix是否添加min后缀, linebreakpos是否压缩在一行。

 4、net.alchim31.maven需要在org.apache.maven.plugins插件之前,否则不能够将压缩的文件打到war包里。

 5、org.apache.maven.plugins配置说明:

       

  1.                    <plugin>  
  2. <groupId>org.apache.maven.plugins</groupId>  
  3. <artifactId>maven-war-plugin</artifactId>  
  4. <version>2.6</version>  
  5. <configuration>  
  6.     <warSourceIncludes>**</warSourceIncludes>  
  7.     <warSourceExcludes> %regex[^.+(?:(?<!(?:-|\.)min)\.js)], %regex[^.+(?:(?<!(?:-|\.)min)\.css)]</warSourceExcludes>  
  8.     <webXml>src/main/webapp/WEB-INF/web.xml</webXml>  
  9. </configuration>  
  10. lt;/plugin>  
其中

      1、warSourceIncludes不填写默认是对应 src/main/webapp下的全部文件。

      2、warSourceExcludes装配warSourceIncludes中排除不以.min.js -min.js结尾的js文件。 warSourceIncludes不填写默认是对应 src/main/webapp下的全部文件,因为在 yuicompressor配置了不会处理min这些文件所以这些文件要拷贝到对应的目录。由于maven-war-plugin会把 yuicompressor配置<include>的文件压缩覆盖输出到target文件夹里,但是<exclude>排除掉的 文件并未放到target里,所以会导致打war包的时间未将<exclude>排除的文件为打入包中,所以此处需要 warSourceExcludes将yuicompressor没有压缩的js文件就会拷贝过去,而压缩过的js则不会拷贝yuicompressor 会拷贝压缩后的文件过去。


以下是maven打包压缩输出结果:




上面配置经过实际环境,分享一下。

posted on 2016-07-21 10:56  夏天的早餐  阅读(6773)  评论(0编辑  收藏  举报

导航