Saturday, May 16, 2009

Development branches

How can I evolve two different versions of the same application? Using the same source code? For example, if one version uses MIDP 2.0 but the other uses MIDP 1.0? The answer is preprocessing.

Since October/2003 I use Antenna: an Ant plugin that allows a developer to easily build, preverify and package a Java ME application, but it also has a pre-processor built in. To use it you need to add special line comments in your source code and call the wtkpreprocess task before calling the build task.
Below is an example of such line comments:

//#ifdef midp20
list.setBackgroundColor(display.getColor(Display.COLOR_BACKGROUND));
//#endif

ifdef means If Defined.
My build.xml Ant script will have the following lines:

<wtkpreprocess src="./src" dest="./prep" symbols="midp20"/>
<wtkbuild src="./prep" dest="./bin"/>

With this the preprocessor will match the informed symbol "midp20" with the ifdef value and keep the original code unchanged.
To differ between the two versions I will need two targets in build.xml. One for MIDP 2.0 and the other for MIDP 1.0. The first will use the sample above, but the target for MIDP 1.0 will use:

<wtkpreprocess src="./src" dest="./prep"/>
<wtkbuild src="./prep" dest="./bin"/>

Without any symbols the preprocessor will change the source code and add comment lines between ifdef and endif. The changed code at prep folder will look like this:

//#ifdef midp20
//list.setBackgroundColor(display.getColor(Display.COLOR_BACKGROUND));
//#endif

You can also use preprocessing with NetBeans Mobility Pack and Eclipse MTJ (EclipseME has moved to this project).

Related Topics:

No comments: