今天是比较郁闷的一天,一不小心把eclipse的一个j2me项目中的verified/classes目录中的东东删除了。之后才发现eclipse是如何惩罚我的:它再也不会在这个目录中生成class了,呵呵,罢工了我的工。
  
  为什么eclipse不是想netbeans那样用ant来编译、运行j2me项目那??好在本人目前正在研究ant,呵呵,小试牛刀地用了2小时做了个比较完善的ant脚本可以来弥补一下eclipse对我心情的打击了。
  
  下面我把这个脚本和源程序(含demo)贡献出来,大家一起研究探讨。
  
  完成以下功能:
  1.建立基本目录框架
  2.编译src目录中的源文件
  3.调用preverify.exe对类文件进行验证
  4.将类文件生成jar包
  5.调用proguard类对jar包进行混淆,并解压缩
  6.将经过混淆、解压缩后的类、资源文件生成jar包,复制src目录中的jad文件,完成程序部署
  7.调用emulator运行程序
  
  用法:
  只需要修改一下CONF.properties文件中的属性:
  1.midp.lib.dir-WTK的lib目录路径(位于CONF.properties文件中)
  2.midp.lib.path-根据需要扩展使用的jar(位于CONF.properties文件中)
  3.programName-生成jar/jad文件的名字
  在本脚本中可修改的地方:
  preverify.exe和emulator.exe的具体参数可以根据需要修改。
  
  注意:
  1.src目录中需要有MANIFEST.MF和${programName}.jar文件,这些将用在生成jar和部署程序时.
  2.res目录中放置使用的png图片文件.
  
  附加类库:
  混淆器类Proguard-位于lib中。
  
  文件说明:
  CONF.properties:配置属性文件
  GEN.properties:生成MANIFEST.MF和${programName}.jad文件的替换属性文件
  JAD.tmp:生成jad文件用到的替换模板文件
  MANIFEST.tmp:生成MANIFEST.MF文件用到的替换模板文件
  
  
<?xml version="1.0" encoding="GBK"?>
<project name="automake" default="end" basedir=".">
        <property file="CONF.properties"/>
        
        <property name="src.dir" value="${basedir}\src"/>
        <property name="res.dir" value="${basedir}\res"/>
        <property name="build.dir" value="${basedir}\build"/>
        <property name="deploy.dir" value="${basedir}\deploy"/>
                
        <property name="package.dir" value="${basedir}\package"/>
        <property name="package.extract.dir" value="${package.dir}\extract"/>

        <property name="preverified.dir" value="${basedir}\preverified"/>
        <property name="preverified.noObfusedClass.dir" value="${preverified.dir}\noObfused\classes"/>
        <property name="preverified.obfusedClass.dir" value="${preverified.dir}\obfused\classes"/>
        
        <property name="preverify.bin" value="${wtk.home.dir}\bin\preverify.exe"/>
        <property name="emulator.bin" value="${wtk.home.dir}\bin\emulator.exe"/>
        
        <property name="proguard.lib.dir" value="${basedir}\lib"/>
        <property name="jad.file" value="${programName}.jad"/>
                
        <target name="init" description="初始化环境">
                <echo message="初始化目录"/>
                <delete dir="${build.dir}"/>
                <delete dir="${deploy.dir}"/>
                <delete dir="${package.dir}"/>
                <delete dir="${preverified.dir}"/>
                <mkdir dir="${build.dir}"/>
                <mkdir dir="${deploy.dir}"/>
                <mkdir dir="${package.dir}"/>
                <mkdir dir="${package.extract.dir}"/>
                <mkdir dir="${preverified.dir}"/>
                <mkdir dir="${preverified.noObfusedClass.dir}"/>
                <mkdir dir="${preverified.obfusedClass.dir}"/>
        </target>
        
        <target name="complie" depends="init" description="编译">
                <echo message="编译程序"/>
                <javac srcdir="${src.dir}" destdir="${build.dir}">
                        <bootclasspath path="${midp.lib.dir}"/>
                        <classpath>
                                <fileset dir="${midp.lib.dir}" includes="*.jar"/>        
                        </classpath>
                </javac>
        </target>
                                
        <target name="verifyForNoObfuscate" description="验证j2me的class,将验证后的class输出到${preverified.noObfusedClass.dir}">
                <echo message="验证生成的class,将验证后的class输出到${preverified.noObfusedClass.dir}"/>
                <exec executable="${preverify.bin}" searchpath="false">
                        <arg line="-classpath ${midp.lib.path}"/>
                        <arg line="-d ${preverified.noObfusedClass.dir}"/>
                        <arg line="${build.dir}"/>
                </exec>
        </target>
        
        <target name="genManifest" description="根据模板属性文件的值生成Manifest文件">
                <echo message="根据模板属性文件的值生成Manifest文件"/>
                <!--
                        使用${programName}替换掉属性文件GEN.properties中的@PROGRAMNAME@
                -->
                <replace file="GEN.properties"
                                token="@PROGRAMNAME@"
                                value="${programName}"
                />
                <!--
                        使用GEN.properties文件中的属性值替换掉模板文件MANIFEST.tmp中的对应属性值,
                        将替换后的数据复制到${src.dir}\MANIFEST.MF
                -->
                <copy file="MANIFEST.tmp"
                        tofile="${src.dir}\MANIFEST.MF">
                        <filterset begintoken="@" endtoken="@">
                                <filtersfile file="GEN.properties"/>
                        </filterset>
                </copy>
        </target>
                        
        <target name="package" description="将class打包,输出到${package.dir}">
                <echo message="将class打包,输出到${package.dir}"/>
                <jar basedir="${preverified.noObfusedClass.dir}"
                        jarfile="${package.dir}\${programName}-noObfused.jar"
                        manifest="${src.dir}\MANIFEST.MF"
                >
                        <fileset dir="${res.dir}" includes="**/*.png"/>
                </jar>
        </target>
        
        <target name="obfuscate" description="对打包后的jar文件进行混淆,然后解压缩至${package.extract.dir}">
                <echo message="对打包后的jar文件进行混淆,然后解压缩至${package.extract.dir}"/>
                <java fork="true" classname="proguard.ProGuard">
                        <classpath path="${proguard.lib.dir}\proguard.jar"/>
                        <arg line="-libraryjars ${midp.lib.path}"/>
                        <arg line="-injar ${package.dir}\${programName}-noObfused.jar"/>
                        <arg line="-outjar ${package.dir}\${programName}-obfused.jar"/>
                        <!--保留MIDlet的类声明不被混淆-->
                        <arg line="-keep 'public class * extends javax.microedition.midlet.MIDlet'"/>
                        <!--打印被保留的类名-->
                        <arg line="-printseeds"/>
                </java>
                <unjar src="${package.dir}\${programName}-obfused.jar"
                        dest="${package.extract.dir}"
                />
        </target>
        
        <target name="verifyForObfuscate" description="验证被混淆的class,输出到${preverified.obfusedClass.dir}">
                <echo message="验证被混淆的class,输出到${preverified.obfusedClass.dir}"/>
                <exec executable="${preverify.bin}" searchpath="false">
                        <arg line="-classpath ${midp.lib.path}"/>
                        <arg line="-d ${preverified.obfusedClass.dir}"/>
                        <arg line="${package.extract.dir}"/>
                </exec>
                <copy todir="${preverified.obfusedClass.dir}">
                        <fileset dir="${package.extract.dir}" includes="**/*.png"/>
                </copy>
        </target>
        
        <target name="deploy" description="将经过验证的混淆后的class打包,部署在delpoy目录">
                <echo message="将经过验证的混淆后的class打包,部署在${deploy.dir}目录"/>
                <jar basedir="${preverified.obfusedClass.dir}"
                        jarfile="${deploy.dir}\${programName}.jar"
                        manifest="${src.dir}\MANIFEST.MF"
                >
                        <fileset dir="${preverified.obfusedClass.dir}" includes="**/*.class;*.png"/>
                </jar>
        </target>
        
        <target name="genJad" description="在${deploy.dir}目录生成JAD文件">
                <echo message="在${deploy.dir}目录生成JAD文件"/>
                <length file="${deploy.dir}\${programName}.jar" property="jarsize"/>
                <echo message="获得jar文件长度:${jarsize}"/>
                <replace file="GEN.properties"
                        token="@JARSIZE@"
                        value="${jarsize}"
                />
                <replace file="GEN.properties"
                        token="@PROGRAMNAME@"
                        value="${programName}"
                />
                <copy file="JAD.tmp" tofile="${deploy.dir}\${programName}.jad">
                        <filterset begintoken="@" endtoken="@">
                                <filtersfile file="GEN.properties"/>
                        </filterset>
                </copy>
        </target>
        
        <target name="recoveryGenProperties" description="恢复被修改的GEN.properties">
                <echo message="恢复被修改的GEN.properties"/>
                <replace file="GEN.properties"
                        token="MIDLET_NAME=${programName}"
                        value="MIDLET_NAME=@PROGRAMNAME@"
                />
                <replace file="GEN.properties"
                        token="MIDLET_JAR_URL=${programName}.jar"
                        value="MIDLET_JAR_URL=@PROGRAMNAME@.jar"
                />
                <replace file="GEN.properties"
                        token="MIDLET_JAR_SIZE=${jarsize}"
                        value="MIDLET_JAR_SIZE=@JARSIZE@"
                />
        </target>

        <target name="run" description="在模拟器中运行">
                <echo message="在模拟器中运行..."/>
                <exec executable="${emulator.bin}" searchpath="false">
                        <arg line="-classpath ${deploy.dir}\${programName}.jar;${wtk.lib.path}"/>
                        <arg line="-Xdescriptor:${deploy.dir}\${jad.file}"/>
                </exec>
        </target>
                
        <target name="end" depends="complie,verifyForNoObfuscate,genManifest,package,obfuscate,
verifyForObfuscate,deploy,genJad,recoveryGenProperties,run"/>
</project>


配置属性文件:CONF.properties

wtk.home.dir=E:\\WTK22
programName=j2mePractice
midp.lib.dir=${wtk.home.dir}\\lib
midp.lib.path=${midp.lib.dir}\\cldcapi11.jar;${midp.lib.dir}\\midpapi20.jar


生成属性文件:GEN.properties

MIDLET_NAME=@PROGRAMNAME@
MIDLET_VERSION=1.0.0
MIDLET_VENDOR=kevin
MIDLET_01_NAME=MainMidlet
MIDLET_01_LOGO=
MIDLET_01_CLASS=com.gamecollege.work.menu.MainMidlet
MIDLET_JAR_URL=@PROGRAMNAME@.jar
MIDLET_JAR_SIZE=@JARSIZE@
ME_PROFILE=MIDP-2.0
ME_CONF=CLDC-1.1


Manifest模板文件:MANIFEST.tmp

MIDlet-Name: @MIDLET_NAME@
MIDlet-Version: @MIDLET_VERSION@
MIDlet-Vendor: @MIDLET_VENDOR@
MIDlet-1: @MIDLET_01_NAME@,@MIDLET_01_LOGO@,@MIDLET_01_CLASS@
MicroEdition-Profile: @ME_PROFILE@
MicroEdition-Configuration: @ME_CONF@


JAD模板文件:JAD.tmp

MIDlet-Name: @MIDLET_NAME@
MIDlet-Version: @MIDLET_VERSION@
MIDlet-Vendor: @MIDLET_VENDOR@
MIDlet-Jar-URL: @MIDLET_JAR_URL@
MIDlet-Jar-Size: @MIDLET_JAR_SIZE@
MIDlet-1: @MIDLET_01_NAME@,@MIDLET_01_LOGO@,@MIDLET_01_CLASS@
MicroEdition-Profile: @ME_PROFILE@
MicroEdition-Configuration: @ME_CONF@

转自:http://www.j2medev.com/Article/Class1/Class11/200610/3416.html