Posted by: Marek | March 13, 2009

Building a Java ME application with Maven

Building a Java ME application with Maven is not trivial, but doable. The following items need to be taken into consideration.

  1. CLDC and MIDP APIs need to be added to the project classpath. This is pretty straightforward:

    <properties>

    <j2me.version>2.0.2</j2me.version>

    </properties>

    <dependencies>

    <dependency>

    <groupId>org.microemu</groupId>

    <artifactId>midpapi20</artifactId>

    <version>${j2me.version}</version>

    <scope>provided</scope>

    </dependency>

    <dependency>

    <groupId>org.microemu</groupId>

    <artifactId>cldcapi11</artifactId>

    <version>${j2me.version}</version>

    <scope>provided</scope>

    </dependency>

    </dependencies>

  2. The compiler needs to be set up to use the Java 1.3 class format and only use CLDC and MIDP APIs in the classpath. Again, this is pretty straightforward:

    <build>

    <plugins>

    <plugin>

    <artifactId>maven-compiler-plugin</artifactId>

    <configuration>

    <source>1.3</source>

    <target>1.3</target>

    <compilerArguments>

    <bootclasspath>

    ${settings.localRepository}/org/microemu/cldcapi11/${j2me.version}/cldcapi11-${j2me.version}.jar${path.separator}${settings.localRepository}/org/microemu/midpapi20/${j2me.version}/midpapi20-${j2me.version}.jar

    </bootclasspath>

    </compilerArguments>

    </configuration>

    </plugin>

    </plugins>

    </build>

  3. A thirdparty plugin needs to be used to package the application. In the example shown below I am using a j2me-maven-plugin provided by pyx4me.com. The plugin is configured to execute the j2me:package goal during the package phase of a Maven build. The goal creates a jar and a jad file for application, along with essential entries in MANIFEST.MF. Important configuration details are as follows:
    • Name of middlet(s) to be included in the application need to be explictly specified
    • WTK_HOME referenced in pom.xml is an environment variable that must be set up to point to a WTK installation before starting the build
    • Proguard is used to compact the application (i.e. remove unused classes from the application and “linked” dependencies. Entry point(s) of the application must be set with the keep option. If the application used introspection, classes used in that fashion would have to be specified as well.

    When mvn package is executed the build script creates files named mobic.jar and mobic.jad in the target directory. An entire working example can be downloaded from the Mobic SVN repository on sourceforge: https://mobic.svn.sourceforge.net/svnroot/mobic/trunk/project

    Execution of tests will be described in a separate post.

    <plugin>

    <groupId>com.pyx4me</groupId>

    <artifactId>j2me-mavenplugin</artifactId>

    <executions>

    <execution>

    <phase>package</phase>

    <goals>

    <goal>package</goal>

    </goals>

    </execution>

    </executions>

    <configuration>

    <wtkHome>${env.WTK_HOME}</wtkHome>

    <j2meConfiguration>CLDC-1.1</j2meConfiguration>

    <j2meProfile>MIDP-2.0</j2meProfile>

    <jarfile>mobic</jarfile>

    <jadfile>mobic</jadfile>

    <appendClassifier>false</appendClassifier>

    <proguard>true</proguard>

    <obfuscate>false</obfuscate>

    <midletVendor>net.sourceforge.mobic</midletVendor>

    <midlets>

    <MIDlet>

    <name>Mobic</name>

    <class>net.sourceforge.mobic.ui.MobicMIDlet

    </class>

    <icon>mobic.png</icon>

    </MIDlet>

    </midlets>

    <proguardOptions>

    <options>

    <option>

    -keep public class net.sourceforge.mobic.ui.MobicMIDlet

    </option>

    </options>

    </proguardOptions>

    </configuration>

    </plugin>

Advertisements

Responses

  1. Hello!
    Very Interesting post! Thank you for such interesting resource!
    PS: Sorry for my bad english, I’v just started to learn this language 😉
    See you!
    Your, Raiul Baztepo


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: