5-Minute Intro to Gradle

Dependencies want to be managed! Gradle makes it easy, even if you’ve been intimidated by other tools before. Let’s get a new project up and running with Apache Commons, JUnit and IntelliJ IDEA.

0. Install Gradle

Head on over to http://gradle.org to download the version for your platform. If you’re a Mac/Homebrew user, it’s this easy:

$ brew install gradle

1. Create Standard Maven Layout

Note The bit of awesomeness between the braces is called shell expansion.

$ mkdir -p my-project/src/{main,test}/java

2. Create Build File

It’s a very simple text file at the root of your project. Any text editor will do.

$ cd my-project
$ vim build.gradle

The file should look like this:

apply plugin: 'java'                                // applies many conventions
apply plugin: 'idea'                                // adds tasks to generate project file (with sources!)

repositories {
    mavenCentral()                                  // find your dependencies on http://search.maven.org
}

dependencies {
    compile 'org.apache.commons:commons-lang3:3.1' // add project dependencies here
    testCompile 'junit:junit:4.11'                 // test-only dependencies here
}

3. Generate IntelliJ Project with Dependencies

$ gradle idea

Now all that’s left is to open my-project.ipr. The folders are correctly set up, the sources for your libraries are available, and your dependencies don’t need to be included in your build system.

For web-related fun, check out the war plugin. If you’re an eclipse user, check out the eclipse plugin. Happy Gradling!


Published Sep 13, 2013