com.pyx4me.maven.j2me.WtkPreverifyMojo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of j2me-maven-plugin Show documentation
Show all versions of j2me-maven-plugin Show documentation
Maven 2 Plugin to make j2me project
/**
* Pyx4me framework
* Copyright (C) 2006-2007 pyx4.com.
*
* @author vlads
* @version $Id: WtkPreverifyMojo.java 103 2007-04-21 01:33:52Z vlads $
*/
package com.pyx4me.maven.j2me;
import java.io.File;
import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.List;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.Path;
import de.pleumann.antenna.WtkPreverify;
import de.pleumann.antenna.misc.Utility;
/**
* The preverify MoJo runs the preverify command from the given J2ME SDK.
*
* Preverification takes place in the phase process-classes, that means after
* successfull compilation. The MoJo takes the classes to preverify from
* ${project.build.outputDirectory} and places the preverified classes into the
* same directory. Therefore no change to the packaging phase is neccessary.
*
*
* @goal preverify
* @phase process-classes
* @description Preverifies j2me class files
*
*/
public class WtkPreverifyMojo extends AbstractWtkMojo {
public void execute() throws MojoExecutionException, MojoFailureException {
WtkPreverify task = createWtkPreverifyTask(this);
// get target directory
String target = mavenProject.getBuild().getOutputDirectory();
task.setSrcdir(new File(target));
task.setDestdir(new File(target));
super.executeTask(task);
}
static WtkPreverify createWtkPreverifyTask(AbstractWtkMojo mojo)
throws MojoExecutionException, MojoFailureException {
WtkPreverify task = new WtkPreverify();
mojo.initTask(task);
Project antProject = mojo.getAntProject();
if (!mojo.useWtkLibs) {
List dependancy = mojo.mavenProject.getCompileArtifacts();
for (Iterator i = dependancy.iterator(); i.hasNext();) {
Artifact artifact = (Artifact) i.next();
if (!Artifact.SCOPE_PROVIDED.equals(artifact.getScope())) {
continue;
}
File file = getClasspathElement(artifact, mojo.mavenProject);
mojo.getLog().info("WtkPreverify.classpathEntry " + file);
task.setClasspath(new Path(antProject, file.getAbsolutePath()));
}
}
// Hacks to keep tmp directory
File tmpDir = new FileTmpDirRemovalHack(mojo.outputDirectory, "wtk-preverify");
if (!tmpDir.exists()) {
tmpDir.mkdir();
}
Utility utility = Utility.getInstance(antProject, task);
privateSet(utility, "tmpDir", tmpDir);
mojo.getLog().info("WtkPreverify.fullClasspath " + task.getFullClasspath());
return task;
}
private static class FileTmpDirRemovalHack extends File {
private static final long serialVersionUID = 1L;
public FileTmpDirRemovalHack(File file) {
super(file.getAbsolutePath());
}
public FileTmpDirRemovalHack(File parent, String child) {
super(parent, child);
}
public File[] listFiles() {
StackTraceElement[] ste = new Throwable().getStackTrace();
if (ste[1].getMethodName().equals("delete")) {
return new File[0];
}
return super.listFiles();
}
public boolean delete() {
return true;
}
}
private static void privateSet(Object obj, String name, Object value) throws MojoExecutionException, MojoFailureException {
Class klass = obj.getClass();
try {
Field f = null;
Field fields[] = klass.getDeclaredFields();
for (int i = 0; i < fields.length; i++){
if (name.equals(fields[i].getName())) {
fields[i].setAccessible(true);
f = fields[i];
break;
}
}
f.set(obj, value);
} catch (Throwable e) {
e.printStackTrace();
throw new MojoExecutionException("Can't accss filed " + name + " " + e.toString(), e);
}
}
}