October 18, 2009

Use Gradle Support in IntelliJ IDEA Community Edition

IntelliJ IDEA Community Edition supports Gradle scripts. We can create a Gradle script in the IDE and run it with a Run Configuration. Let's see how we do this in this blog post.

First we must define where we have Gradle installed on our local computer. We go to File | Settings. Here we select Gradle from the left navigation menu.

We can type the complete path to the Gradle directory or click on the ... button to select the directory graphically.

Once we have set the Gradle directory we are ready to create our first Gradle build script. In our project we add a new file build.gradle. We right-click on the project and select File | New File. We use the name build.gradle and press the OK button. In the root of our project we now have the file build.gradle. We open the file and type the following simple hello task definition:

task hello << {
    println 'Hello world'

But how do we execute our hello task? We need to pass hello to the command-line and we can do this with a Run configuration. We go to Run | Run... and IntelliJ IDEA opens a dialog window. We can change the name here, so it easy to see what we are running if we want to run this command again. Let's change the name to build::hello. And we must set hello at the Script Parameters: input field.

To execute our task we only have to press the Run button and IntelliJ IDEA runs our tasks. We look at the Run window to see the ouput of our task:

We can re-run the task by going to Run | Run... and select build::hello from the list of configurations.