Maven使用yuicompressor-maven-plugin打包壓縮css、js文件

最近項目想使用在maven打包的時間壓縮js,css文件,採用yuicompressor-maven-plugin插件進行壓縮,但只是壓縮減少大小,提升請求速度,並無對js進行混淆。下面就寫一下這個插件的使用。css

假設項目包含以下結構html


須要把項目中設計的css和js所有壓縮,可是須要配出已經壓縮過的css,js文件,否則會報錯。java

 

[html] view plain copy 在CODE上查看代碼片派生到個人代碼片
  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>  

打包遇到的問題以及說明:

 一、include標籤中配置的是須要壓縮的文件,exclude配置的是在include文件中排除須要壓縮的文件。web

 二、include和exclude中可使用通配符,*表示一個文件或路徑名,**表示多個文件或路徑。apache

 三、jswarn表明是否忽略警告, nosuffix是否添加min後綴, linebreakpos是否壓縮在一行。app

 四、net.alchim31.maven須要在org.apache.maven.plugins插件以前,不然不可以將壓縮的文件打到war包裏。webapp

 五、org.apache.maven.plugins配置說明:maven

       

[html] view plain copy 在CODE上查看代碼片派生到個人代碼片
  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>  
其中

      一、warSourceIncludes不填寫默認是對應 src/main/webapp下的所有文件。ui

      二、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 會拷貝壓縮後的文件過去。spa


如下是maven打包壓縮輸出結果:




上面配置通過實際環境,分享一下。

相關文章
相關標籤/搜索