Skip to content

Steve Hill

5-Minute Intro to Gradle

java, code, build1 min read

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:

1$ brew install gradle
  1. Create Standard Maven Layout

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

1$ 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.

1$ cd my-project
2$ vim build.gradle

The file should look like this:

1apply plugin: 'java' // applies many conventions
2apply plugin: 'idea' // adds tasks to generate project file (with sources!)
1repositories {
2 mavenCentral() // find your dependencies on http://search.maven.org
3}
4
5dependencies {
6 compile 'org.apache.commons:commons-lang3:3.1' // add project dependencies here
7 testCompile 'junit:junit:4.11' // test-only dependencies here
8}
  1. Generate IntelliJ Project with Dependencies

1$ 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!