Since v8, IntelliJ IDEA, one of the most popular Java IDEs, has native support for Groovy. There is no need to install a plugin to start coding in Groovy and benefit from a number of interesting features such as code completion and refactoring. The latest iteration of IntelliJ IDEA, Version 12, has full support for Groovy 2.0.
In this recipe, we are going to show how to set up a Groovy project in IDEA and showcase some of the most interesting qualities of the integration.
To get started with this recipe, you need v12 of IntelliJ IDEA. The IDE comes in two versions, Community Edition and Ultimate. JetBrains, the company behind IDEA, offers the Community Edition for free, while it charges for the Ultimate version. The good news is that Groovy support is available in the free version of the IDE so you can start using it straight away. Download IntelliJ IDEA from http://www.jetbrains.com.
You also need to install Java and a Groovy distribution. Refer to the installation recipes from earlier in this chapter.
Let's start with the project creation:
MyGroovyClass
, as shown in the following screenshot:MyJavaClass
, as shown in the following screenshot:As you can see in the previous screenshot, the code autocompletion works perfectly and it's even able to propose synthetic methods that are generated by the Groovy compiler, such as getters and setters.
For a broader look at the integration between Java and Groovy, take a look at the Using Java classes from Groovy recipe in Chapter 2, Using Groovy Ecosystem.
Several refactoring goodies are also available, including Extract Parameter that also works for closures (see the Defining code as data in Groovy recipe in Chapter 3, Using Groovy Language Features), as shown in the following screenshot:
The result of the refactoring will be as shown in the following screenshot: