当前位置: 移动技术网 > IT编程>开发语言>Java > 使用ANT与YUI压缩js的实现方法

使用ANT与YUI压缩js的实现方法

2019年07月22日  | 移动技术网IT编程  | 我要评论
由于项目使用的js很多,为了提高系统效率,将js做压缩处理。
成功的对多个js进行压缩,必须经历下面两步。
1.合并多个js成为一个js.
2.将和并过后的js进行压缩处理。
使用的ant配置主要有:
复制代码 代码如下:

<property  name="root" value="webroot"></property>
 <property  name="js" value="${root}/js"></property>
 <property  name="map_function_js" value="${js}/mapfunc"></property>
 <property name="lib" value="lib"/>
 <property  name="build" value="build"></property>
 <property  name="war" value="war"></property>
 <property  name="war.info" value="${war}/web-inf"></property>
 <property  name="war.lib" value="${war.info}/lib"></property>
 <property  name="war.classes" value="${war.info}/classes"></property>
 <property  name="project.name" value="zjmap"></property>
 <property name="charset" value="utf-8"/>
 <property name="src" value="src"/>
 <target name="创建build目录">
   <mkdir dir="${build}"/>
  </target>

复制代码 代码如下:

 <!-- 将多个js合并成为一个js -->
  <target name="合并js" depends="创建build目录">
          <concat destfile="${build}/mapfuncall.js" encoding="${charset}" outputencoding="${charset}">
           <path path="${map_function_js}/dc.js" />
        <path path="${map_function_js}/stringutil.js" />
           <path path="${map_function_js}/localdc.js" />
           <path path="${map_function_js}/screen.js" />
           <path path="${map_function_js}/wfsquery.js" />
           <path path="${map_function_js}/map.js" />
           <path path="${map_function_js}/query.js" />
           <path path="${map_function_js}/classificationquery.js" />
           <path path="${map_function_js}/busquery.js" />
           <path path="${map_function_js}/routequery.js" />
           <path path="${map_function_js}/cursorposition.js" />
           <path path="${map_function_js}/bufferanalysis.js" />
           <path path="${map_function_js}/divctrl.js" />
            <path path="${map_function_js}/mark.js" />
           <path path="${map_function_js}/overlayanalysis.js" />
           <path path="${map_function_js}/buildquery.js" />
           <path path="${map_function_js}/popshow.js" />
           <path path="${map_function_js}/correct.js" />
           <path path="${map_function_js}/style_result.js" />
            <path path="${map_function_js}/style_ui.js" />
           <path path="${map_function_js}/catalog.js" />
           <path path="${map_function_js}/scenario.js" />
           <path path="${map_function_js}/wfs.js" />
           <path path="${map_function_js}/uuid.js" />
           <path path="${map_function_js}/gps.js" />
           <path path="${map_function_js}/typhoon.js" />
           <path path="${map_function_js}/monitor.js" />
           <path path="${map_function_js}/rainwater.js" />
           <path path="${map_function_js}/approval.js" />
           <path path="${map_function_js}/statistics.js" />
           <path path="${map_function_js}/statisticsnew.js" />
           <path path="${map_function_js}/otilecachecustom.js" />
           <path path="${map_function_js}/bqtool.js" />
           <path path="${map_function_js}/citypositionquery.js" />
           <path path="${map_function_js}/ifieldservice.js" />
           <path path="${map_function_js}/specialquery.js" />
          </concat>
          <replaceregexp match="@debug@" replace="" flags="g" byline="true" file="${build}/mapfuncall.js" encoding="${charset}" />
  </target>
 <!-- 使用雅虎ui进行js压缩 -->
 <target name="开始压缩" depends="合并js">
          <!-- 使用雅虎ui压缩 mapfuncall.js -->
         <antcall target="压缩mapfuncall.js"></antcall>
           <!-- 使用雅虎ui压缩 dataedit.js -->
         <antcall target="压缩dataedit.js"></antcall>
          <!-- 使用雅虎ui压缩 isuggest.js -->
         <antcall target="压缩isuggest.js"></antcall>
         <!-- 复制压缩后的js文件 -->
         <antcall target="复制压缩js文件"></antcall>
 </target>
 <target name="压缩mapfuncall.js">
   <java jar="${lib}/yui/yuicompressor-2.4.6.jar" fork="true">
                <arg line="--type js --charset utf-8 ${map_function_js}/mapfuncall.js -o ${build}/mapfuncall-min.js"/>          
   </java>
 </target>
 <target name="压缩dataedit.js">
  <java jar="${lib}/yui/yuicompressor-2.4.6.jar" fork="true">
            <arg line="--type js --charset utf-8 ${map_function_js}/dataedit.js -o ${build}/dataedit-min.js"/>          
  </java>
 </target>
 <target name="压缩isuggest.js">
   <java jar="${lib}/yui/yuicompressor-2.4.6.jar" fork="true">
       <arg line="--type js --charset utf-8 ${map_function_js}/isuggest.js -o ${build}/isuggest-min.js"/>          
   </java>
 </target>
     <target name="清除build目录" depends="开始压缩">
          <delete dir="${build}"/>
  </target>
 <target name="复制压缩js文件">
      <copy todir="${map_function_js}">
         <fileset dir="${build}">
                   <include name="**.js"/>
            </fileset>
      </copy>
 </target>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网