What Gradle is and what problem it solves

A build is a short sequence of steps that turns source files into something a machine can run: compile the sources, put the classes and the resources in an archive, name the class that starts the program, and run it. Every step is one command of the JDK, and this lesson runs them by hand first. Then it does the same thing with Gradle, and the difference between the two runs is what a build tool is for.

The build by hand

Two classes are enough: one that builds a string and one with a main method. They sit in the folder layout every Java project uses, src/main/java, so the same files serve the hand build and the Gradle build below.

src/main/java/com/example/Greeter.java

package com.example; public final class Greeter { public static String greet(String name) { return "Hello, " + name; } }

src/main/java/com/example/App.java

package com.example; public final class App { public static void main(String[] args) { System.out.println(Greeter.greet("Gradle")); } }

javac -d compiles both files into a folder per package, jar --create packs that folder and names the entry point in the manifest, and java -jar runs the archive.

javac -d out src/main/java/com/example/Greeter.java src/main/java/com/example/App.java

jar --create --file hello.jar --main-class com.example.App -C out .

java -jar hello.jar

Hello, Gradle

Three commands, and every one of them has to be repeated, in this order, by every person and every machine that builds the project. The jar command names one file of the folder; a second source file that is not listed leaves a class out of the archive, and the mistake only shows when the program runs.

The same project in Gradle

Gradle keeps the same sources and replaces the three commands with two small files. The settings file names the project, the build script names the plugins, and the java plugin brings the compile, test and jar tasks with it.

settings.gradle

rootProject.name = 'hello'

build.gradle

plugins { id 'java' id 'application' } version = '1.0.0' application { mainClass = 'com.example.App' }

gradle build --console=plain --offline --no-daemon

> Task :compileJava
> Task :processResources NO-SOURCE
> Task :classes
> Task :jar
> Task :startScripts
> Task :distTar
> Task :distZip
> Task :assemble
> Task :compileTestJava NO-SOURCE
> Task :processTestResources NO-SOURCE
> Task :testClasses UP-TO-DATE
> Task :test NO-SOURCE
> Task :check UP-TO-DATE
> Task :build

BUILD SUCCESSFUL in 15s
5 actionable tasks: 5 executed

The task lines are the steps Gradle ran: compileJava compiled the sources, processResources found no resources to copy, jar packed the classes, and build asked for all of it. The archive is in build/libs, named after the project and its version.

ls build/libs

hello-1.0.0.jar

Run it

The application plugin adds the run task, which starts the class named by mainClass with the runtime class path of the project:

gradle run --console=plain --offline --no-daemon

> Task :compileJava UP-TO-DATE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE

> Task :run
Hello, Gradle

BUILD SUCCESSFUL in 10s
2 actionable tasks: 1 executed, 1 up-to-date

The archive does not run on its own yet, and the error says why:

java -jar build/libs/hello-1.0.0.jar

no main manifest attribute, in build/libs/hello-1.0.0.jar

The application plugin writes the entry point into the start scripts of its distribution, not into the jar, and the jar task of the java plugin writes no Main-Class of its own. The hand build above named it with --main-class; in Gradle it is one attribute in the jar block, and The jar and its manifest adds it and runs the same archive without Gradle.

What the build tool took over

The two commands that named files by hand are gone, and with them four kinds of work that a hand build leaves to the person at the keyboard:

That is the whole promise: the build is written down once, in a file the repository keeps, and every machine that runs the same command gets the same build out of it.

Related

Автор статьи: Arthur Isaev