• maven中用yuicompressor和closure-compiler对js、css文件进行压缩


    转载自:http://matychen.iteye.com/blog/1477350


    项目采用maven构建的时候,需要压缩js,css等,网上找了相关资料,自己综合了下~ 

    直接放代码: 

    Xml代码  收藏代码
    1. <!-- js压缩 -->  
    2.             <plugin>  
    3.                 <groupId>net.alchim31.maven</groupId>  
    4.                 <artifactId>yuicompressor-maven-plugin</artifactId>  
    5.                 <version>1.3.0</version>  
    6.                 <executions>  
    7.                     <execution>  
    8.                         <phase>install</phase>  
    9.                         <goals>  
    10.                             <goal>compress</goal>  
    11.                         </goals>  
    12.                         <configuration>  
    13.                             <excludes>  
    14.                                 <exclude>**/*.min.js</exclude>  
    15.                                 <exclude>**/*-min.js</exclude>  
    16.                             </excludes>  
    17.                             <nosuffix>true</nosuffix>  
    18.                             <linebreakpos>-1</linebreakpos>  
    19.                         </configuration>  
    20.                     </execution>  
    21.                 </executions>  
    22.             </plugin>  
    23.             <!-- native2ascii -->  
    24.             <plugin>  
    25.                 <groupId>org.codehaus.mojo</groupId>  
    26.                 <artifactId>native2ascii-maven-plugin</artifactId>  
    27.                 <version>1.0-beta-1</version>  
    28.                 <executions>  
    29.                     <execution>  
    30.                         <phase>install</phase>  
    31.                         <goals>  
    32.                             <goal>native2ascii</goal>  
    33.                         </goals>  
    34.                         <configuration>  
    35.                             <workDir>${project.build.directory}/${project.build.finalName}</workDir>  
    36.                             <excludes>  
    37.                                 <exclude>**/*.css</exclude>  
    38.                             </excludes>  
    39.                             <includes>  
    40.                                 <include>resources/js/*.js</include>  
    41.                             </includes>  
    42.                         </configuration>  
    43.                     </execution>  
    44.                 </executions>  
    45.             </plugin>  


    更喜欢采用谷歌的closure-compiler + maven-antrun-plugin 


    Xml代码  收藏代码
    1. <plugin>  
    2.                 <groupId>org.apache.maven.plugins</groupId>  
    3.                 <artifactId>maven-dependency-plugin</artifactId>  
    4.                 <version>2.4</version>  
    5.                 <executions>  
    6.                     <execution>  
    7.                         <phase>validate</phase>  
    8.                         <goals>  
    9.                             <goal>copy</goal>  
    10.                         </goals>  
    11.                         <configuration>  
    12.                             <artifactItems>  
    13.                                 <artifactItem>  
    14.                                     <groupId>com.google.javascript</groupId>  
    15.                                     <artifactId>closure-compiler</artifactId>  
    16.                                     <version>r1810</version>  
    17.                                     <type>jar</type>  
    18.                                     <overWrite>true</overWrite>  
    19.                                     <outputDirectory>${project.build.directory}/compressor</outputDirectory>  
    20.                                     <destFileName>closure-compiler.jar</destFileName>  
    21.                                 </artifactItem>  
    22.                                 <artifactItem>  
    23.                                     <groupId>com.yahoo.platform.yui</groupId>  
    24.                                     <artifactId>yuicompressor</artifactId>  
    25.                                     <version>2.4.6</version>  
    26.                                     <type>jar</type>  
    27.                                     <overWrite>true</overWrite>  
    28.                                     <outputDirectory>${project.build.directory}/compressor</outputDirectory>  
    29.                                     <destFileName>yuicompressor.jar</destFileName>  
    30.                                 </artifactItem>  
    31.                             </artifactItems>  
    32.                         </configuration>  
    33.                     </execution>  
    34.                 </executions>  
    35.             </plugin>  
    36.             <plugin>  
    37.                 <groupId>org.apache.maven.plugins</groupId>  
    38.                 <artifactId>maven-antrun-plugin</artifactId>  
    39.                 <version>1.7</version>  
    40.                 <executions>  
    41.                     <execution>  
    42.                         <phase>install</phase>  
    43.                         <goals>  
    44.                             <goal>run</goal>  
    45.                         </goals>  
    46.                         <configuration>  
    47.                             <tasks>  
    48.                                 <echo message="开始压缩 JS 文件. 请耐心等待!" />  
    49.                                 <property name="yuicompressor" value="${project.build.directory}/compressor/yuicompressor.jar" />  
    50.                                 <property name="closure-compiler" value="${project.build.directory}/compressor/closure-compiler.jar" />  
    51.                                 <property name="css.compress.directory" value="${project.build.directory}/${project.build.finalName}/resources" />  
    52.                                 <property name="js.compress.directory" value="${css.compress.directory}/js" />  
    53.                                 <apply executable="java" parallel="false" failonerror="true" dest="${js.compress.directory}">  
    54.                                     <fileset dir="${js.compress.directory}" includes="**/*.js" />  
    55.                                     <arg line="-jar" />  
    56.                                     <arg path="${closure-compiler}" />  
    57.                                     <arg line="--warning_level DEFAULT" />  
    58.                                     <arg line="--compilation_level SIMPLE_OPTIMIZATIONS" />  
    59.                                     <arg line="--js" />  
    60.                                     <srcfile />  
    61.                                     <arg line="--js_output_file" />  
    62.                                     <targetfile />  
    63.                                     <mapper type="regexp" from="^(.*).(js)$" to="1-min.2" />  
    64.                                 </apply>  
    65.                                 <move todir="${js.compress.directory}">  
    66.                                     <fileset dir="${js.compress.directory}" includes="**/*.js" />  
    67.                                     <mapper type="regexp" from="^(.*)-min.(js)$" to="1.2" />  
    68.                                 </move>  
    69.                                 <echo message="成功压缩 JS 文件" />  
    70.                                 <echo message="开始压缩 CSS 文件. 请耐心等待!" />  
    71.                                 <apply executable="java" parallel="false" failonerror="true" dest="${css.compress.directory}">  
    72.                                     <fileset dir="${css.compress.directory}" includes="**/*.css" />  
    73.                                     <arg line="-jar" />  
    74.                                     <arg path="${yuicompressor}" />  
    75.                                     <arg line="--charset UTF-8" />  
    76.                                     <arg value="--type"/>  
    77.                                     <arg value="css"/>  
    78.                                     <srcfile />  
    79.                                     <arg line="-o" />  
    80.                                     <targetfile />  
    81.                                     <mapper type="regexp" from="^(.*).(css)$" to="1-min.2" />  
    82.                                 </apply>  
    83.                                 <move todir="${css.compress.directory}">  
    84.                                     <fileset dir="${css.compress.directory}" includes="**/*.css" />  
    85.                                     <mapper type="regexp" from="^(.*)-min.(css)$" to="1.2" />  
    86.                                 </move>  
    87.                                 <echo message="成功压缩 CSS 文件" />  
    88.                             </tasks>  
    89.                         </configuration>  
    90.                     </execution>  
    91.                 </executions>  
    92.             </plugin>  

    JQUERY 的压缩工具 uglifyjs版本的配置,必须要有nodejs环境 (executable="node")即在cmd中能够执行 node --version. uglifyjs可以在https://github.com/mishoo/UglifyJS 下载,下载后续稍稍修改bin/uglifyjs,把里面的 
    Js代码  收藏代码
    1. var uglify = require("uglify-js")  


    修改为 
    Js代码  收藏代码
    1. var uglify = require("./uglify-js");//或者 ../uglify-js  

    因为uglify-js默认是在bin目录外面的。 

    如需更新uglify的版本,每次覆盖相应文件,只修改上面文件即可。 

    Xml代码  收藏代码
    1. <echo message="开始压缩 JS 文件. 请耐心等待!" />  
    2.                                 <property name="uglifyjs" value="${project.build.directory}/compressor/uglify/uglifyjs" />  
    3.                                 <property name="yuicompressor" value="${project.build.directory}/compressor/yuicompressor.jar" />  
    4.                                 <property name="closure-compiler" value="${project.build.directory}/compressor/closure-compiler.jar" />  
    5.                                 <property name="css.compress.directory" value="${project.build.directory}/${project.build.finalName}/resources" />  
    6.                                 <property name="js.compress.directory" value="${css.compress.directory}/js" />  
    7.                                 <apply executable="node" parallel="false" failonerror="true" dest="${js.compress.directory}">  
    8.                                     <fileset dir="${js.compress.directory}" includes="**/*.js" />  
    9.                                     <arg line=" ${uglifyjs}" />  
    10.                                     <arg line=" --ascii" />  
    11.                                     <arg line=" -o" />  
    12.                                     <targetfile />  
    13.                                     <srcfile />  
    14.                                     <mapper type="regexp" from="^(.*).(js)$" to="1-min.2" />  
    15.                                 </apply>  
    16.                                 <move todir="${js.compress.directory}">  
    17.                                     <fileset dir="${js.compress.directory}" includes="**/*.js" />  
    18.                                     <mapper type="regexp" from="^(.*)-min.(js)$" to="1.2" />  
    19.                                 </move>  


  • 相关阅读:
    mtk lk阶段的lcm流程
    MTK touchscreen 流程
    MTK DDR调试
    增加,删除GMS包
    最大最小背光亮度修改
    HDMI 8193 配置
    mtk6737t摄像头配置文件的编译
    camera调试命令
    Linux 终端显示 Git 当前所在分支
    ubuntu系统,关于源(source)的配置
  • 原文地址:https://www.cnblogs.com/ycpanda/p/3637326.html
Copyright © 2020-2023  润新知