最近項目想使用在maven打包的時間壓縮js,css文件,採用yuicompressor-maven-plugin插件進行壓縮,但只是壓縮減少大小,提升請求速度,並無對js進行混淆。下面就寫一下這個插件的使用。css
假設項目包含以下結構html
須要把項目中設計的css和js所有壓縮,可是須要配出已經壓縮過的css,js文件,否則會報錯。java
- <build>
- <finalName>shop</finalName>
- <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>
- <jswarn>false</jswarn>
- <nosuffix>true</nosuffix>
- <linebreakpos>-1</linebreakpos>
- <sourceDirectory>src/main/webapp</sourceDirectory>
- <includes>
- <include>js/*.js</include>
- <include>wapjs/*.js</include>
- <include>activity/**/*.js</include>
- <include>admin/adminjs/*.js</include>
- <include>css/*.css</include>
- <include>activity/**/*.css</include>
- </includes>
- <excludes>
- <exclude>**/*min.js</exclude>
- <exclude>**/jweixin-1.0.0.js</exclude>
- <exclude>**/*min.css</exclude>
- </excludes>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-surefire-plugin</artifactId>
- <version>2.4.2</version>
- <configuration>
- <skipTests>true</skipTests>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-javadoc-plugin</artifactId>
- <version>2.9.1</version>
- <configuration>
- <javadocDirectory>target/javadoc</javadocDirectory>
- <reportOutputDirectory>target/javadoc</reportOutputDirectory>
- <charset>UTF-8</charset>
- <encoding>UTF-8</encoding>
- <docencoding>UTF-8</docencoding>
- <show>private</show>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-war-plugin</artifactId>
- <version>2.6</version>
- <configuration>
- <warSourceExcludes> %regex[^.+(?:(?<!(?:-|\.)min)\.js)], %regex[^.+(?:(?<!(?:-|\.)min)\.css)]</warSourceExcludes>
- <webXml>src/main/webapp/WEB-INF/web.xml</webXml>
- </configuration>
- </plugin>
- </plugins>
- <resources>
- <resource>
- <directory>src/main/resources</directory>
- <includes>
- <include>**/*.properties</include>
- <include>**/*.xml</include>
- </includes>
- <filtering>true</filtering>
- </resource>
- <resource>
- <directory>src/main/java</directory>
- <includes>
- <include>**/*.xml</include>
- </includes>
- <filtering>true</filtering>
- </resource>
- </resources>
- </build>
- 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
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-war-plugin</artifactId>
- <version>2.6</version>
- <configuration>
- <warSourceIncludes>**</warSourceIncludes>
- <warSourceExcludes> %regex[^.+(?:(?<!(?:-|\.)min)\.js)], %regex[^.+(?:(?<!(?:-|\.)min)\.css)]</warSourceExcludes>
- <webXml>src/main/webapp/WEB-INF/web.xml</webXml>
- </configuration>
- 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打包壓縮輸出結果:
上面配置通過實際環境,分享一下。