All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.pyx4me.maven.j2me.AbstractWtkMojo Maven / Gradle / Ivy

There is a newer version: 2.0.4
Show newest version
/**
 * Pyx4me framework
 * Copyright (C) 2006-2007 pyx4.com.
 * 
 * @author vlads
 * @version $Id: AbstractWtkMojo.java 107 2007-04-21 03:10:01Z vlads $
 */
package com.pyx4me.maven.j2me;

import java.io.File;
import java.util.Iterator;
import java.util.List;
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
	 * @required
	 */
	protected String wtkHome;

    /**
     * Use WTK libraries or project dependancy provided artifacts. See libs
     *
     * @parameter default-value="true"
     * @required
     */
	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;
	
    /**
     * 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() {
		if (antProject != null) {
			return antProject;
		}
		antProject = new Project();
		antProject.setName(mavenProject.getName());
		antProject.init();
		
		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) {
		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;
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy