søndag 14. februar 2010

How I use IntelliJ IDEA 9 and Gradle

The newest version of IntelliJ IDEA includes a few very nice gradle features, reference1 reference2 reference3.

Running gradle tasks works by simply opening the build.gradle file and pressing ctrl+shift+f10, this will run the tasks specified as "defaultTasks". If you want to run a specific task you just have to select it and press the same key combination. Nice!

I just wanted to share how I use a few other features as well. Many times I find myself wanting to use tasks that are not directly implemented in my own build script, but added by a plugin. One example is the "eclipse" plugin, since gradle does not (as of version 0.8) support the creation of IntelliJ IDEA project files. This is identified in the missing features section of the user guide:
  • Creating IDE project and classpath files for IntelliJ and NetBeans. Gradle supports IDE project file generation for Eclipse.
So, to be able to run these tasks I have created an "External tools" entry specifically for custom tasks. In IntelliJ open Settings -> External tools and press Add. Point to gradle_home in program. When you add $Prompt$ to Parameters this means you will get an input dialog when you run the task. In Working directory add $ProjectFileDir$, this will make sure it runs from your project directory.


When you run this task you will be able to enter a custom task, like "eclipse" or "-t" (list all available tasks). The tasks you enter here does not need to be specified in "defaultTasks" or directly in build.gradle.

If you use the gradle dependency management you can now easily generate the eclipse project files by running the "custom task" and entering eclipse. In IntelliJ IDEA you can then import these project files by pressing File -> Open Project... and selecting the newly generated eclipse .project-file. IntelliJ will then prompt for your input on how to import the project settings. I usually select Reimport -> Yes (you want to reuse the project files) -> This window.

You have now generated the project files with all dependencies from the build script. Be aware of one issue; make sure you check that the Modules -> Sources under Project Structure is correct. Sometimes it adds a few of the Test Source Folders under Source Folders. Just take a quick look and verify that everything looks correct.

Once you have done it a few times this process only takes a few seconds. Also it makes the build.gradle file the only place you need to keep track of your dependencies. I have been mixing project-local and repository dependencies in several of my own project, and this method works execellent when setting up your development environment.

Lets just hope the next gradle version includes an IntelliJ IDEA plugin...

2 kommentarer:

  1. Thank you.
    I am trying out Gradle 0.9-preview-1 now. Hope it gets the attention it deserves.

    SvarSlett