Groovy with Intellij

[Updated: Dec 5, 2018, Created: Nov 9, 2018]

This tutorials shows how to use Groovy language in Intellij IDE.

We are using Groovy 2.5.3, IntelliJ IDEA 2018.2.5 (Community Edition) with Java 11.

Creating Groovy Project in IntelliJ

Intellij Community Edition has built in Groovy support, so we don't have to install any plugin.

To create a Groovy project go to File>New>Project:

Select 'Groovy' in the left list. On the right panel click on 'Create...' button to select Groovy library.

Now click on 'Next' button. Enter the project name/location and click OK:

A message will be shown that 'The project directory "C\:groovy-hello-world" does not exist . It will be....". Click on OK.

That will create the Groovy project:

Make sure that your choice of JDK is selected in 'Project Structure' dialog as shown:

Right click on 'src' folder and select 'Groovy Script' as shown:

Enter the script name:

Enter some code in the editor:

In Intellij Community edition there's no right click menu option to run the script directly (Ultimate edition has, which we will see later in this tutorial), so we have to create a run configuration manually.

Click on 'Add Configuration' in the tool bar and click on the + button on left and select 'Groovy' as shown:

Enter the fields as shown:

If you are using Java 9/10/11 then make sure you have entered following in the VM Options:

--add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.invoke=ALL-UNNAMED

Also you have to add JAXB dependencies if you are using Groovy 2.5.3 + Java 11 (also check out Java 11 related change). Groovy comes with extra JAXB Jars so we can add them. Open 'Project Structure' dialog, then select 'Dependencies' tab and add the dependencies as shown:

If you don't add the JAXB Jars then you will have following exception on running the script:

Information:Groovyc: While compiling groovy-hello-world:java.lang.NoClassDefFoundError: Unable to load class org.apache.groovy.jaxb.extensions.JaxbExtensions due to missing dependency javax/xml/bind/JAXBContext
at org.codehaus.groovy.vmplugin.v5.Java5.configureClassNode(
at org.codehaus.groovy.ast.ClassNode.lazyClassInit(
at org.codehaus.groovy.ast.ClassNode.getMethods(
at org.codehaus.groovy.macro.transform.MacroMethodsCache.scanExtClasses(
at org.codehaus.groovy.macro.transform.MacroMethodsCache.access$000(
at org.codehaus.groovy.macro.transform.MacroMethodsCache$2.onModule(
at org.codehaus.groovy.runtime.m12n.ExtensionModuleScanner.scanExtensionModuleFromProperties(
at org.codehaus.groovy.runtime.m12n.ExtensionModuleScanner.scanExtensionModuleFromMetaInf(
at org.codehaus.groovy.runtime.m12n.ExtensionModuleScanner.scanClasspathModulesFrom(
at org.codehaus.groovy.runtime.m12n.ExtensionModuleScanner.scanClasspathModules(

After adding JAXB Jar, run Groovy Script by clicking green run button (make sure our hello-groovy-script is selected in run configuration):

Code Autocompletion

Intellij (including Community edition) provides very quick code autocompletion, help documents and error indications:

Using Intellij Ultimate

There you will find the right click menu to run the Groovy script directly:

Community edition and run action

In our above example we saw, in Intellij Community edition, no 'run' action was available for our simple Groovy script and we had to create a run configuration manually. But we noticed if a certain artifacts is created in groovy script like a class definition e.g.

class Person {....}
then right click 'run <script-name>' is available in Community edition too (observed in version: IntelliJ IDEA 2018.2.5).

See Also