Groovy - How to Execute Groovy Scripts in Sublime Text?

This article covers the steps to setup Sublime Text to execute Groovy scripts.

Step 1

Create a new file viz. groovy.sublime-build and put the following JSON content in there. Replace the path to groovy.bat with the right one on your machine.

    "cmd": ["C:\\groovy-2.4.12\\bin\\groovy.bat","$file"],
    "selector": "source.groovy"

Step 2

Save this file to the following location. The location may vary based on where Windows is installed and the login id.

C:\Users\{{youruserid}}\AppData\Roaming\Sublime Text 3\Packages\User\

Step 3

Close and Reopen Sublime Text.

Step 4

To run the groovy script, open the .groovy file in Sublime Text. While it's in the active view, select [ Tools->Build System->groovy ] or [ Tools->Build System->Automatic ] and press Ctrl+B. This will execute the script and output the results on the Sublime Text console.

Some explanation for the above:

In Sublime Text, Build System can be created for any script by creating a file with .sublime-build extension. This file holds the instructions to execute a script. These instructions are in JSON format. On a restart, the filename will be shown under Tools->Build System.

The "selector" in the sublime-build, is required only if you want Sublime Text to identify the Build System in Automatic mode. It indicates the extension of the file for which the current sublime-build should be executed.