Sub topic 1
What a Build Tool Does for You
You have written a Java class with a main method. You compiled it and ran it. That works for one file. Real projects are bigger. They have many files, tests, and code written by other people. This chapter explains the problem that grows with them and how a build tool solves it.
What "building" means
A build is the set of steps that turns your source code into something people can run. Source code is the text you write in .java files. The computer cannot run that text as it is. It must first be changed into a form the Java Virtual Machine (JVM) understands. The JVM is the program that runs Java code on your computer.
For a typical Java project, a build includes these steps:
- Fetch libraries. A library is code someone else wrote that your project uses, such as a tool for reading JSON. Your project needs a copy of each one.
- Compile. Compiling turns
.javafiles into.classfiles. A.classfile holds bytecode, the instructions the JVM runs. - Test. A test is a small piece of code that checks your code gives the right answer. Running tests catches mistakes early.
- Package. Packaging bundles the
.classfiles into one file, usually a JAR (Java ARchive). A JAR is a zip file with a Java-specific layout. - Share or deploy. The JAR goes to a server, to a teammate, or to a public place where other projects can use it.
A tiny project to think about
Here is the small program this tutorial uses throughout. It lives in a file at src/main/java/com/example/App.java.
package com.example;
public class App {
public static void main(String[] args) {
System.out.println(greeting("World"));
}
static String greeting(String name) {
return "Hello, " + name + "!";
}
}When it runs, it prints Hello, World!. It has one file and uses no libraries. You could build it by hand in a few seconds, and the next chapter shows how. Now imagine the same project a year later. It has 200 files, 15 libraries, 400 tests, and five people working on it. The steps above are the same, but doing them by hand stops being practical.
The problems that grow with a project
Too many commands to remember
To compile by hand, you type a javac command and list your files. You also list where each library lives. That list is called the classpath: the set of folders and JAR files where Java looks for classes. With 15 libraries, the classpath becomes a very long line of text. One typo makes the build fail.
Libraries need other libraries
Many libraries depend on other libraries. Those depend on still more. A dependency your code uses directly is a direct dependency. A dependency that arrives because one of your libraries needs it is a transitive dependency. Adding one library can bring in ten more. Finding and downloading all of them by hand is slow, and it is easy to miss one.
Versions must match
Libraries change over time. Each release has a version number, such as 5.10.2. If you use version 2 and your teammate uses version 3, the code may behave differently on each machine. Sometimes it will not compile at all.
"It works on my machine"
When each person builds in their own way, results differ. One person forgets to run the tests. Another uses a different Java version. The program works on one laptop and fails on the server. A build that gives the same result on every machine is called reproducible.
Wasted time
Compiling everything after a one-line change wastes time. In a large project, a full build can take minutes. You want only the parts that changed to be rebuilt.
What a build tool is
A build tool is a program that runs the build steps for you. You describe your project once in a file. The tool reads that file and does the work every time you ask.
The two most common build tools for Java are:
- Maven. You describe the project in an XML file called
pom.xml. Maven follows a fixed order of steps that every Maven project shares. - Gradle. You describe the project in a script called
build.gradle.kts, written in the Kotlin language. Gradle builds a plan of tasks and runs only the ones that are needed.
Later chapters cover each tool in detail. For now, look at how short the commands are. Each line below runs a complete build of the project: fetch, compile, test, and package.
mvn package
./gradlew buildThe first line uses Maven. The second uses Gradle. Neither command lists any files or libraries, because the build file already holds that information.
By hand versus with a build tool
| Job | By hand | With a build tool |
|---|---|---|
| Find source files | You list them in the `javac` command | The tool looks in standard folders |
| Get libraries | You download each JAR from a website | The tool downloads them from a repository |
| Get libraries' libraries | You track them down yourself | The tool finds them automatically |
| Set the classpath | You type a long path | The tool builds it for you |
| Run tests | You remember to do it | It runs as part of the build |
| Make a JAR | You type a `jar` command | It runs as part of the build |
| Same result for everyone | Depends on each person | The build file is shared, so everyone runs the same steps |
| Skip unchanged work | Not possible by hand | The tool can check what changed |
A repository in this table is an online store of libraries. The best known is Maven Central, which both Maven and Gradle use.
Build tools and conventions
A convention is an agreed way of doing something. Build tools rely on conventions. For example, both Maven and Gradle expect your main code in src/main/java and your tests in src/test/java. If you follow the convention, the build file needs very little setup. You only write down what is different about your project.
This is why the App.java file above sits at src/main/java/com/example/App.java. The folders after src/main/java match the package name com.example. A later chapter covers this layout in full.
What you will learn next
The rest of this tutorial follows the path from simple to complex:
- First, you build by hand so you can see what a tool automates.
- Next, you learn the standard layout and how libraries are named.
- Then you build the same project with Maven and with Gradle.
- Finally, you cover tests, wrappers, plugins, larger projects, and how to choose a tool.
Exercise
Name the five main steps of a typical Java build, in order.
Show answer
Fetch libraries, compile the source code, run the tests, package the result into a JAR, and share or deploy it.
Exercise
Your project uses library A. Library A needs library B to work. Your code never mentions library B. What kind of dependency is library B for your project, and why does this make building by hand harder?
Show answer
Library B is a transitive dependency. Building by hand is harder because you must find out that A needs B, download B yourself, and add it to the classpath. A build tool finds and downloads B automatically.
Exercise
A teammate says, "The program works on my laptop but fails on the server." Give two possible causes that a build tool helps prevent.
Show answer
The two machines may use different versions of a library, or one machine may skip a step such as running the tests. A build tool prevents both. The shared build file fixes the library versions, and every machine runs the same steps in the same order.