Easy Gradle Intro for Java, Windows

Gradle is a hugely popular build tool. It may seem intimidating if you don’t know it, but getting started is very simple.

Install Gradle

Gradle is a “copy install” – unpack the zip file and add the distro’s build directory to your path. See https://gradle.org/install/#manually

After you unpack Gradle and update PATH, test your work by opening a command window and entering

gradle ‐version

Init a Gradle Project

Create a directory, cd into it, and execute

gradle init ‐‐type java-library

This command creates a template project with the directory structure for developing and testing a java library. The project template includes a simple example library file, a test file, and a Gradle build script, build.gradle.

On my system, the generated build.gradle file is:

 * This build file was generated by the Gradle 'init' task.
 * This generated file contains a sample Java Library project to get you started.
 * For more details take a look at the Java Libraries chapter in the Gradle
 * user guide available at https://docs.gradle.org/4.3.1/userguide/java_library_plugin.html

// Apply the java-library plugin to add support for Java Library
apply plugin: 'java-library'

// In this section you declare where to find the dependencies of your project
repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.

dependencies {
    // This dependency is exported to consumers, that is to say found on their compile classpath.
    api 'org.apache.commons:commons-math3:3.6.1'

    // This dependency is used internally, and not exposed to consumers on their own compile classpath.
    implementation 'com.google.guava:guava:23.0'

    // Use JUnit test framework
    testImplementation 'junit:junit:4.12'

Test your new project and build file by executing

gradle build

You should see some output indicating download and compilation, followed by “Build Successful”.

Adding Source Files

You can add java source files to your project by placing them under directory


As with any java development, subdirectories in the sourcetree should map to java packages.

You do not enumerate your source files in build.gradle. Instead, gradle automatically examines the source directory and its descendents.

Convert to an Application

The default project is for creating and testing a Java library. But if you would like your project to create an executable Java application, do the following:

Add a class with signature “public static void main(String args[])” to your project, under directory src\main\java. E.g.,

public class HelloGradle
  public static void main(String[] args)
    System.out.println("Hello gradle!");

After creating file HelloGradle.java, test that it compiles by entering “gradle compileJava“.

Next, edit build.gradle so that you can specify use a “run” target.  First, near the existing “apply plugin” line, add two lines

apply plugin: 'application'
mainClassName = 'HelloGradle'

(If your main class is in a package, then include the package name in the class name. For more details on Gradle’s application plugin, see The Application Plugin).

Test new configuration by entering

    gradle clean
    gradle run

Note that the “run” target will download dependencies and compile source if necessary.

Expect the output to end with:

> Task :run
Hello gradle!

2 actionable tasks: 2 executed

Create Eclipse Project

If you would like to be able to edit and debug your project in the Eclipse IDE, add the following line to build.gradle:

apply plugin: 'eclipse'

The “eclipse” plugin includes the target “eclipse”. After it is in build.gradle, you will be able to execute

gradle eclipse

…which creates configuration files for Eclipse, including paths to packages imported by the project. For example, in this project, the junit libraries are imported.

After running gradle eclipse, you should be able to import your project into eclipse by selecting “Open Projects from File System” from the eclipse “File” menu.


If you change the dependency specifications in build.gradle, rerun gradle eclipse.


The power of Gradle is the ease in which it manages external library dependencies and imports, which we’ve not addressed. But the simple steps above allow you to create a project, including a Gradle build file, that will allow you to build, test, and run your code, and can serve as a stepping-stone for more implementing more complex projects.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s