package com.byluroid.eclipse.maven.android;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.MavenExecutionResult;
import org.apache.maven.plugin.MojoExecution;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.maven.ide.eclipse.MavenPlugin;
import org.maven.ide.eclipse.core.IMavenConstants;
import org.maven.ide.eclipse.embedder.IMaven;
import org.maven.ide.eclipse.project.IMavenProjectFacade;
import org.maven.ide.eclipse.project.MavenProjectManager;
import org.maven.ide.eclipse.project.ResolverConfiguration;
import org.maven.ide.eclipse.project.configurator.AbstractBuildParticipant;

public class AndroidMavenBuildParticipant extends AbstractBuildParticipant {

	MojoExecution execution;

	public AndroidMavenBuildParticipant (MojoExecution execution) {
		this.execution = execution;
	}

	@Override
	public Set<IProject> build(int kind, IProgressMonitor monitor) throws Exception {
		if(IncrementalProjectBuilder.CLEAN_BUILD == kind || IncrementalProjectBuilder.FULL_BUILD == kind) {
			MavenPlugin plugin = MavenPlugin.getDefault();
			MavenProjectManager projectManager = plugin.getMavenProjectManager();
			IMaven maven = plugin.getMaven();
			IProject project = getMavenProjectFacade().getProject();
			IFile pom = project.getFile(new Path(IMavenConstants.POM_FILE_NAME));
			IMavenProjectFacade projectFacade = projectManager.create(pom, false, monitor);
			ResolverConfiguration resolverConfiguration = projectFacade.getResolverConfiguration();
			MavenExecutionRequest request = projectManager.createExecutionRequest(pom, resolverConfiguration, monitor);

			List<String> goals = new ArrayList<String>();
			goals.add("package");
			request.setGoals(goals);

			Properties properties = request.getUserProperties();
			properties.setProperty("maven.test.skip", "true");
			request.setUserProperties(properties);

			MavenExecutionResult executionResult = maven.execute(request, monitor);

			if (executionResult.hasExceptions()){
				List<Throwable> exceptions = executionResult.getExceptions();
				for (Throwable throwable : exceptions) {
					throwable.printStackTrace();
				}
			}
		}
		return null;
	}

}

@@ -38,16 +38,37 @@
 droid;%0A%0A
+import java.io.File;%0A
 import j
@@ -83,24 +83,24 @@
 .ArrayList;%0A
-
 import java.
@@ -162,16 +162,99 @@
 l.Set;%0A%0A
+import org.apache.commons.io.FileUtils;%0Aimport org.apache.maven.artifact.Artifact;%0A
 import o
@@ -546,24 +546,63 @@
 ectBuilder;%0A
+import org.eclipse.core.runtime.IPath;%0A
 import org.e
@@ -639,16 +639,16 @@
 onitor;%0A
-
 import o
@@ -673,24 +673,104 @@
 ntime.Path;%0A
+import org.eclipse.jdt.core.IJavaProject;%0Aimport org.eclipse.jdt.core.JavaCore;%0A
 import org.m
@@ -2652,16 +2652,593 @@
 ;%0A%09%09%09%09%7D%0A
+%09%09%09%7Delse%7B%0A%09%09%09%09Artifact apkArtifact = executionResult.getProject().getArtifact();%0A%09%09%09%09if (%22apk%22.equals(apkArtifact.getType()))%7B%0A%09%09%09%09%09File apkFile = apkArtifact.getFile();%0A%09%09%09%09%09IJavaProject javaProject = JavaCore.create(project);%0A%09%09%09%09%09IPath outputLocation = javaProject.getOutputLocation();%0A%09%09%09%09%09File realOutputFolder = project.getWorkspace().getRoot().getFolder(outputLocation).getLocation().toFile();%0A%09%09%09%09%09String newApkFilename = project.getName() + %22.apk%22;%0A%09%09%09%09%09File newApkFile = new File(realOutputFolder, newApkFilename);%0A%09%09%09%09%09FileUtils.copyFile(apkFile, newApkFile);%0A%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D

Copy the built apk file to where Eclipse and the ADT expects to find it, when the user does "Run As: Android Application".