Gradle on Gradle

gradle: the better way to build After a truly awesome presentation about Gradle that Hans Dockter gave at Philly’s Groovy and Grails user group last week, I got really excited about the project. The truth is, if you ever had to do any kind of build work in JVM world, you would feel strict limitations and inflexibility of Maven, hardcore maintenance challenges of ANT ( or ANT + IVY ) with your bones. Well, let your bones relax, here comes Gradle… : )

So where do you start? You can of course go hit the books, or watch some videos, but the true way to learn something is to experience it, at least that is my philosophy.

So here we go, let’s do it, let’s dive in to Gradle “by hand”. I was playing with many examples provided in documentation, but it still felt I am missing a big picture, so I decided to try Gradle out on a real, full blown, mature project. I thought for about 3.14159265 seconds, and decided: “Let me throw Gradle on Gradle itself”. That’s right! Let me get Gradle sources and build them with Gradle.

1. Get Gradle sources

Gradle lives downtown github: http://github.com/gradle/gradle, so getting the sources is as simple as:

$ git clone git://github.com/gradle/gradle.git

and you have it:

$ git clone git://github.com/gradle/gradle.git
Initialized empty Git repository in /home/you/playground/gradle/.git/
remote: Counting objects: 62393, done.
remote: Compressing objects: 100% (15693/15693), done.
remote: Total 62393 (delta 37178), reused 61976 (delta 37004)
Receiving objects: 100% (62393/62393), 10.07 MiB | 190 KiB/s, done.
Resolving deltas: 100% (37178/37178), done.
Checking out files: 100% (2725/2725), done.
$

2. Build “Them Sources”

You thought getting the sources was easy? Well not as easy as to build them. Funny thing is that Gradle is build with Gradle. How is that for eating some dog food? : )

Looking at “Build Gradle from source“, I see that the only thing I should do is:

$ cd gradle/
$ ./gradlew clean assemble

which in fact goes from subproject to subproject and builds the whole thing:

:buildSrc:clean
:buildSrc:compileJava
:buildSrc:compileGroovy
:buildSrc:processResources
 
....                                     # I omitted the rest cause it is _very_ descriptive

I could stop here, but wait, that would not be fun..

3. Meet Gradle Plugin Universe

“So what?” you think, how is it different from “mvn clean install”, or “ant build”? Well, when Maven ‘just works’ (which is 50% of the time on more than 5 people project), this particular step would not be too different. But now I want to look at some pictures to see the project I just built. And for pictures I usually go to IntelliJ : )

Of course you can start IntelliJ, import this project as a “Java project from existing sources”, try to figure out how to set the classpath for a dozen of subprojects, how to create them as modules with good names, add Groovy, Scala, Web Project Facets, etc.. but why to go through such troublesome exercise?

In Grails world, if anybody asks: “Can Grails do that?”, the most definite answer would be: “Of course, there is a plugin for that!” Turns out that Gradle is not much different: you want to create an IntelliJ project? => There is a plugin for that!

But I am here for the first time, so I have no clue what exactly I should be calling to create that IntelliJ project. Well.. why not ask Gradle.

“Dear Gradle, what can you do for me with this project?”

$ gradle -t          # show me all the "tasks", hence "-t", that are available for me to play with
:report
 
------------------------------------------------------------
Root Project
------------------------------------------------------------
Default Tasks: clean, assemble
 
.....  # many more tasks that I omitted here
 
:cleanIdea - Cleans IDEA project files (IML, IPR)
   -> :cleanIdeaModule, :cleanIdeaProject, :cleanIdeaWorkspace
 
:idea - Generates IDEA project files (IML, IPR)
   -> :ideaModule, :ideaProject, :ideaWorkspace
:ideaModule - Generates IDEA module files (IML)
:ideaProject - Generates IDEA project file (IPR)
:ideaWorkspace - Generates an IDEA workspace file (IWS)
 
.....  # many more tasks that I omitted here

Very cool! So I can “Generate IDEA project files (IML, IPR)” and “Clean IDEA project files (IML, IPR)”. ( Notice how “:idea” task explicitly depends on “:ideaModule, :ideaProject, :ideaWorkspace” ). These tasks are available through Gradle IDEA Plugin, which is one of many plugins that come with Gradle. So let’s create that project:

$ gradle idea

4. Gradle in IDE

Once this is done ( takes about 64 seconds ), import it as an IDEA project, and…:

Gradle on Intellij IDEA

Classpath, modules, workspace, etc.. everything was pre-created by Gradle. So what can you do with the project at this point, besides starting committing your ideas, of course : ) ? Well, let’s ask Gradle, but this time using IntelliJ itself:

Gradle IDEA Plugin

Want to take a closer look at what Gradle can do with individual module?

Gradle IDEA Plugin Individual Module

Seems like there are many things that can be done outside of Gradle box, doesn’t it?

If it doesn’t yet, how about posting formal releases to Twitter, Snarl or maybe have it sent as Ubuntu notification? Well, as I mentioned earlier: “there is a plugin for it”:

Gradle Announce Plugin Build

And just to give you a little taste of a Gradle build script, here is a Gradle DSL to build this plugin:

Gradle Announce Plugin Build

This was just a little intro, the real Gradle power is very well documented, and is just plain obvious after 5 minutes actually trying Gradle.

Gradle Away!