Maven is out, Gradle is in— so I 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 Gradle.
package com.rampmeupscotty.blog;
public class Hello {
public static void main(String[] args) {
System.out.println("Hello Gradle");
}
}
I put this file in 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 the myproject directory I can check what tasks are available with:
gradle tasks -all
The relevant ones are build and run. Running gradle -q run should produce:
Hello Gradle
It ‘s only half the fun without Docker. Let’s extend 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 "thedude@email.com"'
tag = 'thedude/gradle-docker:latest'
}
}
Build the image with gradle dockerBuildImage. After that, run it with:
docker run -ti thedude/gradle-docker
Happy with the result? Push the image to the registry with gradle dockerPushImage. Without any parameters, the official Docker registry will be used.
Done for today!