转自: http://blog.csdn.net/wormwormwormworm/ 

ANT的应用范围很广,而且用起来也是简单方便。最近用ANT工具实现了J2ME程序的编译,打包,混淆,运行 ,一条龙服务,以后再也不用在IDE里点来点去,浪费时间了,写篇文章,做为总结。

一 实现J2ME程序的编译,打包,混淆,运行 的环境

 1) JAVA环境
开发环境是JDK1.5,WTK22
 2) 环境变量
JAVA_HOME,PATH,WTK_HOME之类的设置
 3) JAR包
混淆包:proguard3.5
针对J2ME的包:antenna.jar

二 编写build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="MobileBook" default="run" basedir=".">
 <property name="midlet.name" value="MobileBook"/>
 <property name="wtk.cldc.version" value="1.0"/>
 <property name="wtk.midp.version" value="2.0"/>
<!---->
 <property name="path.antenna.jar" value="E:\work\Java\ant-1.7.0\lib\antenna.jar"/>
 <property name="wtk.home" value="D:\WTK22"/>
 <property name="wtk.proguard.home" value="E:\work\Java\proguard3.5"/>
 <property name="path.build.output" value="${basedir}/Deploy"/>
 <property name="flag.autoversion" value="false"/>
 <property name="project.root" value="${basedir}"/>
 <property name="path.build.output" value=" ${basedir}/build"/>
 <property name="wtk.midpapi" value="${wtk.home}\lib\midpapi20.jar;${wtk.home}\lib\cldcapi11.jar;
    ${wtk.home}\lib\wma20.jar;${wtk.home}\lib\mmapi.jar;
    ${wtk.home}\lib\j2me-ws.jar;${wtk.home}\lib\jsr75.jar;
    ${wtk.home}\lib\jsr082.jar;${wtk.home}\lib\jsr184.jar"/>
 <property name="path.build" value="${basedir}/build"/>
 <property name="path.build.classes" value="${basedir}/build/classes"/>
 <property name="flag.preverify" value="true"/>
 <property name="run.device.name" value="DefaultColorPhone"/>
 <property name="run.trace.options" value=""/>
 <property name="jad.vendor" value="1.0"/>
 <property name="jad.version" value="1.0"/>
 <path id="classpath.project">
  <path location="${path.build.classes}"/>
  <path location="${path.build.classes}/_no_export"/>
  <path location="${project.root}/KXml.jar"/>
 </path>
 <target depends="initialize_antenna" name="jad">
  <wtkjad jadfile="${midlet.name}.jad" name="${midlet.name}" vendor="${jad.vendor}" version="${jad.version}">
<!--要修改,class是指向你工程的入口类-->
   <midlet name="${midlet.name}" icon="" class="com.gaodu.book.AppMidlet"/>
   <attribute name="MIDlet-Jar-URL" value="${midlet.name}.jar"/>
   <attribute name="MicroEdition-Profile" value="MIDP-${wtk.midp.version}"/>
   <attribute name="MicroEdition-Configuration" value="CLDC-${wtk.cldc.version}"/>
   <attribute name="OrgCode" value="gaodu"/>
  </wtkjad>
 </target>
 <target depends="jad" name="build">
  <echo message="Building source files..."/>
  <antcall target="clean"/>
  <wtkbuild destdir="${path.build.classes}" encoding="GBK" source="1.3" sourcepath="" srcdir="${project.root}/res">
   <classpath refid="classpath.project"/>
  </wtkbuild>
  <wtkbuild destdir="${path.build.classes}" encoding="GBK" source="1.3" sourcepath="" srcdir="${project.root}/src">
   <classpath refid="classpath.project"/>
  </wtkbuild>
 </target>
 <target  name="initialize_build">
  <echo message="Initializing build environment..."/>
  <mkdir dir="${path.build}"/>
  <mkdir dir="${path.build.classes}"/>
  <mkdir dir="${path.build.classes}/_no_export"/>
  <mkdir dir="${path.build.output}"/>
 </target>
 <target depends="initialize_build" name="initialize_antenna">
  <echo message="Initializing EclipseME Antenna build environment..."/>
  <taskdef resource="antenna.properties">
   <classpath location="${path.antenna.jar}"/>
  </taskdef>
 </target>
 <target depends="build" name="jar">
  <echo message="Generating unobfuscated jar file..."/>
  <delete includeEmptyDirs="true" quiet="true">
   <fileset dir="${path.build.output}" includes="*,*/**"/>
  </delete>
  <antcall target="package"/>
 </target>
 <target name="package">
  <copy file="${midlet.name}.jad" todir="${path.build.output}"/>
  <wtkpackage autoversion="${flag.autoversion}" jadfile="${path.build.output}/${midlet.name}.jad" jarfile="${path.build.output}/${midlet.name}.jar" obfuscate="false" preverify="${flag.preverify}">
   <fileset dir="${path.build.classes}">
    <exclude name="_no_export/"/>
   </fileset>
   <classpath refid="classpath.project"/>
   <fileset dir="${project.root}/res">
    <exclude name="**/*.java"/>
    <exclude name="build/"/>
    <exclude name="**/*.scc"/>
    <exclude name="**/*.db"/>
   </fileset>
   <fileset dir="${project.root}/src">
    <exclude name="build/"/>
    <exclude name="**/*.scc"/>
    <exclude name="**/*.java"/>
   </fileset>
   <zipfileset src="${project.root}/kxml.jar"/>
  </wtkpackage>
  <antcall target="obfuscate"/>
 </target>
 <target if="flag.should.obfuscate" name="obfuscate">
  <wtkobfuscate jadfile="${path.build.output}/${midlet.name}.jad" jarfile="${path.build.output}/${midlet.name}.jar">
   <argument value="'-keep public class * extends javax.microedition.midlet.MIDlet'"/>
   <argument value="-dontusemixedcaseclassnames -dontnote -defaultpackage ''"/>
  </wtkobfuscate>
  <wtkpreverify if="flag.preverify" jadfile="${path.build.output}/${midlet.name}.jad" jarfile="${path.build.output}/${midlet.name}.jar"/>
 </target>
 <target depends="jar" name="deployable">
  <echo message="Generating obfuscated jar file..."/>
  <delete includeEmptyDirs="true" quiet="true">
   <fileset dir="${path.build.output}" includes="*,*/**"/>
  </delete>
  <antcall target="package">
   <param name="flag.should.obfuscate" value="true"/>
  </antcall>
 </target>
 <target depends="deployable" name="run">
  <echo message="Running emulator..."/>
  <wtkrun device="${run.device.name}" jadfile="${path.build.output}/${midlet.name}.jad" trace="${run.trace.options}" wait="true"/>
 </target>
 <target name="clean">
  <delete includeEmptyDirs="true" quiet="true">
   <fileset dir="${path.build}" includes="*,*/**"/>
  </delete>
 </target>
</project>

三 运行

运行命令:ANT -f  E:\Ant\Build.xml

有个地方是要注意的,<wtkjad jadfile="${midlet.name}.jad" name="${midlet.name}" vendor="" version="">,如果vendor="" version="" 值为空的话,调用手机模拟器的时候会出错的。

虽然结构不是很清晰,显得有点粗糙,也算是告一段落。