Tuesday, June 27, 2006

 

J2ME and Maven2 solved!

After much much too much head scratching I have discovered 'the way' to develop with maven in general. Maybe I'll write up a little more in depth description of 'the way' later, but for now a summary:

  1. Maven needs a scripting language. Enter groovy. Groovy rocks. If you wanna migrate some ant scripts or take advantage of ant taskdefs for some specific library, then groovy is the way to do it. Although maven2 has an Ant plugin to run arbitrary ant code, it is in essence useless for all but the simplest tasks (Unfortunately I discovered that the groovy-maven-plugin is slightly broken. You can't use the AntBuilder out of the box. Worry not, there is a workaround. See http://jira.codehaus.org/browse/MOJO-429 for more information).
  2. When standard libraries don't have maven plugins don't despair. You can:
    1. write your own. It's really easy. I advice writing it in Java, not Ant. Do this if you want glory or expect to use it more than once.
    2. just interface with the ant taskdef, or straight to the library yourself using groovy. Do this if it's a one off.
  3. Probably the biggest problem when starting with maven is that a library you need isn't in the public repository. Use Maven - Guide to installing 3rd party JARs


Back to getting that J2MEPolish example running. Again, I was missing a dependency:

mvn install:install-file -Dfile=blackberry-4.0.jar -DgroupId=j2mepolish -DartifactId=blackberry -Dversion=4.0 -Dpackaging=jar


I fixed my groovy-maven-plugin by adding the Ant dependency to the groovy-maven-plugin pom.xml. I could then use the AntBuilder. I have to say I was skeptical at first, but once I got started, wow!

Rather than writing a maven plugin for proguard (yes there should be one, no I'm not writing it to get an example application running), it was simply a matter of using the Ant taskdef from within groovy. Here is my groovy script:

import groovy.util

def libraryjars = ""
def ant = new AntBuilder()

for (dep in project.artifacts) {
if (dep.artifactId =~ /enough-client/) j2medep = dep
else if (dep.artifactId =~ /proguard/) {
proguard = dep
ant.taskdef(name:"proguard",classname:"proguard.ant.ProGuardTask",classpath:dep.file)
}
else {
libraryjars += "\n-libraryjar ${dep.file}"
}
}

def configuration = "-injars target/classes"+
"\n-injars ${j2medep.file}"+
"\n-outjars target/proguard.jar"+
"\n${libraryjars}"+
"\n-overloadaggressively"+
"\n-ignorewarnings"+
"\n-defaultpackage ''"+
"\n-allowaccessmodification"+
"\n-dontobfuscate"+
"\n-keep public class com.grimo.me.product.midpsysinfo.MIDPSysInfoMIDlet"

println "executing proguard for:\n ${configuration}"

ant.proguard(configuration)
ant.move(file:"target/classes",tofile:"target/oldclasses")
ant.unzip(src:"target/proguard.jar",dest:"target/classes")

This weaved together underlying dependencies, preverified, and made the jad. I then used the mpowerplayer to run the app. It had a little issue with localization, but apart from that the app really ran. Wow, that took a week of odd hours.

Blogged with Flock


Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?