Monday, April 5, 2010

Managing Intellij dependencies with Gradle

Here I am once again facing a legacy project in need of some dependency management love, and a general simplification/cleanup of its build process. I have been following the Gradle project for some time and decided to see how it performs. The short answer is very well. I don't want to focus on all the details of building a project with Gradle, but instead look at one very important piece that I've found is often neglected with these build tools: IDE integration.

I am a big fan of Intellij (currently using the latest 9.x), so I was hoping I could easily synchronize my project files with the Gradle build file. This is sort of a make or break feature for me, as having to hand-tune the XML project files every time I want to change a dependency is not practical; doubly so when other developers in the office are making changes to those dependencies and have zero inclination to understand how their project files work. Unfortunately I could find nothing in Intellij or Gradle that would perform this synchronization for me. Fortunately I was able to very easily roll my own!

The XML processing capabilities of Groovy, Intellij's XML based project files, and Gradle's groovy-based dsl form a powerful build trifecta. If you're not already familiar with Gradle then you might want to browse the Build Script Basics. If you're already up to speed let's jump right in:

You can create many different types of libraries in Intellij, but for this we are going to look exclusively at module libraries. They are sets of JARs that are associated to a specific module in your project, and are declared in that module's .iml file. A simplified view of your .iml with a couple of JARs might look something like:




So you can see that in order to setup our Intellij module's dependencies we need to update that orderEntry element. Here is a gradle script that does that. A few dependencies are declared including some maven-style artifacts along with a couple of local directories. These are available in gradle via configurations.compile.asPath. The rest should be pretty straightforward if you are familiar with Gradle and XMLParser.Just execute the intellijSync task and you're good to go!







4 comments:

  1. Hello,

    Nice solution. But, I'm using .idea directory for project structure. Do you know to port it to work on this configuration?

    ReplyDelete
  2. Found a solution for projects that uses .idea directory. Posted on my blog: http://felipecypriano.com/blog/2010/04/08/synchronizing-intellij-idea-dependencies-with-gradle/

    Thanks!

    ReplyDelete
  3. Hey Felipe,
    I'm using intellij 9 which does have a bunch of .xml in the .idea directory. There is also a .iml file in the root of each module, so that's where you can setup your module libraries. You can probably setup project or system level libraries through modifications in the .idea files.

    ReplyDelete
  4. Great post - exactly what I was looking for. Thanks very much.

    ReplyDelete