Pimp My Groovy – boosting CPI Groovy developments with IntelliJ IDEA
After months of using it on my current project with CPI as the integration platform, I am very pleased with the productivity and efficiency boosts it has given me when developing Groovy scripts.
While Eclipse in its own right is a great IDE with a rich ecosystem, following are some aspects that have endeared IntelliJ to me.
- Context-aware code completion
- Native support for Groovy language (third party plugin not required, e.g. Groovy-Eclipse for language support in Eclipse)
- Native support for running Spock Framework specifications (including executing individual features within a specification)
- Easily download libraries from Maven repository (and linking them to the project)
- Simplified approach to include CPI libraries in project’s build path
In the following sections of this post, I will show how you can set up IntelliJ for your own Groovy developments.
Since IntelliJ IDEA and Groovy are running on Java, you will need a JDK installed on your local machine (if it’s not already there). Since CPI is now running on Java 8, JDK 8 would be a suitable choice.
And of course, you will need to install IntelliJ IDEA as well – the free Community Edition would suffice.
Installation is straightforward, and you can accept the default settings (or tweak them according to your preferences).
IntelliJ Project & Module Setup
After the installation, we will begin with creating a new IntelliJ project to house all the development related files. An IntelliJ project is similar to a workspace in Eclipse (refer IntelliJ IDEA vs Eclipse terminology for further details).
A new project can be created via the Create New Project quick link on the Welcome screen or through File > New > Project menu item.
Select Groovy as the project type.
Once you have a skeleton IntelliJ project, you can then proceed to import CPI developments into it. Each CPI integration flow will be imported as a Module in the IntelliJ project.
As a demo example, the following Integration Flow is created in CPI with just a single step containing the default generated Groovy script.
Download the contents of the Integration Flow from Web UI.
Extract the ZIP file, and move it to the directory of the IntelliJ project.
Next, import the module using File > New > Module from Existing Sources.
Navigate to the directory containing the extracted CPI Integration Flow, and import the module as an Eclipse model. Accept all the default settings provided by the import wizard.
Once the module has been imported successfully, it will have the following directory structure.
As mentioned earlier, adding libraries is one aspect of IntelliJ that particularly stands out compared to Eclipse.
In order to proceed with local development of Groovy scripts in IntelliJ, we would need the following libraries included as dependencies of the module.
- Groovy SDK
- Spock Framework
- CPI libraries
The first two are available from the public Maven repository, while the last one needs to be retrieved from the CPI tenant following the approach detailed in my earlier blog post.
In Eclipse, for non-Maven projects, libraries have to be manually downloaded and added into the classpath. However, IntelliJ provides a handy functionality to automatically download Maven libraries and link them to the IntelliJ project/module.
Go to File > Project Structure. Select Global Libraries, then the “+” button to add a new Global Library from Maven.
Enter org.codehaus.groovy:groovy-all:2.4.12 in the input field, and press OK. This will automatically download the library for Groovy version 2.4.12 (version of Groovy used in CPI at the time of writing) from the public Maven repository.
Once the library has been downloaded, choose the module to add it to.
Back at the Project Structure screen, the downloaded JAR file will be shown.
Repeat the steps above for org.spockframework:spock-core:1.3-groovy-2.4. Spock includes Groovy library, so we need to remove the groovy-all JAR file to prevent conflict with the previous Groovy library.
Local Java libraries
For local JAR files, instead of manually adding each file to the classpath, IntelliJ provides a simplified approach to include a directory containing all the JAR files as a Global Library. The Global Library will be monitored by IntelliJ, and any file changes (additions, deletions) are automatically reflected in the classpath.
Select Global Libraries, then the “+” button to add a new Java Global Library.
Select the directory containing the retrieved CPI libraries to create the Global Library, and subsequently add it to the module.
Next, we need to set the SDK (equivalent to JRE in Eclipse) – Go to Modules, select the Sample1 Module, and click on the Dependencies tab. Initially, it would have no SDK configured, so select the Project SDK for it.
Finally, we need to indicate the directory containing the Groovy source codes. Change to the Sources tab, select root directory and mark it as a sources root.
At this point, if we build the project, the compilation would now be successful. Return to the main view by closing the Project Structure window, then select Build > Build Project.
The Fun Begins 🙂
Now that the (lengthy) setup has been completed, we can start enjoying the benefits of the IDE.
Source Code Assist
To start off, just by viewing the default Groovy script in the IDE, it already provides some suggestions for potential improvements to the code.
To quickly clean up the code, we execute the following commands:-
Code > Optimize Imports
Analyze > Code Cleanup
Local Standalone Testing
To execute local standalone testing, we will use the tester script provided in my earlier post (link here).
First of all, create a new package – right click the root directory of the module, then select New > Package.
Enter src.test.groovy as the package name.
Next, right click on the newly created package and select New > Groovy Script and enter the name of the script.
Copy the source code listing and paste it into the new script – make sure to modify the content for the package and location of the script under test.
Right click on the Tester.groovy, and select Run ‘Tester’.
The first run will fail with an error indicating the script under test is missing. This needs to be fixed by changing the working directory.
Remove src\test\groovy from the working directory, so that it is the root directory of the module.
Now, rerun the test and it should be successful, with the results shown in the bottom console.
Next we can introduce Spock for unit testing following approach described in CPI’s Groovy meets Spock – To boldly test where none has tested before.
To create a Spock Specification, right click on the src.test.groovy package and select New > Groovy Class. Enter GroovyScriptSpecification as name of the class.
Copy the source code listing from GitHub repository here and paste it into the new class – make sure to modify the content for the package and location of the script under test.
As mentioned earlier, IntelliJ provides native support for Spock allowing easy execution of the whole specification or individual tests.
Once the specification/test is executed, the results are shown in the bottom console.
For those who spend significant time and effort working on Groovy scripts in CPI, it is worth exploring IntelliJ IDEA as the development environment. While the initial setup might seem a little lengthy, it is a one-off activity and once completed, you can benefit from the productivity boosts provided by the IDE.
While I have listed some benefits of IntelliJ, it is not meant to be exhaustive as there are many other features that enhance one’s development experience – code inspection, debugging and version control just to name a few. This blog post serves as an introduction to the usage of IntelliJ and I encourage the reader to explore more.