Saturday, October 16, 2010

Gant Script: print ant default properties

If you want to know exactly what kind of ant properties in your environment, try the following gant target:

target(properties : "print ant default properties") {
     println "ant properties are:" { key, value ->
         println "${key} = ${value}"

Here are some of the out put:
ant properties are:
ant.version = Apache Ant version 1.8.1 compiled on April 30 2010
sun.os.patch.level = unknown
java.vm.vendor = Apple Inc.
environment.HOME = /Users/steve
environment.TERM_PROGRAM = Apple_Terminal
file.encoding = MacRoman
user.language = en
java.class.path = /opt/local/share/java/gant/lib/groovy-all-1.7.3.jar
file.separator = / = Java(TM) SE Runtime Environment
basedir = /Users/steve/dev/gant
java.vm.specification.version = 1.0
awt.nativeDoubleBuffering = true


Look how easy it work in the Groovy/Gant script, I am sure it is much harder to write in ant xml build script, and you have to use ant-contrib to implement the for loop!  Byebye XML!

Build J2ME app using Gant

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.

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");
    protected void startApp() {
    protected void pauseApp() {}
    protected void destroyApp(boolean force) {}
    public void commandAction(Command cmd, Displayable disp) {
        if (cmd == exitCommand) {

The Ant script looks like:

<project name="antenna-test" default="jar" basedir=".">
 <taskdef resource="" 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 name="compile" depends="init">
  <wtkbuild source="1.3" srcdir="${src.dir}" destdir="${out.dir}">
 <target name="preverify" depends="compile">
  <wtkpreverify srcdir="${out.dir}" destdir="${preverified.dir}">
 <target name="jar" depends="compile,jad,preverify">
  <wtkpackage jarfile="${dist.dir}/helloworld.jar"

       <fileset dir="${out.dir}"/>
 <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">
 <target name="init" description="initialization for properties and paths" depends="clean">
  <mkdir dir="${out.dir}"/>
  <mkdir dir="${dist.dir}"/>
  <mkdir dir="${preverified.dir}" />

the build script build.gant:

ant.taskdef(resource:"", classpath:"./antenna-bin-1.2.1-beta.jar")"wtk.home", value:"/Applications/")"wtk.midp.version", value:"2.0")"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") {
 /*buildnumber() */

target(name:"compile", description:"compile") {
 wtkbuild( source:"1.3", srcdir:srcDir, destdir:outDir)

target(name:"preverify", description:"preverify") {
 wtkpreverify(srcdir:outDir, destdir:preverifiedDir)

//target jad file

target(name:"jad", description:"generate jad file") {
 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") {
 wtkpackage( jadfile:"${distDir}/helloworld.jad", jarfile:"${distDir}/helloworld.jar", obfuscate:"false", preverify:"false")  {

//target obfuscate

//target all
target(name:"all", description: "build the whole app") {


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.