Sub topic 1
What a Build Tool Does for You
A Java program starts as text files that you write. Before anyone can run it, those files have to go through several steps. A build tool is a program that does those steps for you, in the right order, every time. This tutorial covers the two most popular build tools for Java: Maven and Gradle.
The Steps Between Code and a Working Program
When you "build" a Java project, a few things happen:
- Compile. The compiler, called
javac, turns your.javasource files into.classfiles. A class file holds bytecode, which is code the Java Virtual Machine can run. - Test. Small programs called unit tests check that your code works.
- Package. The class files are bundled into a JAR file. A JAR (Java ARchive) is one file that holds a whole program or library.
- Fetch dependencies. A dependency is someone else's code that your project uses, such as a library for reading JSON. It has to be downloaded and made available to your code.
You can do all of this by hand with JDK commands. The next chapter shows how. For a tiny program that is fine. For a real project it gets slow and it breaks easily.
Why Doing It by Hand Goes Wrong
Picture a project with 200 source files and 15 libraries. Doing it by hand means you have to:
- Type long commands that list every library file.
- Download each library yourself and pick the right version.
- Find the libraries those libraries need, too.
- Remember to run the tests before you package.
- Make sure your teammates run exactly the same steps.
If one person skips a step or uses the wrong library version, the program may work on their computer and fail on someone else's.
What a Build Tool Gives You
| Problem by hand | What the build tool does |
|---|---|
| Long, error-prone commands | One short command, such as `mvn package` |
| Downloading libraries yourself | Downloads them from an online repository |
| Missing libraries of libraries | Finds and adds them automatically |
| Forgetting to run tests | Runs tests as a normal part of the build |
| "Works on my machine" | Every person and server follows the same written steps |
The key idea is that you describe your project in a build file. Maven uses pom.xml. Gradle uses build.gradle.kts. The build file says what the project is called, which libraries it needs, and how to build it. The tool reads that file and does the work.
Maven and Gradle at a Glance
Both tools solve the same problems. Maven uses a fixed, standard set of steps, and you configure it with XML. Gradle is more flexible, and you write its build files in a programming language (Kotlin or Groovy). Later chapters teach each tool and then compare them.
Exercise
Name three jobs a build tool does that you would otherwise do by hand with javac and jar.
Show answer
Any three of these: compiling the source files, running the unit tests, packaging class files into a JAR, and downloading dependencies (plus the libraries those dependencies need).
Exercise
Why does a build file help a team avoid "it works on my machine" problems?
Show answer
The build file writes down the exact steps and library versions. Everyone, including build servers, runs the same instructions, so builds come out the same everywhere.