Assigning a keyboard shortcut for a specific Eclipse build configuration

前端 未结 4 629
孤街浪徒
孤街浪徒 2020-11-28 23:39

In our Java project in Eclipse, we have several build configurations, as we have an engine that when run, builds installation jar for a specific projects according to the pa

相关标签:
4条回答
  • 2020-11-28 23:48

    One alternative is to use the accelerator keys to navigate the Run menu and select the right run configuration.

    On my computer it is:

    Alt + R, H, Number

    Alt+R gets you to get to Run menu, then H for Debug History, then one of the numbered alternatives.

    To make a run configuration always be visible in the Debug or Run history menus and retain its position one can do the following:

    Select the Run menu -> Run Configurations -> [some config] -> Common, then check Display in favourites menu box.

    (This answer is a copy of one of my previous answers.)

    0 讨论(0)
  • 2020-11-28 23:58

    You could define a plugin with some launchShortcuts in it.

    This thread is a good illustration.

    But to actually bind it, you would need to define a command running that configuration, and bind that command to a key (like in this plugin.xml configuration file)

    the shortcut definition of a launch configuration:

      <shortcut id="org.maven.ide.eclipse.pomFileAction"
                category="org.maven.ide.eclipse"
                class="org.maven.ide.eclipse.actions.ExecutePomAction"
                icon="icons/m2.gif"
                label="%m2.popup.pomFile.label"
                modes="run,debug">
         <contextualLaunch>
           <contextLabel label="%m2.popup.pomFile.label" mode="run"/>
           <contextLabel label="%m2.popup.pomFile.label" mode="debug"/>
           <enablement>
             <with variable="selection">
               <count value="1"/>
               <iterate>
                 <and>
                   <test property="org.maven.ide.eclipse.launchable"/>
                   <adapt type="org.eclipse.core.runtime.IAdaptable"/>
                 </and>
               </iterate>
             </with>
           </enablement>
       </contextualLaunch>
     </shortcut>
    

    Then the command:

     <extension point="org.eclipse.ui.commands">
        <command id="org.maven.ide.eclipse.pomFileAction.run"
                 categoryId="org.eclipse.debug.ui.category.run"
                 name="%m2.shortcut.description.run"
                 description="%m2.shortcut.description.run"/>
         ...
     </extension>
    

    Then the key binding for a keyboard shortcut:

    <extension point="org.eclipse.ui.bindings">
        <key sequence="M3+M2+X M"
             contextId="org.eclipse.ui.globalScope"
             commandId="org.maven.ide.eclipse.pomFileAction.run"
             schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"/>
     </extension>
    
    0 讨论(0)
  • 2020-11-29 00:08

    I was able to put together specific steps based on splintor's thread and get this working (I also added a loop at the top that finds any existing launch with the same name and terminates it, effectively making this a "restart" macro):

    To assign keyboard shortcuts to specific Eclipse launch configurations, perform the following steps:

    • Install https://sourceforge.net/projects/practicalmacro/, which you can inside Eclipse via Help->Software Updates: http://puremvcnotificationviewer.googlecode.com/svn/trunk/PracticallyMacroGoogleUpdateSite

    • Restart Eclipse and open Eclipse Preferences. You will have a new section called "Practically Macro Options", expand that section.

    • Click on "Editor Macro Definitions"

    • Click on "new..."

    • In the list of available commands, scroll down to "Editor Macro script (Beanshell)", select it and then click "Add->"

    • When the script editor pops up, add the following code to the existing code:

      import org.eclipse.debug.core.DebugPlugin;
      import org.eclipse.debug.core.ILaunchConfiguration;
      import org.eclipse.debug.core.ILaunch;
      import org.eclipse.debug.ui.DebugUITools;
          try
          {
            // Terminate process if it already exists from a previous launch 
            org.eclipse.debug.core.ILaunch[] allLaunches=DebugPlugin.getDefault().getLaunchManager().getLaunches();
            for (ILaunch l : allLaunches)
            {              
              if (l.getLaunchConfiguration().getName().equals("YOUR CONFIG NAME HERE"))
              {
                console.write("terminating launch: " );
                console.writeln(l.getLaunchConfiguration().getName());
                l.terminate();
                break; 
              }
            }
      
              org.eclipse.debug.core.ILaunchConfiguration[] allConfigurations=DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurations();
              for (ILaunchConfiguration config : allConfigurations) {
                  if (config.getName().equals("YOUR CONFIG NAME HERE"))
                  {
                      DebugUITools.launch(config, "debug");
                      break;
                  }
              }
          } catch (CoreException e) {
              e.printStackTrace();
          }
          finally{}
      
    • Note line 11 that checks the configuration name, replace with whatever you want

    • Also note DebugUITools.launch command on line 15, you can pass either "run" or "debug"

    • In the "Macro info" section at the bottom of this dialog, specify a macro name

    • IMPORTANT!: If you want to be able to see this macro in the standard Eclipse key binding dialog, you need to assign an id. I started with 1...

    • Click OK

    • Expand the "General" section and click on "Keys"

    • You can now search the possible key bindings for your new macro's name and assign it to any key you want.

    • NOTE: After assigning a key I often had to close and restart Eclipse before the key binding was respected.

    0 讨论(0)
  • 2020-11-29 00:12

    I was able to do it using Practically Macro - see this thread.

    0 讨论(0)
提交回复
热议问题