As I use Gradle in a lot of my projects these days, I thought I would write a few posts about my experiences using it. This first introductory post won’t go into much detail, so if you already know how to use Gradle, even if it’s just the very basics, I would skip this post for now.

Well for me, there are two main reasons:

1. It’s more adaptable than other build languages like maven. It’s commonly referred to as the sweet spot between Maven and Ant, due to it having the useful dependency and life-cycle features that Maven has, as well as the task based options that you get with Ant.
2. The readability of gradle build scripts are a lot easier than both Ant and Maven (at least in my opinion).

To get started with Gradle, you’ll need a copy of Gradle installed which can be found here. The latest version will do for now (at time of writing, latest version was 2.6). Unzip it to somewhere on your machine and add to your path.

1. Windows press “Start + R”
2. Type “control /name microsoft.system” and hit enter
3. Go to “Advanced System Settings”
4. Go to “Environment Variables”
5. Under “System Variables”, select “Path” and hit edit
6. Append this to the end of the variable value: “;\path\to\gradle\bin”
7. Exit everything you opened
8. Open a command prompt and type “gradle -version” to check it’s working

1. Open up a text editor.
2. Open the file ~/.bashrc
3. At the bottom, add: export PATH=\$PATH:/path/to/gradle/bin/:.;
4. Save and close
5. Open up a terminal, and type “gradle -version” to check it’s working

With Gradle installed, all you now need is a build.gradle file, which defines your Gradle build.

Here’s an example that IntelliJ IDEA generates for you when you want a new Gradle Java project:

group 'uk.co.tomkdickinson.scraper' version '1.0-SNAPSHOT'   apply plugin: 'java'   sourceCompatibility = 1.7   repositories { mavenCentral() }   dependencies { testCompile group: 'junit', name: 'junit', version: '4.11' }

Lets take a look at the different components that make the build script:

group 'uk.co.tomkdickinson.scraper' version '1.0-SNAPSHOT'

These are standard gradle properties that indicate the group, and version number of your application. You can find more details on these and other variables in section 13.2.1 of the gradle documentation.

apply plugin: 'java'

Plugins in gradle are what specifies the default build logic for your project. Here we apply the Java plugin which defines a basic Java project. This tells Gradle that the following locations contain your Java code:

Java classes

src/main/java/

Java Unit Tests

src/test/java/

When specifying plugins, there are additional properties you can add to your build.gradle file.

For example:

sourceCompatibility = 1.7

specifies that the source compatibility that gradle uses when compiling your Java classes, in this case, java version 1.7. More of these types of properties can be found in the Gradle documentation for the Java Plugin.

The last part of this build script is all about dependency management. If you are familiar already with dependency management, then Gradle is compatible out of the box with both Maven and Ivy. For those who are not, dependency management is a really useful tool to automatically include other libraries in your project. In the context of Maven, you specify a groupName, artifactId, and version of a particular library you want to include in your project. When your project builds, it will then automatically fetch this library and makes it available in your project.

Lets take a look at our example:

repositories { mavenCentral() }   dependencies { testCompile group: 'junit', name: 'junit', version: '4.11' }

Here we notice two sections: repositories, and dependencies.

Repositories allows us to specify sites that host libraries (called a nexus). mavenCentral() is a built in method in gradle that specifies the central repository. If you want to include local maven artifacts that you may have installed locally, you can include mavenLocal(), or if you want to add a different repository altogether, you can specify:

mvn { url "http://yourrepository" }

Dependencies are where you link your libraries. In our example, IDEA has automatically included junit. As it’s a library we only want when running our unit tests, we specify “testCompile”. This library then won’t be bundled with our final jar when built.

For any other dependency you want to include in your application, you specify:

 compile group: 'groupName', name 'artifcatName', version: 'versionNumber'

To test that the build file works, you can just run “gradle build” from the command line, when you’re in the same directory as your build.gradle

That’s the very basics for using Gradle. I’ll follow up with some more useful functionality in the near future, rather than just constructing a skeleton build file.