Gradle first steps - rioantirio1/xtoyinzmin GitHub Wiki

A highly opinionated introduction to using Gradle

Situation:

X = Newbie
Y = Building projects using Gradle
Z = 30 min

Gradle is a build automation tool for multi-language development … Java, Python, C++ or the language of your choice (although there is no need to build in Python!)

  • Build large or small projects
  • Uses language Groovy
    • Uses closures (blocks of code that can be passed as variables and executed later)

References

Setting up Gradle

Option #1 -- From the command line

From the command line

$ brew install gradle
$ cd /to/somewhere
$ gradle init

Option #2 (preferred) -- using Intellij Idea

On Intellij Idea, things are then nicely set up for a noob

  • For one, it gradle is built-in — so don’t need to install gradle from the command line
  • Also starts off a minimal build.gradle for you

Lets get this working on Intellij Idea:

We will create a new project:

  • New Project > [Check] Gradle > [Uncheck] Java
  • Our project will be mybox
  • Specify a directory location, ending with our project name mybox (we always start clean — this will be an empty folder in the beginning)
    • Say /path/to/mybox
  • Provide GroupId (e.g. my.sandbox) <— my convention: company domain [dot] something
  • Provide artifactid (e.g. mybox) <- what you’re building
  • You get a project mybox, which contains the basic file build.gradle in it, which looks like:
group 'my.sandbox'
version '1.0-SNAPSHOT'
  • Now lets add some fun plugins to build.gradle:
plugins { 
   id ‘java’ 
}

(that is, if you're going to use Java)

  • Now add src / main / java directories under the project (mybox) folder. So you’ll have the tree:
    • mybox > src > main > java
  • Might as well add src / main / resources — this is where you keep other stuff, like test data
    • mybox > src > main > resources
  • This (above) is the standard project setup
  • Now you can add package, a class, etc, etc and get going with your project...

Project #1 -- Hello, World!

We’re gonna do Hello World!

First steps

  • Add a package (my.sandbox) under mybox > src > main > java
  • Add a class HelloWorld under my.sandbox. You should now have:
package my.sandbox;

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

Build and run

On command line, under /path/to/mybox, lets build the project using gradlew

$ ./gradlew build

It should build ok. Then run

$ ./gradlew run

This will probably fail, since gradle doesn’t know which “main” to run — there could be many starting points in the project. Likely error:

Task 'run' not found in root project ‘mybox’

Wiring things up properly in build.gradle

Lets add a plugin application and specify the main class in build.gradle. The final build.gradle now looks like:

// full build.gradle — updated
plugins {
   id 'java'
   id 'application'
}
application {
   mainClass = 'my.sandbox.HelloWorld'
}
group 'my.sandbox'
version '1.0-SNAPSHOT'

Now build and run should succeed

$ ./gradlew build
$ ./gradlew run

.
.
.

> Task :run
Hello World

BUILD SUCCESSFUL in 1s
2 actionable tasks: 2 executed

Project #2 -- Hello World! with tests

Even a minimal project should have tests. We will set things up nicely with JUnit.