5-Minute Intro to Gradle

September 13, 2013

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.

  1. 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] expansion.

$ mkdir -p my-project/src/{main,test}/java
  1. 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
}
  1. 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!


Profile picture

Written by @sghill, who works on build, automated change, and continuous integration systems.