Skip to main content

Hello Gradle

·2 mins

Maven is out, Gradle is in, so I have learned recently. Time to take a peek …

First, it needs to be installed. On the Mac, I use sdkman

curl -s "https://get.sdkman.io" | bash
source "/Users/twiss1/.sdkman/bin/sdkman-init.sh"

Also see the official docs.

sdk list gradle
sdk install gradle 3.4.1
gradle -v

Let’s start with a simple Java “Hello World!” that will be compiled and run by using Gradle.

package com.rampmeupscotty.blog;

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

I have put this file into the directory myproject/src/main/java/com/rampmeupscotty/blog

Then I create a file called build.gradle in myproject/app with the following content

apply plugin: 'java'
apply plugin: 'application'

mainClassName = "com.rampmeupscotty.blog.Hello"

In directory myproject I can check what tasks I can run with

gradle tasks -all

The relevant ones are build and run. With a gradle -q run you should get the desired result.

"Hello World!"

It is only half the fun without using Docker. Let’s extend the build.gradle a bit.

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'com.bmuschko:gradle-docker-plugin:3.0.6'
    }
}

apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'com.bmuschko.docker-java-application'

mainClassName = 'com.rampmeupscotty.blog.Hello'

docker {
  javaApplication {
     maintainer = 'The Dude "[email protected]"'
     tag = 'thedude/gradle-docker:latest'
  }
}

The image is being built with gradle dockerBuildImage. After that, run it with docker run -ti thedude/gradle-docker and see what happens.

Fine with the result? Then push the image to the registry gradle dockerPushImage. Without any parameters supplied, the official Docker registry will be used.

Done for today!