最近的一个兼职项目中涉及到了J2ME,需要在程序中使用ANT打包手机驱动、图片等下载资源;这是一个主要程序的备份:
这是要执行的build文件:
用于j2me的build.xml:
package lesvos_package;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;
import eu.eodigos.hibernate.bean.Content;
import eu.eodigos.hibernate.server.AccessDBServer;
import eu.eodigos.hibernate.server.AccessDBServerImp;
public class AntUtil
{
AccessDBServer access = new AccessDBServerImp();
String email = "";
String language = "";
String phoneid = "";
String phonename = "";
String subphonename = "";
String dimage = "";
String dmap = "";
String dvideoOraudio = "";
String xmlfilename = "";
String screensize = "";
String screenfolder = "";
String devicename = "";
List contidlist = new ArrayList();
public AntUtil(String email, String language, String phoneid, String phonename, String subphonename, String dimage,
String dmap, String dvideoOraudio)
{
try
{
this.email = email;
this.language = language;
this.phoneid = phoneid;
this.phonename = phonename;
this.subphonename = subphonename;
this.dimage = dimage;
this.dmap = dmap;
this.dvideoOraudio = dvideoOraudio;
} catch (Exception e)
{
e.printStackTrace();
}
}
public void doResourceCopy()
{
screensize = access.getScreenFlagByPhoneid(phoneid);
devicename = access.getJ2meNameByPhoneid(phoneid);
if (screensize.equals("A"))
{
screenfolder = "FullCanvas.128+x160";
} else if (screensize.equals("B"))
{
screenfolder = "FullCanvas.176x220";
} else if (screensize.equals("C"))
{
screenfolder = "FullCanvas.240x320";
}
contidlist = access.getMunicipalDate(email);
// System.out.println(System.getProperty("user.dir"));
File buildFile = new File(FileConf.copyBuildingpath);
Project p = new Project();
DefaultLogger consoleLogger = new DefaultLogger();
consoleLogger.setErrorPrintStream(System.err);
consoleLogger.setOutputPrintStream(System.out);
consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
p.addBuildListener(consoleLogger);
try
{
p.fireBuildStarted();
p.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
helper.parse(p, buildFile);
// set the path of resource
p.setProperty("toresource.dir", FileConf.resourceTopath);
p.setProperty("fromresource.dir", FileConf.rescourceFrompath);
// p.setProperty("jar.dir", FileConf.rescourceFrompath + "/dist/");
// if the resourceTopath directory is not exsit,then do not run
// deldir target
File resourcetofile = new File(FileConf.resourceTopath);
if (resourcetofile.exists())
{
// resourcetofile.mkdir();
p.executeTarget("deldir");
}
p.setProperty("screenfolder", screenfolder);
p.executeTarget("makefolder");
if (this.dvideoOraudio.equals("1"))
{
p.setProperty("dvideoOraudio", "video");
p.executeTarget("makeOrfolder");
} else if (this.dvideoOraudio.equals("2"))
{
p.setProperty("dvideoOraudio", "audio");
p.executeTarget("makeOrfolder");
}
p.executeTarget("docopyscreen");
p.executeTarget("docopyxml");
if (contidlist != null)
{
String contid = "";
Content content = new Content();
for (Iterator iter = contidlist.iterator(); iter.hasNext();)
{
content = (Content) iter.next();
contid = content.getContId().toString();
p.setProperty("cont_id", contid);
p.setProperty("screensize", screensize);
if (this.dimage.equals("1"))
{
p.executeTarget("docopyimages");
}
if (this.dvideoOraudio.equals("1"))
{
p.executeTarget("docopyvideo");
} else if (this.dvideoOraudio.equals("2"))
{
p.executeTarget("docopyaudio");
}
}
}
// judge the files
if (this.dvideoOraudio.equals("1"))
{
if (judgeVideoOrAudioFolder("video"))
p.executeTarget("docopynovideo");
} else if (this.dvideoOraudio.equals("2"))
{
if (judgeVideoOrAudioFolder("audio"))
p.executeTarget("docopynoaudio");
}
p.setProperty("devicename", devicename);
// p.executeTarget("doresourcejar");
p.fireBuildFinished(null);
} catch (BuildException e)
{
p.fireBuildFinished(e);
}
}
/**
* according to the dvideoOraudio value to judge this folder is null or not
*/
public boolean judgeVideoOrAudioFolder(String folder)
{
File f = new File(FileConf.resourceTopath + "/" + folder);
// System.out.println(System.getProperty("user.dir") +
// "/webapps/ROOT/resource/" + folder);
File[] files = f.listFiles();
if (f.isDirectory())
{
// It's a directory
if (files.length == 0)
{
// no files
return true;
} else
{
// have files
return false;
}
}
return false;
}
public void doJ2meBuild()
{
// -Dvideo=true -Daudio=true -Dshowmap=true -Dlocalization=el
// -Ddevice=Sony-Ericsson/W900
devicename = access.getJ2meNameByPhoneid(phoneid);
File j2mebuildFile = new File(FileConf.j2meBuildingpath);
Project j2mep = new Project();
DefaultLogger consoleLogger = new DefaultLogger();
consoleLogger.setErrorPrintStream(System.err);
consoleLogger.setOutputPrintStream(System.out);
consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
j2mep.addBuildListener(consoleLogger);
try
{
j2mep.fireBuildStarted();
j2mep.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
helper.parse(j2mep, j2mebuildFile);
// set the para video and audio
if (this.dvideoOraudio.equals("1"))
{
j2mep.setProperty("video", "true");
j2mep.setProperty("audio", "false");
} else if (this.dvideoOraudio.equals("2"))
{
j2mep.setProperty("video", "false");
j2mep.setProperty("audio", "true");
} else if (this.dvideoOraudio.equals("2"))
{
j2mep.setProperty("video", "false");
j2mep.setProperty("audio", "false");
}
// set the para showmap
if (this.dmap.equals("1"))
{
j2mep.setProperty("showmap", "true");
} else if (this.dmap.equals("0"))
{
j2mep.setProperty("showmap", "false");
}
// set the para localization
if (this.language.equals("en_US"))
{
j2mep.setProperty("localization", "en_US");
devicename = devicename + "-en_US-mymytilene";
} else
{
j2mep.setProperty("localization", "el");
devicename = devicename + "-en_GR-mymytilene";
}
// set the name of device
j2mep.setProperty("device", devicename);
// run the j2me build
j2mep.executeTarget("cleanbuild");
// copy the jar file to server root in order to download from page
j2mep.setProperty("jartopath.dir", FileConf.jarToPath);
j2mep.setProperty("jarfrompath.dir", FileConf.jarFromPath);
j2mep.executeTarget("docopyjar");
j2mep.fireBuildFinished(null);
} catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
AntUtil antutil = new AntUtil("jia_daoger@tom.com", "en_US", "101", "1", "1", "1", "1", "1");
antutil.doResourceCopy();
}
}
这是要执行的build文件:
<project name="testproject" default="makefolder" basedir=".">
<property name="toresource.dir" value="" />
<property name="fromresource.dir" value="" />
<!--property name="jar.dir" value="" /-->
<target name="deldir">
<delete includeEmptyDirs="true">
<fileset dir="${toresource.dir}/" />
</delete>
<!--delete>
<fileset dir="${jar.dir}/" />
</delete-->
</target>
<target name="makefolder">
<mkdir dir="${toresource.dir}" />
<mkdir dir="${toresource.dir}/${screenfolder}" />
<!--mkdir dir="${jar.dir}/" /-->
</target>
<target name="makeOrfolder">
<mkdir dir="${toresource.dir}/${dvideoOraudio}" />
</target>
<target name="docopyscreen">
<copy todir="${toresource.dir}/${screenfolder}">
<fileset dir="${fromresource.dir}/${screenfolder}">
<include name="*.*" />
</fileset>
</copy>
</target>
<target name="docopyxml">
<copy todir="${toresource.dir}/${screenfolder}">
<fileset dir="${fromresource.dir}/xml">
<include name="*.xml" />
</fileset>
</copy>
<delete>
<fileset dir="${fromresource.dir}/xml/" />
</delete>
</target>
<target name="docopyimages">
<copy todir="${toresource.dir}/${screenfolder}">
<fileset dir="${fromresource.dir}/images/pics">
<include name="${cont_id}/${cont_id}${screensize}.png" />
</fileset>
</copy>
</target>
<target name="docopyvideo">
<copy todir="${toresource.dir}/video">
<fileset dir="${fromresource.dir}/video">
<include name="${cont_id}/${cont_id}.3gp" />
</fileset>
</copy>
</target>
<target name="docopyaudio">
<copy todir="${toresource.dir}/audio">
<fileset dir="${fromresource.dir}/audio">
<include name="${cont_id}/${cont_id}.mp3" />
</fileset>
</copy>
</target>
<target name="docopynovideo">
<copy todir="${toresource.dir}/video">
<fileset dir="${fromresource.dir}/video">
<include name="novideo.3gp" />
</fileset>
</copy>
</target>
<target name="docopynoaudio">
<copy todir="${toresource.dir}/audio">
<fileset dir="${fromresource.dir}/audio">
<include name="noaudio.mp3" />
</fileset>
</copy>
</target>
<!--target name="doresourcejar">
<jar jarfile="${jar.dir}/${devicename}.jar">
<fileset dir="${toresource.dir}">
<include name="**/*.*" />
</fileset>
</jar>
</target-->
</project>
用于j2me的build.xml:
<project name="Mytilene" default="j2mepolish">
<property file="mytilene.properties" />
<taskdef name="j2mepolish" classname="de.enough.polish.ant.PolishTask" classpath="${polish.home}/lib/enough-j2mepolish-build.jar;${polish.home}/lib/jdom.jar" />
<!-- build targets, each target can be called via "ant [name]",
e.g. "ant clean", "ant test j2mepolish" or just "ant" for calling the default-target -->
<target name="setdeploy" description="Call this target first to set the OTA download-URL, e.g. ant setdeploy j2mepolish">
<property name="deploy-url" value="http://www.company.com/download/" />
</target>
<target name="enableDebug" description="Call this target first to skip the obfuscation step, call the emulator and start the debugger, e.g. ant enableDebug j2mepolish">
<property name="debug" value="true" />
</target>
<target name="test" description="Call this target first to skip the obfuscation step and call the emulator, e.g. ant test j2mepolish">
<property name="test" value="true" />
<property name="dir.work" value="build/test" />
</target>
<target name="init">
<property name="test" value="false" />
<property name="dir.work" value="build/real" />
<property name="deploy-url" value="" />
<property name="resource.dir" value="resources" />
</target>
<!-- start the build with J2ME Polish -->
<target name="j2mepolish">
<j2mepolish>
<info name="${app.name}" vendorName="${app.vendorName}" version="${app.version}" description="${app.description}" jarName="${polish.vendor}-${polish.name}-${polish.locale}-mymytilene.jar" jarUrl="${polish.jarName}" infoUrl="${app.infourl}" icon="${app.icon}" copyright="${app.copyright}" />
<deviceRequirements if="test">
<requirement name="Identifier" value="${device}" />
</deviceRequirements>
<deviceRequirements unless="test">
<requirement name="Identifier" value="${device}" />
</deviceRequirements>
<build usePolishGui="True" fullscreen="menu">
<midlet class="${app.mainClass}" />
<libraries>
<library file="lib/kxml.zip" />
</libraries>
<variables includeAntProperties="true">
<variable name="polish.MenuBar.useExtendedMenuBar" value="true" />
<variable name="polish.useScrollBar" value="true" />
</variables>
<resources dir="resources" defaultexcludes="yes" excludes="readme.txt, *.definition">
<fileset dir="resources/video" includes="*.3gp" if="${video}" />
<fileset dir="resources/audio" includes="*.mp3" if="${audio}" />
<fileset dir="resources/map" includes="*.png" if="${showmap}" />
<localization>
<locale name="${localization}" encoding="utf-8" unless="test" />
</localization>
</resources>
<obfuscator name="ProGuard">
<keep class="${app.mainClass}" />
<parameter name="optimize" value="false" />
</obfuscator>
<packager name="7zip">
<parameter name="compression" value="maximum" />
<parameter name="passes" value="4" />
<parameter name="fastbytes" value="212" />
</packager>
</build>
<!--<emulator /> -->
</j2mepolish>
</target>
<target name="clean" description="allows a clean build. You should call [ant clean] whenever you made changes to devices.xml, vendors.xml or groups.xml">
<delete dir="build" />
<delete dir="dist" includes="**/*" />
</target>
<target name="cleanbuild" description="allows a clean build. You should call [ant cleanbuild] whenever you made changes to devices.xml, vendors.xml or groups.xml" depends="clean, j2mepolish" />
<target name="debug" description="debugs the project" depends="enableDebug, test, j2mepolish" />
<!-- add by jia-->
<target name="docopyjar" description="copy the j2me jar to server root dictory ">
<copy todir="${jartopath.dir}">
<fileset dir="${jarfrompath.dir}">
<include name="*.jar" />
</fileset>
</copy>
</target>
</project>