Eclipse RCP : exécuter une LaunchConfiguration

Dans une application RCP, il peut être utile de créer une action pour exécuter une launchConfiguration présente dans le workspace.

Pour ce faire, il va falloir utiliser des classes présentes dans le plugin org.eclipse.debug et re-créer manuellement la configuration telle que l'IDE l'exécute lorsque l'on lance l'action "Run...".

Commençons par exporter la configuration dans un fichier afin de voir de quoi elle est constituée. Dans le menu "Run configurations", ouvrir l'onglet Common pour définir l'endroit où sauvegarder le fichier (option "Shared file") :

A la fermeture de cette fenêtre, un fichier *.launch a été créé. Voici un exemple de contenu :


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.ant.AntLaunchConfigurationType">
<booleanAttribute key="org.eclipse.ant.ui.DEFAULT_VM_INSTALL" value="false"/>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
<listEntry value="/my.project/buildProduct.xml"/>
</listAttribute>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
<listEntry value="1"/>
</listAttribute>
<stringAttribute key="org.eclipse.jdt.launching.CLASSPATH_PROVIDER" value="org.eclipse.ant.ui.AntClasspathProvider"/>
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="my.project"/>
<stringAttribute key="org.eclipse.jdt.launching.SOURCE_PATH_PROVIDER" value="org.eclipse.ant.ui.AntClasspathProvider"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_LOCATION" value="${workspace_loc:/my.project/buildProduct.xml}"/>
<stringAttribute key="process_factory_id" value="org.eclipse.ant.ui.remoteAntProcessFactory"/>
</launchConfiguration>
 

Le fichier est finalement assez simple, on trouve le type de configuration (ici une tâche Ant) et tous les paramètres requis.

Et voici en quelques lignes comment créer les objets nécessaires pour le lancement manuel :


import java.util.Arrays;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.ui.DebugUITools;

public class RunAntBuild {

 public static void main(String... args) {
  ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
  ILaunchConfigurationType type =
        manager.getLaunchConfigurationType("org.eclipse.ant.AntLaunchConfigurationType");
  try {
   ILaunchConfigurationWorkingCopy workingCopy =
           type.newInstance(null, "Run ant task");
   workingCopy.setAttribute("org.eclipse.ant.ui.DEFAULT_VM_INSTALL","false");
   workingCopy.setAttribute("org.eclipse.debug.core.MAPPED_RESOURCE_PATHS",Arrays.asList("/my.project/buildProduct.xml"));
   workingCopy.setAttribute("org.eclipse.debug.core.MAPPED_RESOURCE_TYPES",Arrays.asList("1"));
   workingCopy.setAttribute("org.eclipse.jdt.launching.CLASSPATH_PROVIDER","org.eclipse.ant.ui.AntClasspathProvider");
   workingCopy.setAttribute("org.eclipse.jdt.launching.PROJECT_ATTR","my.project");
   workingCopy.setAttribute("org.eclipse.jdt.launching.SOURCE_PATH_PROVIDER","org.eclipse.ant.ui.AntClasspathProvider");
   workingCopy.setAttribute("org.eclipse.ui.externaltools.ATTR_LOCATION",antXmlPath);
   workingCopy.setAttribute("process_factory_id","org.eclipse.ant.ui.remoteAntProcessFactory");
   DebugUITools.launch(workingCopy, ILaunchManager.RUN_MODE);
  } catch (CoreException e) {
   e.printStackTrace();
  }
 }

}

Fichier(s) joint(s) :

0 commentaires: