com.pyx4me.maven.j2me.AbstractWtkMojo Maven / Gradle / Ivy
/**
* Pyx4me framework
* Copyright (C) 2006-2007 pyx4.com.
*
* @author vlads
* @version $Id: AbstractWtkMojo.java 1498 2007-10-17 02:05:35Z vlads $
*/
package com.pyx4me.maven.j2me;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.PropertyHelper;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.Task;
/**
*
* @requiresDependencyResolution test
*
*/
public abstract class AbstractWtkMojo extends AbstractMojo {
/**
* The path to the j2me sdk to use for preverifying. e.g. ${env.WTK_HOME}
*
* @parameter
*/
protected String wtkHome;
/**
* Use WTK libraries or project dependency provided artifacts. See libs
*
* @parameter default-value="true"
*/
protected boolean useWtkLibs;
/**
* Additional -libraryjars e.g. ${java.home}/lib/rt.jar Project compile
* dependency used by ProGuard instead of WTK libs. See useWtkLibs
*
* @parameter
*/
protected List libs;
/**
* Apply ProGuard classpathentry Filters to dependencies. When project
* compile dependency is used by ProGuard instead of WTK libs. See
* useWtkLibs
*
* @parameter
*
*/
protected List dependencies;
/**
* Directory containing the generated JAR.
*
* @parameter expression="${project.build.directory}"
* @required
*/
protected File outputDirectory;
/**
* The Maven project reference where the plugin is currently being executed.
* The default value is populated from maven.
*
* @parameter expression="${project}"
* @required
*/
protected MavenProject mavenProject;
/**
* @parameter expression="${project.packaging}"
* @readonly
* @required
*/
protected String packaging;
/**
* The plugin dependencies.
*
* @parameter expression="${plugin.artifacts}"
* @required
* @readonly
*/
protected List pluginArtifacts;
/**
* The midlets name
*
* @parameter expression="${j2me.midlet.name}"
* default-value="${project.name}"
* @required
*/
protected String midletName;
/**
* The midlets vendor
*
* @parameter expression="${j2me.midlet.vendor}"
* default-value="${project.organization.name}"
* @required
*/
protected String midletVendor;
/**
* The midlets version, -SNAPSHOT would be removed.
*
* @parameter expression="${j2me.midlet.version}"
* default-value="${project.version}"
* @required
*/
protected String midletVersion;
/**
* The midlets configuration
*
* @parameter expression="${j2me.midlet.configuration}"
* default-value="CLDC-1.1"
* @required
*/
protected String j2meConfiguration;
/**
* The midlets profile
*
* @parameter expression="${j2me.midlet.profile}" default-value="MIDP-2.0"
* @required
*/
protected String j2meProfile;
private Project antProject;
public void executeTask(Task task) throws MojoExecutionException, MojoFailureException {
List antTasks = new Vector();
antTasks.add(task);
executeTasks(antTasks);
}
String getWtkVersion(String version) {
int i = version.indexOf('-');
if (i == -1) {
return version;
} else {
return version.substring(i + 1);
}
}
Project getAntProject() throws MojoExecutionException {
if (antProject != null) {
return antProject;
}
antProject = new Project();
antProject.setName(mavenProject.getName());
antProject.init();
if (useWtkLibs && (wtkHome == null)) {
throw new MojoExecutionException(
"required plugin parameter is missing\n inside the definition for plugin: 'j2me-maven-plugin' specify the following:\n"
+ "\n" + " ...\n" + " ${env.WTK_HOME} \n"
+ " .");
}
if (wtkHome != null) {
antProject.setProperty("wtk.home", wtkHome);
}
antProject.setProperty("wtk.cldc.version", getWtkVersion(j2meConfiguration));
antProject.setProperty("wtk.midp.version", getWtkVersion(j2meProfile));
PropertyHelper propertyHelper = PropertyHelper.getPropertyHelper(antProject);
propertyHelper.setNext(new AntPropertyHelper(this.mavenProject, getLog()));
if (useWtkLibs) {
detectJSRLibrary(mavenProject, antProject);
}
DefaultLogger antLogger = new DefaultLogger();
antLogger.setOutputPrintStream(System.out);
antLogger.setErrorPrintStream(System.err);
antLogger.setMessageOutputLevel(getLog().isDebugEnabled() ? Project.MSG_DEBUG : Project.MSG_INFO);
antProject.addBuildListener(antLogger);
antProject.setBaseDir(mavenProject.getBasedir());
return antProject;
}
private void detectJSRLibrary(MavenProject mavenProject2, Project antProject2) {
// TODO Auto-generated method stub
}
public static String getSimpleName(Object obj) {
// Java 1.5
// obj.getClass().getSimpleName()
String simpleName = obj.getClass().getName();
// strip the package name
return simpleName.substring(simpleName.lastIndexOf(".") + 1);
}
public void initTask(Task task) throws MojoExecutionException {
task.setTaskName(getSimpleName(task));
task.setProject(getAntProject());
task.init();
}
public void executeTasks(List antTasks) throws MojoExecutionException, MojoFailureException {
getAntProject();
Target tasks = new Target();
tasks.setName(getSimpleName(this));
tasks.setProject(antProject);
for (Iterator i = antTasks.iterator(); i.hasNext();) {
Task task = (Task) i.next();
tasks.addTask(task);
}
tasks.execute();
}
public static File getClasspathElement(Artifact artifact, MavenProject mavenProject) throws MojoExecutionException {
String refId = artifact.getGroupId() + ":" + artifact.getArtifactId();
MavenProject project = (MavenProject) mavenProject.getProjectReferences().get(refId);
if (project != null) {
return new File(project.getBuild().getOutputDirectory());
} else {
File file = artifact.getFile();
if ((file == null) || (!file.exists())) {
throw new MojoExecutionException("Dependency Resolution Required " + artifact);
}
return file;
}
}
private Map compileArtifactsByID = null;
public Artifact getArtifactByID(String id) {
if (compileArtifactsByID == null) {
compileArtifactsByID = new HashMap();
List dependencies = mavenProject.getCompileArtifacts();
for (Iterator i = dependencies.iterator(); i.hasNext();) {
Artifact artifact = (Artifact) i.next();
compileArtifactsByID.put(artifact.getId(), artifact);
}
}
return (Artifact) compileArtifactsByID.get(id);
}
public Dependency getDependencyConfig(Artifact artifact) {
if (dependencies == null) {
return null;
}
boolean hasTrailFlag = false;
for (Iterator iter = dependencies.iterator(); iter.hasNext();) {
Dependency d = (Dependency) iter.next();
if (d.match(artifact)) {
return d;
}
if (d.withTrail) {
hasTrailFlag = true;
}
}
if (hasTrailFlag) {
for (Iterator trailIter = artifact.getDependencyTrail().iterator(); trailIter.hasNext();) {
String artifactID = (String) trailIter.next();
Artifact trailArtifact = getArtifactByID(artifactID);
if (trailArtifact == null) {
continue;
}
for (Iterator iter = dependencies.iterator(); iter.hasNext();) {
Dependency d = (Dependency) iter.next();
if ((d.withTrail) && (d.match(trailArtifact))) {
return d;
}
}
}
}
return null;
}
public boolean isUseDependency(Artifact artifact) {
Dependency dependencyConfig = getDependencyConfig(artifact);
boolean include = Artifact.SCOPE_PROVIDED.equals(artifact.getScope())
|| Artifact.SCOPE_SYSTEM.equals(artifact.getScope());
if (dependencyConfig != null) {
if (dependencyConfig.exclude) {
return false;
}
if (dependencyConfig.include) {
include = true;
}
}
return include;
}
}