·  ANT – Examples – Compile, jar, jad & run application

 

- Info:

  - This example show how to create complete ANT script for software deployment.

 

- Procedure:

 

  - Manualy create directories:

    - building

    - building\ant

    - building\templates

    - res

    - src    

 

  - In ant directory create:

    - build.xml

    - FirstTime.xml

    - Repeated.xml

    - build.properties.

 

  - In templates directory create:

    - MANIFEST.MF

    - Template.jad

    - filesize.jar

 

  - In src directory copy file:

    - Barbarian.java

 

  - Define package names in:

    - MANIFEST.MF

    - Template.jad

    - MIDlet-1: @name@, @name@.png, @name@        (for no package)

    - MIDlet-1: @name@, @name@.png, com.@name@      (for com package)

 

  - In build.properties define name for your midled. Should be the same as class name.

 

  - Check package names in source files.

 

  - Start ANT scripts in following order:

    - FirstTime.xml (one time, only builds directories and MANIFEST)

    - Repeated.xml   (while developing, uses reduced code)

    - build.xml (when making release version, obfuscates)

 

- Files:

 

    Barbarian.java

    import javax.microedition.midlet.MIDlet;  

    import javax.microedition.lcdui.Display;   

    import javax.microedition.lcdui.TextBox;

    

    public class Barbarian extends MIDlet {

    

      public void startApp(){

        TextBox my = new TextBox("MY TITLE","HELLO WORLD",256,0);

        Display.getDisplay(this).setCurrent(my);

      }

    

      public void pauseApp(){}

      

      public void destroyApp(boolean b){}

    

      void exitRequested(){

        destroyApp(false);

        notifyDestroyed();

      }

    }

    

    MANIFEST.MF

    MIDlet-1: @name@, @name@.png, @name@

    MIDlet-Name: @name@

    MIDlet-Vendor: Sun Microsystems

    MIDlet-Version: @version@

    MicroEdition-Configuration: CLDC-1.0

    MicroEdition-Profile: MIDP-1.0

    

    Template.jad

    MIDlet-1: @name@, @name@.png, @name@

    MIDlet-Jar-Size: @jarSize@

    MIDlet-Jar-URL: @name@.jar

    MIDlet-Name: @name@

    MIDlet-Vendor: Sun Microsystems

    MIDlet-Version: @version@

    MicroEdition-Configuration: CLDC-1.0

    MicroEdition-Profile: MIDP-1.0

 

    build.properties

     name    = Barbarian

     version = 0.1

     device  = D:/ProgramFiles/WirelessToolkit20/wtklib/devices/Nokia_7210_MIDP_SDK_v1_0

 

    build.xml

    <?xml version="1.0" encoding="UTF-8"?>

    <project basedir="." default="all" name="MyAntJava">

    

      <!-- ======================= INIT ================================== -->

      <property     file        = "build.properties"/>

    

      <property     name        = "lib"

                    value       = "${device}\lib\classes.zip"/>

                

      <property     name        = "jarSize"

                    value       = "0"/>            

                

      <!-- FOR CALCULATING FILE SIZE -->

      <taskdef      name        = "filesize"

                    classname   = "ant.FileSizeTask"

                    classpath   = "..\templates\filesize.jar"/>            

    

      <!-- TARGETS FLOW  -->

      <target       name        = "all"

                    depends     =     "buildingStructure,compile,preverify,copyManifest,jar,obfuscate,unjar,preverifyObfuscated,jarObfuscated    ,copyJad,copyJar,run"> 

      </target>  

      

      <!-- ========================= CREATE JAD ============================= -->

        

      <!-- BUILDING STRUCTURE -->

      <target       name        = "buildingStructure">

    

        <mkdir      dir         = "..\s1_compile"/>

        <mkdir      dir         = "..\s2_preverify"/>

        <mkdir      dir         = "..\s3_copyManifest"/>

        <mkdir      dir         = "..\s4_jar"/>

        <mkdir      dir         = "..\s5_obfuscate"/>

        <mkdir      dir         = "..\s6_unjar"/>   

        <mkdir      dir         = "..\s7_preverifyObfuscated"/>   

        <mkdir      dir         = "..\s8_jarObfuscated"/>

      

        <delete>

          <fileset  dir         = "..\s1_compile"              includes="**\*"/>

          <fileset  dir         = "..\s2_preverify"            includes="**\*"/>

          <fileset  dir         = "..\s3_copyManifest"         includes="**\*"/>

          <fileset  dir         = "..\s4_jar"                  includes="**\*"/>

          <fileset  dir         = "..\s5_obfuscate"            includes="**\*"/>

          <fileset  dir         = "..\s6_unjar"                includes="**\*"/>

          <fileset  dir         = "..\s7_preverifyObfuscated"  includes="**\*"/>

          <fileset  dir         = "..\s8_jarObfuscated"        includes="**\*"/>     

        </delete>      

      

      </target>    

    

      <!-- COMPILE --> 

      <target       name        = "compile">

        <javac      classpath   = "${lib}"                

                    srcdir      = "..\..\src"

                    destdir     = "..\s1_compile" />

      </target>  

    

      <!-- PREVERIFY --> 

      <target       name        = "preverify">

        <exec       executable  = "${device}\bin\preverify">

            <arg    line        = "-classpath ${lib}"/>

            <arg    line        = "-d ..\s2_preverify"/>

            <arg    line        = "..\s1_compile"/>

        </exec>

      </target> 

      

      <!-- COPY MANIFEST -->   

      <target       name        = "copyManifest">

    

        <filter     token       = "name"

                    value       = "${name}"/>

    

        <filter     token       = "version"

                    value       = "${version}"/>

    

        <copy       file        = "..\templates\MANIFEST_template.MF"

                    tofile      = "..\s3_manifest\MANIFEST.MF"

                    overwrite   = "yes"       

                    filtering   = "true"/>

      </target>   

      

      <!-- JAR -->        

      <target       name        = "jar">

        <jar        basedir     = "..\s2_preverify"

                    jarfile     = "..\s4_jar\${name}.jar"

                    manifest    = "..\s3_manifest\MANIFEST.MF"> 

          <fileset  dir         = "..\..\res"/> 

        </jar>

      </target>     

      

      <!-- OBFUSCATE JAR -->  

      <target       name        = "obfuscate">

        <java       fork        = "yes" classname="proguard.ProGuard" classpath="${device}\bin\proguard.jar">

          <arg      line        = "-libraryjars ${device}\lib\classes.zip"/>

          <arg      line        = "-injars ..\s4_jar\${name}.jar"/>

          <arg      line        = "-outjar ..\s5_obfuscate\${name}.jar"/>

          <arg      line        = "-keep 'public class * extends javax.microedition.midlet.MIDlet'"/>

        </java>

      </target>   

    

      <!-- UNJAR -->    

      <target       name        = "unjar"> 

        <unjar      src         = "..\s5_obfuscate\${name}.jar"

                    dest        = "..\s6_unjar" />

      </target>   

      

      <!-- PREVERIFY OBFUSCATED CLASSES --> 

      <target   name        = "preverifyObfuscated">

        <exec       executable  = "${device}\bin\preverify">

          <arg      line        = "-classpath ${lib}"/>

          <arg      line        = "-d ..\s7_preverifyObfuscated"/>

          <arg      line        = "..\s6_unjar"/>

        </exec>

      </target>    

      

      <!-- JAR OBF -->   

      <target       name        = "jarObfuscated">

        <jar        basedir     = "..\s7_preverifyObfuscated"

                    jarfile     = "..\s8_jarObfuscated\${name}.jar"

                    manifest    = "..\s3_manifest\MANIFEST.MF">

          <fileset  dir         = "..\..\res"/>

        </jar>

      </target>    

           

      <!-- COPY JAD -->   

      <target       name        = "copyJad">

    

        <filesize   file        = "..\s8_jarObfuscated\${name}.jar"

                    property    = "jarSize"/>

    

        <filter     token       = "name"

                    value       = "${name}"/>

    

        <filter     token       = "jarSize"

                    value       = "${jarSize}"/>

    

        <filter     token       = "version"

                    value       = "${version}"/>

    

        <copy       file        = "..\templates\Template.jad"

                    tofile      = "..\bin\${name}.jad"

                    overwrite   = "yes"       

                    filtering   = "true"/>

      </target> 

      

      <!-- COPY JAR --> 

      <target       name        = "copyJar">

        <copy       file        = "..\s8_jarObfuscated\${name}.jar"

                    tofile      = "..\bin\${name}.jar"

                    overwrite   = "yes"/>       

      </target>     

          

      <!-- ========================= RUN ============================= -->

            

      <!-- RUN -->       

      <target       name        = "run">

        <exec       executable  = "${device}\bin\7210.exe">   

          <arg      value       = "..\bin\${name}.jad"/>

        </exec>   

      </target>     

      

    </project>             

             

 

    FirstTime.xml

    <?xml version="1.0" encoding="UTF-8"?>

    <project basedir="." default="all" name="MyAntJava">

    

      <!-- PROPERTY FILE -->

      <property     file        = "build.properties"/> 

      

      <!-- TARGETS FLOW  -->

      <target       name        = "all"

                    depends     = "buildingStructure,copyManifest"> 

      </target>    

        

      <!-- BUILDING STRUCTURE -->

      <target       name        = "buildingStructure">

    

        <mkdir      dir         = "..\s1_compile"/>

        <mkdir      dir         = "..\s2_preverify"/>

        <mkdir      dir         = "..\s3_copyManifest"/>

        <mkdir      dir         = "..\s4_jar"/>

        <mkdir      dir         = "..\s5_obfuscate"/>

        <mkdir      dir         = "..\s6_unjar"/>   

        <mkdir      dir         = "..\s7_preverifyObfuscated"/>   

        <mkdir      dir         = "..\s8_jarObfuscated"/>

        <mkdir      dir         = "..\bin"/>

      

        <delete>

          <fileset  dir         = "..\s1_compile"              includes="**\*"/>

          <fileset  dir         = "..\s2_preverify"            includes="**\*"/>

          <fileset  dir         = "..\s3_copyManifest"         includes="**\*"/>

          <fileset  dir         = "..\s4_jar"                  includes="**\*"/>

          <fileset  dir         = "..\s5_obfuscate"            includes="**\*"/>

          <fileset  dir         = "..\s6_unjar"                includes="**\*"/>

          <fileset  dir         = "..\s7_preverifyObfuscated"  includes="**\*"/>

          <fileset  dir         = "..\s8_jarObfuscated"        includes="**\*"/>     

          <fileset  dir         = "..\bin"               includes="**\*"/>     

        </delete>      

      

      </target>    

      

      <!-- COPY MANIFEST -->   

      <target       name        = "copyManifest">

    

        <filter     token       = "name"

                    value       = "${name}"/>

    

        <filter     token       = "version"

                    value       = "${version}"/>

    

        <copy       file        = "..\templates\MANIFEST_template.MF"

                    tofile      = "..\s3_copyManifest\MANIFEST.MF"

                    overwrite   = "yes"       

                    filtering   = "true"/>

      </target>   

        

    </project>  

    

    Repeated.xml

    <?xml version="1.0" encoding="UTF-8"?>

    

    <project basedir="." default="all" name="MyAntJava">

    

      <!-- ======================= INIT ================================== -->

      <property     file        = "build.properties"/>

    

      <property     name        = "lib"

                    value       = "${device}\lib\classes.zip"/>

                

      <property     name        = "jarSize"

                    value       = "0"/>            

                

      <!-- FOR CALCULATING FILE SIZE -->

      <taskdef      name        = "filesize"

                    classname   = "ant.FileSizeTask"

                    classpath   = "..\templates\filesize.jar"/>            

    

      <!-- TARGETS FLOW  -->

      <target       name        = "all"

                    depends     = "buildingStructure,compile,preverify,jar,copyJad,run"> 

      </target>  

      

      <!-- ========================= CREATE JAD ============================= -->

        

      <!-- BUILDING STRUCTURE -->

      <target       name        = "buildingStructure">

     

        <delete>

          <fileset  dir         = "..\s1_compile"              includes="**\*"/>

          <fileset  dir         = "..\s2_preverify"            includes="**\*"/>

        </delete>      

      

      </target>    

    

      <!-- COMPILE --> 

      <target       name        = "compile">

        <javac      classpath   = "${lib}"                

                    srcdir      = "..\..\src"

                    destdir     = "..\s1_compile" />

      </target>  

    

      <!-- PREVERIFY --> 

      <target       name        = "preverify">

        <exec       executable  = "${device}\bin\preverify">

            <arg    line        = "-classpath ${lib}"/>

            <arg    line        = "-d ..\s2_preverify"/>

            <arg    line        = "..\s1_compile"/>

        </exec>

      </target> 

      

      <!-- JAR -->   

      <target       name        = "jar">

        <jar        basedir     = "..\s2_preverify"

                    jarfile     = "..\bin\${name}.jar"

                    manifest    = "..\s3_copyManifest\MANIFEST.MF"> 

          <fileset  dir         = "..\..\res"/> 

        </jar>

      </target>     

             

      <!-- COPY JAD -->   

      <target       name        = "copyJad">

    

        <filesize   file        = "..\bin\${name}.jar"

                    property    = "jarSize"/>

    

        <filter     token       = "name"

                    value       = "${name}"/>

    

        <filter     token       = "jarSize"

                    value       = "${jarSize}"/>

    

        <filter     token       = "version"

                    value       = "${version}"/>

    

        <copy       file        = "..\templates\Template.jad"

                    tofile      = "..\bin\${name}.jad"

                    overwrite   = "yes"       

                    filtering   = "true"/>

      </target> 

          

      <!-- ========================= RUN ============================= -->

            

      <!-- RUN -->       

      <target       name        = "run">

        <exec       executable  = "${device}\bin\7210.exe">   

          <arg      value       = "..\bin\${name}.jad"/>

        </exec>   

      </target>     

      

    </project>              

 

- Errors: 

  - Application error java/lang/ClassNotFoundException

  - In preverify block use ';' instead ':' to define multiplie class paths.

 

  - BUILD FAILED: Unable to load default ProjectHelper due to java.lang.NoClassDefFoundError: org/xml/sax/SAXException

  - Eclipse 3.0 – RC on ANT file – Run – External Tools – JRE – Run in the same JRE as workspace

  - Eclipse 3.0 – RC on ANT file – Run – External Tools – JRE – Separate JRE:check – Select SDK1.4