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"/>
<propertyname="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}"/>
<wtkpackageautoversion="${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>
<wtkpreverifyif="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..."/>
<wtkrundevice="${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=""值為空的話,調用手機模仿器的時候會出錯的。
固然結構不是很明白,顯得有點粗糙,也算是告一段落。