Hi

Here is some code snippet that launches a maven goal.
(It is easy to for instance launch an Ant target, it is just another launch configuration that must be used)
This snippet does not really has any dependency to the m2eclipse plugin.



import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.core.ILaunchesListener2;
import org.eclipse.jface.operation.IRunnableWithProgress;

public class Maven2Runner {

  private static final String MAVEN_LAUNCH_CONFIG_TYPE = "org.maven.ide.eclipse.Maven2LaunchConfigurationType"; //$NON-NLS-1$
 
  /**
   * Run a maven goal for the project. After the goal has been run, the
   * project is refreshed
   *
   * @param project
   * @param mavenGoalString
   */
  public static void runMavenGoal(final IProject project,
      final String mavenGoalString) {
    final ILaunchConfigurationType type = DebugPlugin.getDefault().getLaunchManager()
        .getLaunchConfigurationType(MAVEN_LAUNCH_CONFIG_TYPE); //$NON-NLS-1$
    final String launchName = project.getName();
   
    IRunnableWithProgress runnable = new IRunnableWithProgress() {
      @SuppressWarnings("unchecked")
      public void run(IProgressMonitor monitor)
          throws InvocationTargetException, InterruptedException {
        try {
          monitor.beginTask("Running maven goal '" + mavenGoalString + "'", 100);
          ILaunchConfigurationWorkingCopy config = null;
          config = type.newInstance(null, launchName);
          List options = new ArrayList();
          config.setAttribute(
              "org.eclipse.debug.ui.ATTR_LAUNCH_IN_BACKGROUND", true);
          config.setAttribute("M2_PROPERTIES", options);
          config.setAttribute("org.eclipse.jdt.launching.WORKING_DIRECTORY",
              "${workspace_loc:/" + project.getName() + "}");
          config.setAttribute("M2_GOALS", mavenGoalString);
          config.setAttribute("M2_PROFILES", "");
          // Launch the goal
          ILaunch launch = config.launch(ILaunchManager.RUN_MODE, new NullProgressMonitor());
          // add listener so the project can be refreshed later when the maven
          // goal is terminaded
          DebugPlugin.getDefault().getLaunchManager().addLaunch(launch);
          LaunchesListener.PostLaunchAction pla = new LaunchesListener.PostLaunchAction() {
            public void execute() throws Exception {
              project.refreshLocal(IResource.DEPTH_INFINITE, null);
            }
          };
          ILaunchesListener2 launchesListener = new LaunchesListener(launch, pla);
          DebugPlugin.getDefault().getLaunchManager().addLaunchListener(
              launchesListener);
          monitor.worked(100);
        }
        catch (CoreException ce) {
           // TODO
        }
      }
    };

    // Launch the task with progress
    try {
      Progress progress = new Progress(MyPlugin.getShell(), runnable);
      progress.run();
    }
    catch (InvocationTargetException ite) {
        // TODO
    }
    catch (InterruptedException ie) {
         // TODO
    }
  }


This code can be called from a context menu item
i.e. an org.eclipse.ui.actions.ActionDelegate implmentation of your own.

Cheers
Lucas



On 12/21/2010 08:27 AM, Gajo Csaba wrote:
Hi,

I have a question regarding the m2eclipse plugin. Let me know if this is not the right place to ask.

I'm writing my own custom plugin, and I would like to execute the "mvn package" command. Meaning: I want to run the m2eclipse builder, so that it would run the "Run As->Maven package". Anyone has some source code or can give me a tip how to do this?

I'm guessing that I would need to create a new LaunchConfiguration, but I've no idea how to start...

Thanks,
Csaba



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@maven.apache.org
For additional commands, e-mail: users-h...@maven.apache.org


--
Oracle
Lucas Persson | Principal Member of Technical Staff
Phone: +4684773644 | | | Mobile: +46730946656
Oracle Communications Platform
ORACLE Sweden | Folkungagatan 122 | 116 30 Stockholm


Oracle Svenska AB, Kronborgsgränd 17, S-164 28 KISTA, reg.no. 556254-6746
Green
          Oracle Oracle is committed to developing practices and products that help protect the environment

Reply via email to