Monday, December 14, 2009

Intellij 9 and Gradle

One of the hidden gems of the Intellij 9 release is it's support for Gradle. Some of the information on the web is out of date and some features are not intuitive. This post will detail some of the nuances and follow it up with a wish list for the next update :)

Gradle Support
The information from JetBrains is out of date. In order to setup gradle after the GA release, all you need to do is configure the gradle home under File -> Other Settings -> Template Setttings.


Gradle Intellij Nuances
After setting up IDEA to work with Gradle, you'll need a build file. I didn't discover a way to create a gradle build file from IDEA. Just create a new file and name it "build.gradle" in the root of the module directory structure.

When writing the build script, Intellij expects to work with gradle in a way that is depreciated. It will pick up on a createTask(..) method. If the cursor is on a task such as this, and you hit ctrl+shft+F10, it will auto-discover and run that task in the script. (cool stuff)

Hotkeys to know:
ctrl+shft+F10 - runs task the cursor is on
shft+F10 - runs the last script (or the last run)
alt+shft+F10 - pops up a run menu (with all previously run gradle run configs)

The figure below shows an alt+shft+F10 when the cursor is on a task called task2.


If you code gradle in a non-depreciated way... then IDEA does not auto-discover the tasks. Below is what one would expect in a gradle build file:

usePlugin 'java'

repositories {
mainCentral()
}

// from the gradle user guide
// http://www.gradle.org/0.8/docs/userguide/tutorial_using_tasks.html
task hello << {
println 'Hello world'
}
task intro(dependsOn: hello) << {
println "I'm Gradle"
}

Tasks would normally be created this way... It is still great that IDEA has gradle support in general regardless of this oversight. After a run configuration is configured with a targeted task, it worlds with no pain. Also you could add:
 defaultTasks "intro"
and gradle runs this as the fallback when IDEA doesn't provide the required task.

Intellij / Gradle features I'm looking forward to:
  1. IDEA understands proper task configuration
  2. IDEA makes it easy to create new projects with a structure maven and gradle expects.
  3. IDEA treats gradle as a peer with Maven and Ant. This includes:
    a. Gradle Window (similar to the ant build and maven projects panels)
    b. Before Launch Gradle tasks in the run configurations

2 comments:

Peter Gromov said...

You could file requests for all the features you want IDEA to have. Moreover, you could also submit patches. In particular, extending Ctrl+Shift+F10 to know not only about createTask should be simple.

tania said...

Submitting patches was difficult for me, how can I make ti simple?

- Tanya
Web Design Firm