Tech and Media Labs
This site uses cookies to improve the user experience.




Gradle For Java Projects

Jakob Jenkov
Last update: 2015-08-10

Gradle comes with a "Java plugin" which provides a set of tasks commonly used when building Java projects. Using the Gradle Java plugin can save you a lot of time when writing your Gradle build script, because you don't have to write all the needed tasks yourself. The Gradle Java plugin provides them for you. In this text we will have a closer look at this Gradle Java plugin.

Adding the Java Plugin

Before you can use the tasks in the Gradle Java plugin you must first add the Java plugin to your Gradle build script. You add the Gradle Java plugin to your Gradle build script by adding this line at the top of the build script:

apply plugin: 'java'

Listing the Java Build Tasks

Once you have added the Gradle Java plugin to your build script you can list the build tasks the Java plugin adds using this command:

gradle tasks

You should see a longer list of tasks, similar to the list below, but with descriptions next to each task (I removed the descriptions to make the output width fit the page):

D:\data\projects\gradle-experiments>gradle tasks
:tasks

-------------------
All tasks runnable
-------------------

Build tasks
-----------
assemble
build
buildDependents
buildNeeded
classes
clean
jar
testClasses

Build Setup tasks
-----------------
init
wrapper

Documentation tasks
-------------------
javadoc

Help tasks
----------
components
dependencies
dependencyInsight
helph
model
projects
properties
tasks

Verification tasks
------------------
check
test

Other tasks
-----------
count
intro
myTask
secondTask
upper

Rules
-----
Pattern: clean
Pattern: build
Pattern: upload

To see all tasks and...

To see more detail...

BUILD SUCCESSFUL

Total time: 11.33 s
D:\data\projects\gradle-experiments>

Building Your Java Project

To build your Java project you can write:

gradle build

This will execute the build task. The build task depends on a lot of other tasks which will be executed before the build task. Here is an example output from executing the build task:

D:\data\projects\gradle-experiments>gradle build
:compileJava
:processResources UP-TO-DATE
:classes
:jar
:assemble
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build

BUILD SUCCESSFUL

Total time: 13.232 secs
D:\data\projects\gradle-experiments>

The build task will compile all your Java classes and create a JAR file with them in. Gradle outputs classes and JAR file to the directory named build inside your project root directory. If the build directory does not exist, Gradle will create it. The compiled classes are written to build/classes and the JAR file to build/libs .

Gradle remembers which tasks that have been executed, and if no file required by a task has been changed, Gradle will not execute that task again. That is why you see the comment UP-TO-DATE next to many of the tasks.

Skipping tasks which has already been executed (the output of these tasks is still valid) makes your builds run faster. If you run the same gradle build command again, the output will look like this:

D:\data\projects\gradle-experiments>gradle build
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar UP-TO-DATE
:assemble UP-TO-DATE
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build UP-TO-DATE

BUILD SUCCESSFUL

Total time: 10.645 secs
D:\data\projects\gradle-experiments>

Notice that the build time listed at the end of the output is almost 3 seconds shorter. This is because the classes, jar and assemble tasks are now all UP-TO-DATE from the last run. 3 seconds may not feel like a lot, but as the amount of code to compile grows, the savings get bigger too.

Cleaning Build Output

Once in a while you might want to rebuild your Java project from scratch. To do so you must first remove all previous build output. You do so using the clean task, like this:

gradle clean

Running the clean task removes the build directory, and thus all compiled classes and created JAR files.

Jakob Jenkov




Copyright  Jenkov Aps
Close TOC