maven 实现Js、css文件压缩

260 阅读1分钟
<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>         
            <configuration>            
                <excludes>               
                    <exclude>**/*.min.js</exclude>               
                    <exclude>**/*-min.js</exclude>               
                    <exclude>**/echarts.js</exclude>               
                </excludes>            
                <includes>               
                    <include>**/*.js</include>               
                    <include>**/*.css</include>            
                </includes>            
                <encoding>UTF-8</encoding>            
                <jswarn>false</jswarn>            
                <failOnWarning>false</failOnWarning>            
                <nosuffix>true</nosuffix>            
                <force>true</force>            
                <removeIncluded>true</removeIncluded>            
                <linebreakpos>-1</linebreakpos>         
                </configuration>      
        </execution>   
    </executions>
</plugin>

<plugin>   
    <groupId>org.apache.maven.plugins</groupId>   
    <artifactId>maven-war-plugin</artifactId>   
    <version>2.6</version>   
    <configuration>      
        <warName>XXX</warName>      
        <!-- 打包时排除dev和prod目录 -->      
        <warSourceExcludes>
            resources/js/dev/*,resources/js/prod/*,resources/js/trial-dev/*,resources/js/trial-prod/*,**/*.js,**/*.css
        </warSourceExcludes>      
        <webResources>         
            <resource>            
                <!-- 使用对应版本目录(dev/prod)下的内容进行替换 -->            
                <directory>src/main/webapp/resources/js/${package.environment}</directory> 
               <targetPath>resources/js/ilnmcio</targetPath>         
            </resource>      
        </webResources>   
    </configuration>
</plugin>

重点:

1.一定要设置成prepare-package

<phase>prepare-package</phase>

2.要加上includes包含的文件

<includes>               
                    <include>**/*.js</include>               
                    <include>**/*.css</include>            
                </includes>  

3. 打包时排除dev和prod目录加上 **/*.js,**/*.css

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.6</version>    <configuration>   
        <warName>XXXX</warName>   
        <webXml>src/main/webapp/WEB-INF/web.xml</webXml>   
        <!-- 打包时排除的目录 -->   
        <warSourceExcludes>    
            <!-- 文件夹路径这样匹配 -->   
            resources/js/dev/*,
            file/**,            %regex[^.+(?:(?&lt;!(?:-|\.)min|ilnmcio)\.js)],
            %regex[^.+(?:(?&lt;!(?:-|.)min|iconfont).css)],
        </warSourceExcludes>   
        <webResources>      
            <resource>         
                <!-- 使用对应版本目录(dev/prod)下的内容进行替换 -->         
                <directory>src/main/webapp/resources/js/${package.environment}</directory>
                <targetPath>            
                    resources/js/ilnmcio         
                </targetPath>      
            </resource>      
            <resource>         
                <directory>src/main/webapp/resources/js/file</directory>
                <targetPath>            
                    resources/js/file
                </targetPath>     
            </resource> 
        </webResources>
    </configuration></plugin>

yuicompressor-maven-plugin 插件里exclude的文件和文件要在maven-war-plugin的warSourceExcludes中,如果是正则匹配的文件就不用在webResources在把他添加回来 如果是文件目录,需要在webResources再次添加回来。否则生成的war包中就不含有你排除的目录