Thursday, May 27, 2010

LWUIT blackberry ANT script refactored 2

Last time I posted the refactored blackberry build script, today I made some other small improvements:

1. Add a "run" target, support running both blackberry and regular J2ME simulator.
<target name="run" depends="init,jar,bb-run,cldc-run">
     
    <target name="cldc-run" unless="do.rapc" >
       <nb-run jadfile="${dist.dir}/${dist.jad}" 
                  jarfile="${dist.dir}/${dist.jar}" jadurl="${dist.jad.url}" 
                  device="${platform.device}" platformhome="${platform.home}" 
                  platformtype="${platform.type}" 
                  execmethod="${run.method}" 
                  securitydomain="${evaluated.run.security.domain}" 
                  commandline="${platform.runcommandline}" 
                  classpath="${platform.bootclasspath}:${dist.dir}/${dist.jar}" 
                  cmdoptions="${run.cmd.options}"/>
    </target>
    
    <target name="bb-run" depends="copyBBSimulator" if="do.rapc">
     <exec os="Windows NT Windows 95 Windows 98 Windows 2000 Windows XP"
             dir="${platform.home}/simulator"
            executable="${platform.home}/simulator/${platform.device}.bat"
        failonerror="true" resolveExecutable="true"/>
   </target>
   The above code snipplet means, when user press "run" button in NetBeans IDE, if the current configuration is blackberry, then launch the blackberry simulator, else launch the regular J2ME simulator.

2. Separate the blackberry specific ant targets into aother build script file, make the build file generic and easier to use.
At the beginning I tried to put all the targets in a separate file: bb-build.xml, then import it in the build.xml, but I found the netbeans custom targets like post-init, post-jar are not called, it seems these targets needs to be explicit specified in the build.xml.
I found another solution - embedding the xml snippet in the build.xml using XML entity, based on this link:
 for exmaple, like this:
<!DOCTYPE project [
       <!ENTITY bb-common SYSTEM "bb-build.xml">
    ]>

   &bb-common;
 The above snippet is equivlent of copying the code from bb-build.xml into build.xml.  

Benefits:
The new refactored build script becomes generic, does not specific to lwuit project, can apply to any blackberry CLDC app.

I attach the full build script here, and you can use them as template for your blackberry build script in your NetBeans project.
1) build.xml
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE project [
       <!ENTITY bb-common SYSTEM "bb-build.xml">
]>

<project name="myApp" default="jar" basedir=".">
    <description>Builds, tests, and runs the project .</description>
    <import file="nbproject/build-impl.xml"/>
   
    <property name="app.title" value="myApp" />
    <property name="app.version" value="1" />
    <property name="app.icon"   value="/icon.png" />
    <property name="app.vendor" value="foo" />

    &bb-common;
 </project>

2) bb-build.xml, the blackberry build script snippet
<target name="post-init">
          <available file="${platform.home}/bin/rapc.exe" property="do.rapc" />
          <condition property="jpda.port" value="8000">
              <isset property="do.rapc">
              </isset>
         </condition>
         
  </target>


   <target name="post-jar" if="do.rapc">
     <antcall target="bbbuild" />
   </target>

   <target name="run" depends="init,jar,bb-run,cldc-run">

</target>

   <target name="cldc-run" unless="do.rapc" >
       <nb-run jadfile="${dist.dir}/${dist.jad}" jarfile="${dist.dir}/${dist.jar}" jadurl="${dist.jad.url}" device="${platform.device}" platformhome="${platform.home}" platformtype="${platform.type}" execmethod="${run.method}" securitydomain="${evaluated.run.security.domain}" commandline="${platform.runcommandline}" classpath="${platform.bootclasspath}:${dist.dir}/${dist.jar}" cmdoptions="${run.cmd.options}"/>
   </target>

<target name="post-clean">
   <delete failonerror="false">
      <fileset dir="${platform.home}/simulator">
       <include name="**/${name}.*">
       </include>
      </fileset>
   </delete>
</target>

<typedef resource="bb-ant-defs.xml" classpath="bb-ant-tools.jar" />
  <target name="bbbuild" description="blackberry build" depends="init" if="do.rapc">
          <echo message="rapc build, dir=${dist.dir}"></echo>
          <mkdir dir="${dist.dir}/bbant" />
          <rapc verbose="true" output="${name}" jdehome="${platform.home}" import="${platform.bootclasspath}" destdir="${dist.dir}/bbant/" noconvert="true">
          <src>
             <fileset file="${dist.dir}/${dist.jar}" />
          </src>
          <jdp title="${app.title}" vendor="${app.vendor}" version="${app.version}" type="cldc" icon="${app.icon}" >
          </jdp>
          </rapc>

        <!-- sigtool jdehome="C:Program FilesResearch In MotionBlackBerry JDE 4.7.0" codfile="${dist.dir}/bbant/${name}.cod" password="" / -->
        <alx destdir="${dist.dir}/bbant" filename="${name}.alx">
            <application id="${app.title}">
               <codset>
                    <fileset dir="${dist.dir}/bbant" includes="*.cod">
                   </fileset>
               </codset>
           </application>
       </alx>
       <mkdir dir="${dist.dir}/bbant-final" />
       <jadtool input="${dist.dir}/bbant/${dist.jad}" destdir="${dist.dir}/bbant-final">
        <fileset dir="${dist.dir}/bbant" includes="*.cod">
        </fileset>
       </jadtool>
 </target>

<target name="copyBBSimulator" if="do.rapc">
    <copy todir="${platform.home}/simulator" verbose="true">
            <fileset dir="${dist.dir}/bbant">
                  <include name="*.cod"/>
                  <include name="*.jad" />
            </fileset>
    </copy>

</target>

<target name="bb-run" depends="copyBBSimulator" if="do.rapc">
    <exec os="Windows NT Windows 95 Windows 98 Windows 2000 Windows XP"
             dir="${platform.home}/simulator"
            executable="${platform.home}/simulator/${platform.device}.bat"
        failonerror="true" resolveExecutable="true"/>
</target>

Next Step:
Add a target support debugging blackberry Application inside the NetBeans IDE.

Tuesday, May 18, 2010

LWUIT BlackBerry ANT script refactored

My last article about using ANT script to build LWUIT blackberry application was intereted by some people, which is encouraging. I spent some time to review the ant script again and made some improvements. Here I posted my updated build script, it is cleaner and easier to use, you can just use it as template, copy and paste it in your NetBeans project by just configure some parameters.

<property name="app.title" value="helloLWUIT" />
    <property name="app.version" value="1" />
    <property name="app.icon"   value="/icon.png" />
    <property name="app.vendor" value="foo" />

    <target name="post-init">
          <available file="${platform.home}/bin/rapc.exe" property="do.rapc" />
          <condition property="jpda.port" value="8000">
              <isset property="do.rapc">
              </isset>
         </condition>
    </target>

  
   <target name="post-jar" if="do.rapc">
     <antcall target="bbbuild" />
   </target>
  

<target name="post-clean">
   <delete failonerror="false">
      <fileset dir="${platform.home}/simulator">
       <include name="**/${name}.*">
       </include>
      </fileset>
   </delete>
</target>

<typedef resource="bb-ant-defs.xml" classpath="bb-ant-tools.jar" />
  <target name="bbbuild" description="blackberry build" depends="init">
          <echo message="rapc build, dir=${dist.dir}"></echo>
          <mkdir dir="${dist.dir}/bbant" />
          <rapc verbose="true" output="${name}" jdehome="${platform.home}" import="${platform.bootclasspath}" destdir="${dist.dir}/bbant/" noconvert="true">
          <src>
             <fileset file="${dist.dir}/${dist.jar}" />
          </src>
          <jdp title="${app.title}" vendor="${app.vendor}" version="${app.version}" type="cldc" icon="${app.icon}" >
          </jdp>
          </rapc>

        <!-- sigtool jdehome="C:Program FilesResearch In MotionBlackBerry JDE 4.7.0" codfile="${dist.dir}/bbant/${name}.cod" password="" / -->
        <alx destdir="${dist.dir}/bbant" filename="${name}.alx">
            <application id="${app.title}">
               <codset>
                    <fileset dir="${dist.dir}/bbant" includes="*.cod">
                   </fileset>
               </codset>
           </application>
       </alx>
       <mkdir dir="${dist.dir}/bbant-final" />
       <jadtool input="${dist.dir}/bbant/${dist.jad}" destdir="${dist.dir}/bbant-final">
        <fileset dir="${dist.dir}/bbant" includes="*.cod">
        </fileset>
       </jadtool>
 </target>

<target name="copyBBSimulator" depends="bbbuild" if="do.rapc">
    <copy todir="${platform.home}/simulator" verbose="true">
            <fileset dir="${dist.dir}/bbant">
                  <include name="*.cod"/>
                  <include name="*.jad" />
            </fileset>
    </copy>
</target>

<target name="bb-run" depends="copyBBSimulator">
    <exec os="Windows NT Windows 95 Windows 98 Windows 2000 Windows XP"
             dir="${platform.home}/simulator"
            executable="${platform.home}/simulator/${platform.device}.bat"
        failonerror="true" resolveExecutable="true"/>
</target>

It has following improvements:
    1. Separate out the customized parameters in the build scriciptThen entire build script only needs following customized parameters: app.title, app.version, app.icon, app.vendor. You only need to configure them with your own application parameters.
    2. Simplify the build script.  In the old build script, you have to use target "copyBBTouchSources" and "copyBBSources" to copy and update the LWUIT library source code into specific folder, then call rapc compiler to build the entire application. But when you investigate a little bit deeper, since NetBeans IDE already generate jar file for you, which already included the preprocessed application and the LWUIT blackbery library class files, why not just pass this jar file to rapc and then generate the cod file? based on this idea, I modified the target "bbbuild" and it works.
    3. One build script supports both BlackBerry touch and non-touch build. since the build script is simplified and only use jar file to generate cod file,the touch or non-touch specific files are already embedded in the jar file, the build script itself becomes generic. To tested it, I installed JDE 4.5 and JDE 4.7 two different JDE in the NetBeans, and set up 4.5 and 4.7 different configurations in my project, and I found both works.
    4. Added the ant target "bb-run", which allow you run the application in the blackberry simulator. To make it work, you had better to check the nbproject\project.properties file, if the simualtor device name is configured properly, In JDE 4.7, the device name is 9500, like this:




      configs.BlackBerryTouch.platform.device=9500
      In 4.5, the device name is 8320, like this:




      configs.BlackBerry.platform.device=8320
    Update: Sample project
    Please click here to download the sample project.

    Tuesday, May 4, 2010

    Why should managers embrace agile?

    When you look at the articles about Agile/Scrum/Lean, they are all about self-organizing, decentralized, democracy, etc.Those are welcomed by the developers but conflict with managers benefit, because our conventional management is about hierarchy, command and control, chasing the power, the manager cares more about how his boss think instead of how his subordinates think. For managers, embracing agile means loosing power, weaken their benefit, that is why manager don't like agile.
    On the other hand, agile is about changing, changing means risky, but the managers care more about the stability of their position, their power, they don't have much enthusiasm to make changes.

    So we have a conclusion that developer and manager like two different classes, developers like agile because it brings them benefits; managers dislike agile because it will lose managers' benefit. The progress of agile movement is quite similar with the progress of human's democracy movement.

    This sounds discouraging, but this is the fact, that will explain why agile has not become mainstream after 10 years of its movement.Then what can we do? Try to educate as many developers as possible, gradually if agile principles are accepted by most people, then it is quite possible to force the managers make changes. This will take time, but please remember that the manager won't truely embrace agile,just like no ruler will truely embrace democracy.

    Saturday, May 1, 2010

    ANT build script for LWUIT blackberry application

    Update:
      This article is more than a year old, now LWUIT project uses a new tool to build BlackBerry application - NetBeans BlackBerry Plugin, please refer my two latest post:

      Developing Blackberry application using NetBeans Plugin - part 1, first impression
      Developing Blackberry application using NetBeans Plugin - part 2, fixing debug issue


    Shai's article about build script for LWUIT in BlackBerry is great, but it has so many errors,I spend two days trying to fix the errors, and finally make it work.

    Here I posted my modified ant buil.xml and application source code. The application is very simple, just display a "Hello World" text. Since this article is based on Shai's article, for details such as project set up, configuration and tools installation etc, please check his article.

    application source code
    HelloLWUITMidlet.java

    import com.sun.lwuit.Display;
    import com.sun.lwuit.Form;
    import com.sun.lwuit.Label;
    import com.sun.lwuit.layouts.BorderLayout;
    
    
    public class HelloLWUITMidlet extends
    //#ifdef RIM
         net.rim.device.api.ui.UiApplication
    //#else
    //#      javax.microedition.midlet.MIDlet
    //#endif
     {
        public void startApp() {
    
            Display.init(this);
            Form f = new Form();
            f.setTitle("Hello World");
            f.setLayout(new BorderLayout());
            f.addComponent("Center",
            new Label("I am a Label"));
            f.show();
    
        }
    //#ifdef BlackBerry
       public static void main(String[] argv)
       {
           new HelloLWUITMidlet().startApp();
       }
    
       public void notifyDestroyed()
       {
           System.exit(0);
       }
    
       public void platformRequest(String s) {
          net.rim.blackberry.api.browser.Browser.getDefaultSession().displayPage(s);
         }
    //#endif
    
    
        public void pauseApp() {
        }
    
        public void destroyApp(boolean unconditional) {
        }
    }
    

    ANT build script
    <?xml version="1.0" encoding="UTF-8"?>
    <!-- You may freely edit this file. See commented blocks below for -->
    <!-- some examples of how to customize the build. -->
    <!-- (If you delete it and reopen the project it will be recreated.) -->
    <project name="helloLWUIT" default="jar" basedir=".">
        <description>Builds, tests, and runs the project .</description>
        <import file="nbproject/build-impl.xml"/>
       
    
       
         <target name="post-init">
            <available file="${platform.home}/bin/rapc.exe" property="do.rapc" />
            <available file="${platform.home}/simulator/9500.bat" property="bbtouch" />
            <condition property="jpda.port" value="8000">
               <isset property="do.rapc">
               </isset>
            </condition>
         </target>
    
    
            
            <target name="post-preprocess" depends="copyBBSources,copyBBTouchSources">
            </target>
            
    
           <typedef resource="bb-ant-defs.xml" classpath="bb-ant-tools.jar" />
           <target name="bbbuild" description="blackberry build" depends="init,copyBBSources,copyBBTouchSources">
              <echo message="Compiling ${preprocessed.dir}"></echo>
              <mkdir dir="${dist.dir}/bbant" />
              <rapc verbose="true" output="${name}" jdehome="${platform.home}" import="${platform.bootclasspath}" destdir="${dist.dir}/bbant/" noconvert="true">
              <src location="${basedir}/${preprocessed.dir}">
              </src>
              <jdp title="helloLWUIT" version="1" type="cldc" icon="/icon.png" >
              </jdp>
              </rapc>
              
            <!-- sigtool jdehome="C:\Program Files\Research In Motion\BlackBerry JDE 4.7.0" codfile="${dist.dir}/bbant/${name}.cod" password="" / -->
            <alx destdir="${dist.dir}/bbant" filename="${name}.alx">
            <application id="helloLWUIT">
           <codset>
            <fileset dir="${dist.dir}/bbant" includes="*.cod">
           </fileset>
           </codset>
           </application>
           </alx>
    
          
           <mkdir dir="${dist.dir}/bbant-final" />
           <jadtool description="desc" id="appId" input="${dist.dir}/bbant/${dist.jad}" destdir="${dist.dir}/bbant-final">
            <fileset dir="${dist.dir}/bbant" includes="*.cod">
            </fileset>
           </jadtool>
           
          </target>
    
     <target name="copyBBTouchSources" if="bbtouch">
     <echo message="Copying blackberry touch sources"></echo>
     <copydir forceoverwrite="true" src="${project.BlackberryPort}/build/touch/preprocessed" dest="${basedir}/${preprocessed.dir}" />
     <touch file="${basedir}/${preprocessed.dir}/com/sun/lwuit/M3G.java" />
     <touch file="${basedir}/${preprocessed.dir}/com/sun/lwuit/SVGImage.java" />
     <touch file="${basedir}/${preprocessed.dir}/com/sun/lwuit/animations/Transition3D.java" />
     <touch file="${basedir}/${preprocessed.dir}/com/sun/lwuit/impl/midp/SVGImplementation.java" />
     
     </target>
    
     <target name="copyBBSources" if="do.rapc">
       <echo message="Copying blackberry sources ${preprocessed.dir}"></echo>
       <copydir src="${project.LWUIT}/src" dest="${basedir}/${preprocessed.dir}" />
       <copydir forceoverwrite="true" src="${project.BlackberryPort}/build/preprocessed" dest="${basedir}/${preprocessed.dir}" />
       <touch file="${basedir}/${preprocessed.dir}/com/sun/lwuit/M3G.java"/>
       <touch file="${basedir}/${preprocessed.dir}/com/sun/lwuit/SVGImage.java"/>
       <touch file="${basedir}/${preprocessed.dir}/com/sun/lwuit/animations/Transition3D.java"/>
       <touch file="${basedir}/${preprocessed.dir}/com/sun/lwuit/impl/midp/SVGImplementation.java"/>
     </target>
    
     <target name="post-jar" if="do.rapc">
       <rapc verbose="true" output="${name}" jdehome="${platform.home}" import="${platform.bootclasspath}" destdir="${platform.home}/simulator/" noconvert="true">
         <src location="${basedir}/${preprocessed.dir}">
         </src>
         
         <jdp title="helloLWUIT" version="1" type="cldc" icon="/icon.png" >
         </jdp>
       </rapc>
     </target>
     
     <target name="post-clean">
        <delete failonerror="false">
           <fileset dir="${platform.home}/simulator">
            <include name="**/${name}.*">
            </include>
           </fileset>
        </delete>
     </target>
    
    </project>
    


    Note:
    This build script is for touch screen build, for non touch sreen build you need to
    1.Comment out "bbtouch" property in target "post-init" :
    <!-- <available file="${platform.home}/simulator/9500.bat" property="bbtouch">  --> </available>
    2.Remove the "copyBBTouchSources" dependency in target  "bbbuild".