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:
- The file list. Gradle reads the source sets of the project, so a new class is compiled because it is in src/main/java, not because somebody added it to a command.
- The order. A task declares what it needs, and Gradle works out the order of all of them before it runs the first. The task lines above are that plan being executed.
- The repeat. A task whose inputs did not change is skipped, and the next lesson of this chapter shows the line that says so.
- The dependencies. A jar the project needs is downloaded from a repository and put on the right class path, for compiling or for running, without a script that copies files around.
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
- Gradle versus Maven: the same project built by both tools, and where each one is the better fit.
- The java plugin and its lifecycle: the tasks this lesson ran, one by one.
- Java: the language, with a course of its own.
Автор статьи: Arthur Isaev