5-Minute Intro to Gradle
— java, code, build — 1 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.
- 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
- 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
- Create Build File
It's a very simple text file at the root of your project. Any text editor will do.
1$ cd my-project2$ vim build.gradle
The file should look like this:
1apply plugin: 'java' // applies many conventions2apply plugin: 'idea' // adds tasks to generate project file (with sources!)
1repositories {2 mavenCentral() // find your dependencies on http://search.maven.org3}45dependencies {6 compile 'org.apache.commons:commons-lang3:3.1' // add project dependencies here7 testCompile 'junit:junit:4.11' // test-only dependencies here8}
- 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!