选择显示字体大小

在eclipse中使用ant来自动编译j2me程序

eclipse中使用ant来自动编译j2me程序

作者:cleverpig


版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本声明
原文地址:
http://www.matrix.org.cn/resource/article/43/43770_ant_j2me.html
关键字:j2me,ant,验证,混淆



今天是比较郁闷的一天,一不小心把eclipse的一个j2me项目中的verified/classes目录中的东东删除了。之后才发现eclipse是如何惩罚我的:它再也不会在这个目录中生成class了,呵呵,罢工了我的工。

为什么eclipse不是.netbeans那样用ant来编译、运行j2me项目那??好在本人目前正在研究ant,呵呵,小试牛刀地用了2小时做了个比较完善的ant脚本可以来弥补一下eclipse对我心情的打击了。

下面我把这个脚本和源程序(含demo)贡献出来,大家一起研究探讨。
ant脚本:automake.xml

程序名称:j2me程序的自动编译脚本
version:1.0.0
作者:kevin
mail:greatcleverpig@126.cm

完成以下功能:
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图片文件.

参考资料:
midlet packaging with j2me http://www.onjava.com/pub/a/onjava/2001/04/26/midlet.html
proguard manual http://proguard.sourceforge.net/
ant manual http://ant.apache.org

附加类库:
混淆器类proguard-位于lib中。

文件说明:
conf.properties:配置属性文件
gen.properties:生成manifest.mf和${programname}.jad文件的替换属性文件
jad.tmp:生成jad文件用到的替换模板文件
manifest.tmp:生成manifest.mf文件用到的替换模板文件




<?xml version=&quot;1.0&quot; encoding=&quot;gbk&quot;?>
<project name=&quot;automake&quot; default=&quot;end&quot; basedir=&quot;.&quot;>
        <property file=&quot;conf.properties&quot;/>
        
        <property name=&quot;src.dir&quot; value=&quot;${basedir}\src&quot;/>
        <property name=&quot;res.dir&quot; value=&quot;${basedir}\res&quot;/>
        <property name=&quot;build.dir&quot; value=&quot;${basedir}\build&quot;/>
        <property name=&quot;deploy.dir&quot; value=&quot;${basedir}\deploy&quot;/>
                
        <property name=&quot;package.dir&quot; value=&quot;${basedir}\package&quot;/>
        <property name=&quot;package.extract.dir&quot; value=&quot;${package.dir}\extract&quot;/>

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

        <target name=&quot;run&quot; description=&quot;在模拟器中运行&quot;>
                <echo message=&quot;在模拟器中运行...&quot;/>
                <exec executable=&quot;${emulator.bin}&quot; searchpath=&quot;false&quot;>
                        <arg line=&quot;-classpath ${deploy.dir}\${programname}.jar;${wtk.lib.path}&quot;/>
                        <arg line=&quot;-xdescriptor:${deploy.dir}\${jad.file}&quot;/>
                </exec>
        </target>
                
        <target name=&quot;end&quot; depends=&quot;complie,verifyfornoobfuscate,genmanifest,package,obfuscate,verifyforobfuscate,deploy,genjad,recoverygenproperties,run&quot;/>
</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@


源程序下载:
[下载文件]


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons