Writing Eclipse plugins with Groovy
January 17, 2008
Writing plugins for Eclipse with other languages than Java is not officially supported, but there is a way to write an Eclipse plugin with Groovy only. As prerequisites you need Eclipse with a JDT and the Groovy Eclipse plugin.
Follow these steps.
- Create a new Plugin-in project, do not use any templates.
- Add the Groovy Nature to the project.
- Create a Java package under
src
- Create a Groovy class
HelloGroovyWorld
with the contents shown below. - Edit the plugin.xml file:
- On the Dependencies tab add
org.eclipse.ui
andorg.eclipse.core.runtime
. andorg.codehaus.groovy
. - On the Runtime tab add
bin-groovy
to the classpath - On the Extensions tab add an
org.eclipse.ui.actionSets
extension, set visible to true. Add a menu and an action (Left click the ActionSet, right click and choose “new, menu and action”) and choose HelloGroovyWorld as the class for the action. The plugin.xml is shown below.
- On the Dependencies tab add
<extension
point="org.eclipse.ui.actionSets">
<actionSet
id="hellogroovyworld.actionSet1"
label="Groovy ActionSet"
visible="true">
<menu
id="groovyMenu"
label="Groovy Menu">
</menu>
<action
class="hellogroovyworld.HelloGroovyWorld"
id="hellogroovyworld.action2"
label="Groovy World"
menubarPath="groovyMenu"
toolbarPath="groovyMenu"
tooltip="Hello Groovy World">
</action>
</actionSet>
</extension>
The source code:
package hellogroovyworld
import org.eclipse.jface.action.IAction
import org.eclipse.jface.viewers.ISelection
import org.eclipse.ui.IWorkbenchWindow
import org.eclipse.ui.IWorkbenchWindowActionDelegate
import org.eclipse.jface.dialogs.MessageDialog
class HelloGroovyWorld implements IWorkbenchWindowActionDelegate {
private IWorkbenchWindow window
void run(IAction action) {
MessageDialog.openInformation(
window.getShell(),
"Hellogroovyworld Plug-in",
"Hello, Groovy world")
}
void selectionChanged(IAction action, ISelection selection) {}
void dispose() {}
void init(IWorkbenchWindow window) {
this.window = window
}
}
Now run the project as an Eclipse Application: