Build Tools: Maven & Gradle
Build tools like Maven and Gradle download your libraries, compile your code and package it into a runnable JAR with one command.
What you will learn
- Explain why projects use a build tool
- Read a Maven pom.xml and add a dependency
- Build and run a project with mvn or gradle
Why javac is not enough for real projects
For one file, javac and java are fine. But a real project has many files, depends on libraries (ready-made code written by others, such as a JSON parser or a database driver), and needs to be packaged so others can run it. Doing all that by hand — downloading each library, getting the versions right, compiling in the correct order — is slow and error-prone.
A build tool automates the whole job. You list what your project needs, and the tool downloads the libraries (its dependencies), compiles everything, runs your tests, and packages the result into a single runnable file called a JAR (Java ARchive). The two most common Java build tools are Maven and Gradle.
| Tool | Config file | Style |
|---|---|---|
| Maven | pom.xml | XML — very widely used, very standard |
| Gradle | build.gradle | A short script — more concise, popular for Android |
A dependency is a library you depend on
A dependency is simply a library your code needs to run. You do not download it yourself — you declare it in the config file, and the build tool fetches it (and anything it in turn needs) from a giant online store of libraries called a repository (Maven Central). Here is how a single dependency looks in a Maven pom.xml:
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>Note: Output: (No output — this is configuration. The three lines name the library precisely: groupId is the organisation (com.google.code.gson), artifactId is the library name (gson), and version is which release. Maven reads this, downloads Gson 2.11.0 from Maven Central, and makes it available to your code — you never hunt for a download link.)
That groupId / artifactId / version trio is how every Java library is identified, like a postal address for code. When a tutorial says "add this dependency", it is giving you those three values to paste into your config.
Building and running with one command
Once your dependencies are declared, you build with a single terminal command. Maven uses mvn followed by a phase — a named step in the build:
mvn clean packageNote: Output (shortened):
[INFO] Building my-app 1.0
[INFO] Compiling 4 source files
[INFO] Tests run: 3, Failures: 0
[INFO] Building jar: target/my-app-1.0.jar
[INFO] BUILD SUCCESS
mvn clean package did a lot in one line: clean deleted old build output, then Maven compiled your code, ran your tests, and packaged everything into target/my-app-1.0.jar. BUILD SUCCESS means every step passed. If a test had failed, the build would stop and tell you.
The equivalent in Gradle is just as short — gradle build — and produces a JAR in the same way. Whichever tool a project uses, the idea is identical: one command turns your source into a tested, packaged program.
The build-tool workflow, step by step
- Describe the project once in
pom.xml(Maven) orbuild.gradle(Gradle): its name, version and dependencies. - Declare a dependency by adding its groupId / artifactId / version — do not download libraries by hand.
- Run the build with
mvn clean packageorgradle build. The tool fetches dependencies, compiles, and tests. - Get a JAR in the
target(Maven) orbuild(Gradle) folder — a single file containing your packaged program. - Run it with
java -jar target/my-app-1.0.jar, or share that JAR with anyone who has Java.
Tip: You almost never create these config files by hand from scratch. Editors like IntelliJ IDEA and sites like start.spring.io generate a ready pom.xml for you; you then just add dependencies as you need them. Knowing what the file means is what matters.
Watch out: Do not commit the downloaded libraries or the target/build output folder to version control. Those are regenerated by the build tool from your config. Only your source code and the config file (pom.xml / build.gradle) belong in your repository.
Q. What is the main job of a build tool like Maven or Gradle?
✍️ Practice
- Write the three-line dependency block (groupId, artifactId, version) you would add to a
pom.xmlto include a library, using made-up but realistic values. - In your own words, explain what
mvn clean packagedoes at each step (clean, compile, test, package).
🏠 Homework
- Find a real Java library online (for example a JSON or testing library), note its groupId, artifactId and latest version, and write out the Maven dependency block you would paste into a pom.xml to use it.