These days I spent some time learning
Groovy. Why I learn Groovy? Because I want to use
Gant. Gant = Groovy + Ant. Gant is really powerful, since you can use Groovy script and Ant API to write your build script instead of XML. I struggle a long time in Ant, because I have to use Ant-Contrib plugin, and it is so hard to write an condition block and even for loop, it is much easier to do that in Gant script.
Now I am going to share the gant script of building J2ME app, this is just basic, but you can see the power of Gant.
Requirement:
1. You need to install Groovy and Gant,
here is the link, I used 1.9.3 in Mac.
2. download the latest version of antenna,
here is the download link, I used 1.2.1-beta.
3. install the JavaME SDK, I choose JavaME SDK 3, since it works on Mac.
The J2ME MIDlet class source code:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class HelloWorld extends MIDlet implements CommandListener {
private final Command exitCommand;
private final Form form;
public HelloWorld() {
exitCommand = new Command("Exit", Command.EXIT, 1);
form = new Form("Hello World!");
form.append("from Steve");
form.addCommand(exitCommand);
form.setCommandListener(this);
}
protected void startApp() {
Display.getDisplay(this).setCurrent(form);
}
protected void pauseApp() {}
protected void destroyApp(boolean force) {}
public void commandAction(Command cmd, Displayable disp) {
if (cmd == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
}
}
The Ant script looks like:
<project name="antenna-test" default="jar" basedir=".">
<taskdef resource="antenna.properties" classpath="./antenna-bin-1.2.1-beta.jar" />
<property name="wtk.home" value="/Users/Steve/dev/wtk252" />
<property name="wtk.midp.version" value="2.0" />
<property name="wtk.cldc.version" value="1.1" />
<property name="src.dir" location="src" />
<property name="out.dir" location="out" />
<property name="dist.dir" location="dist" />
<property name="preverified.dir" location="preverified" />
<!-- Regular targets -->
<target name="clean" description="cleans the target directory">
<delete dir="${out.dir}"/>
<delete dir="${dist.dir}"/>
<delete dir="${preverified.dir}"/>
</target>
<target name="compile" depends="init">
<wtkbuild source="1.3" srcdir="${src.dir}" destdir="${out.dir}">
</wtkbuild>
</target>
<target name="preverify" depends="compile">
<wtkpreverify srcdir="${out.dir}" destdir="${preverified.dir}">
</wtkpreverify>
</target>
<target name="jar" depends="compile,jad,preverify">
<wtkpackage jarfile="${dist.dir}/helloworld.jar"
jadfile="${dist.dir}/helloworld.jad"
obfuscate="false"
preverify="false">
<fileset dir="${out.dir}"/>
</wtkpackage>
</target>
<target name="jad" depends="compile">
<wtkjad version="0.01" name="hello" vendor="foo" jadfile="${dist.dir}/helloworld.jad" jarfile="${dist.dir}/helloworld.jar">
<midlet name="hello" class="HelloWorld">
</midlet>
</wtkjad>
</target>
<target name="init" description="initialization for properties and paths" depends="clean">
<buildnumber/>
<mkdir dir="${out.dir}"/>
<mkdir dir="${dist.dir}"/>
<mkdir dir="${preverified.dir}" />
</target>
</project>
the build script build.gant:
ant.taskdef(resource:"antenna.properties", classpath:"./antenna-bin-1.2.1-beta.jar")
ant.property(name:"wtk.home", value:"/Applications/Java_ME_SDK_3.0.app/Contents/Resources")
ant.property(name:"wtk.midp.version", value:"2.0")
ant.property(name:"wtk.cldc.version", value:"1.1")
final srcDir = "src"
final outDir = 'out'
final distDir = 'dist'
final preverifiedDir = 'preverified'
//clean target
includeTargets << gant.targets.Clean
cleanPattern << '**/*~'
cleanDirectory << [ outDir, distDir, preverifiedDir]
target(name:"init", description:"initialization") {
depends(clean)
/*buildnumber() */
mkdir(dir:outDir)
mkdir(dir:distDir)
mkdir(dir:preverifiedDir)
}
target(name:"compile", description:"compile") {
depends(init)
wtkbuild( source:"1.3", srcdir:srcDir, destdir:outDir)
}
target(name:"preverify", description:"preverify") {
depends(compile)
wtkpreverify(srcdir:outDir, destdir:preverifiedDir)
}
//target jad file
target(name:"jad", description:"generate jad file") {
depends(compile)
wtkjad( version:"0.01", name:'hello', vendor:"foo", jadfile:"${distDir}/helloworld.jad", jarfile:"${distDir}/helloworld.jar" ) {
midlet(name:"hello", class:"HelloWorld")
}
}
//target jar
target(name:"jar", description:"generating jar file") {
depends(compile,jad)
wtkpackage( jadfile:"${distDir}/helloworld.jad", jarfile:"${distDir}/helloworld.jar", obfuscate:"false", preverify:"false") {
fileset(dir:outDir)
}
}
//target obfuscate
//target all
target(name:"all", description: "build the whole app") {
depends(jar)
}
setDefaultTarget(all)
From the above script, you can see how easy to write gant build script.
This is only the basic implementation, I am going to post more Gant based script in future.
In future I am going to consider:
1. How to integrate with ANT script;
2. How import multiple Gant script file.