js,css代码压缩
web站点需要对js,css代码进行压缩,打包,下面是利用maven进行打包压缩的配置
将压缩后的代码打入到war包中,并且压缩后的js,css文件名不变
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-war-plugin</artifactId>
- <version>2.4</version>
- <configuration>
- <warSourceExcludes>**/*.js,**/*.css</warSourceExcludes>
- </configuration>
- </plugin>
- <plugin>
- <!-- YUI Compressor Maven压缩插件 -->
- <groupId>net.alchim31.maven</groupId>
- <artifactId>yuicompressor-maven-plugin</artifactId>
- <version>1.3.0</version>
- <executions>
- <execution>
- <phase>process-resources</phase>
- <goals>
- <goal>compress</goal>
- </goals>
- </execution>
- </executions>
- <configuration>
- <!-- 读取js,css文件采用UTF-8编码 -->
- <encoding>UTF-8</encoding>
- <!-- 不显示js可能的错误 -->
- <jswarn>false</jswarn>
- <!-- 若存在已压缩的文件,会先对比源文件是否有改动 有改动便压缩,无改动就不压缩 -->
- <force>false</force>
- <!-- 在指定的列号后插入新行 -->
- <linebreakpos>-1</linebreakpos>
- <!-- 压缩之前先执行聚合文件操作 -->
- <preProcessAggregates>true</preProcessAggregates>
- <!-- 压缩后保存文件后缀 无后缀 -->
- <nosuffix>true</nosuffix>
- <!-- 源目录,即需压缩的根目录 -->
- <sourceDirectory>src/main/webapp/resource</sourceDirectory>
- <!-- 压缩js和css文件 -->
- <includes>
- <include>**/*.js</include>
- <include>**/*.css</include>
- </includes>
- <!-- 以下目录和文件不会被压缩 -->
- <excludes>
- <exclude>**/*.min.js</exclude>
- <exclude>**/*.min.css</exclude>
- </excludes>
- </configuration>
- </plugin>
- </plugins>
下面这个标签是对已经压缩过的文件不进行压缩
- <excludes>
- <exclude>**/*.min.js</exclude>
- <exclude>**/*.min.css</exclude>
- </excludes>
但是如果按照如下配置进行打包,那些在源文件中已经是.min结尾的js,css代码将不会打进war包
- <configuration>
- <warSourceExcludes>**/*.js,**/*.css</warSourceExcludes>
- </configuration>