Showing posts with label LaunchConfiguration. Show all posts
Showing posts with label LaunchConfiguration. Show all posts

Wednesday, February 26, 2014

Running external tools programmatically with eclipse

Small tutorial for running external tools programmatically with eclipse

Last couple of day I had to refactor code for an eclipse plugin.
The source code to modify rely on java runtime exec to launch a temporary maven project. This code works fine but the process launched does not log in eclipse console.
So at this point I look for a way to launch this maven build, external to the workspace as the eclipse menu "launch external tool does". After a few search on the web I found a way to be able to do this:
  • create a launch configuration
  • save it in a file
  • build the same setup in source code
  • and finally launch the configuration

First thing to do : create an external tool configuration...
saved in a file (see Shared file field)

Run this configuration.

You should find a file in the root folder project named : New_configuration.launch.
Now when editing this file I have all the parameters needed to write my code :
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.ui.externaltools.ProgramLaunchConfigurationType">
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_LOCATION" 
 value="${system_path:mvn.bat}"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_TOOL_ARGUMENTS" 
 value="-Dlogback.configurationFile=customlogback.xml -U clean install exec:java"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_WORKING_DIRECTORY" 
 value="D:\DONNEES\FDS_DATA\genDoc"/>
</launchConfiguration>

With this content and this address I could now write the source code that allow me to launch programmatically my temporary maven project.
A nice point here, you could rely on eclipse variable (like ${system_path:mvn.bat})  
//to be able to listen the end
manager.addLaunchListener(this);
ILaunchConfigurationType type = manager
  .getLaunchConfigurationType("org.eclipse.ui.externaltools.ProgramLaunchConfigurationType");
ILaunchConfiguration[] configurations = manager
  .getLaunchConfigurations(type);
for (int i = 0; i < configurations.length; i++) {
 ILaunchConfiguration configuration = configurations[i];
 if (configuration.getName().equals("Gen doc")) {
  configuration.delete();
  break;
 }
}
ILaunchConfigurationWorkingCopy workingCopy = type.newInstance(
  null, "Gen doc");
String exec = "mvn.sh";
if (nameOS.toLowerCase().contains("win")) {
 exec = "mvn.bat";
}
workingCopy.setAttribute(
  "org.eclipse.ui.externaltools.ATTR_LOCATION",
  "${system_path:" + exec + "}");
workingCopy.setAttribute(
  "org.eclipse.ui.externaltools.ATTR_TOOL_ARGUMENTS",
  "-Dlogback.configurationFile=genDoclogback.xml -U clean install exec:java");
workingCopy.setAttribute(
  "org.eclipse.ui.externaltools.ATTR_WORKING_DIRECTORY",
  tmpGenDir.getCanonicalPath());
ILaunch launch = workingCopy.launch(ILaunchManager.RUN_MODE,
  new NullProgressMonitor());
//to retreive the launched project at the end
launchProject.put(launch, project);

and to be able to listen the end of your launch configuration implement "ILaunchesListener2" and add

@Override
public void launchesTerminated(ILaunch[] launches) {
 for (ILaunch launch : launches) {
  Project project = launchProject.get(launch);
  if (project != null) {
   launchProject.remove(launch);
   //if needed...
   Display.getDefault().asyncExec(new Runnable() {
    @Override
    public void run() {
     
    }
   });
  }
 }
}