Professional Java ToolkitPro· 35 min read

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.

ToolConfig fileStyle
Mavenpom.xmlXML — very widely used, very standard
Gradlebuild.gradleA 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:

Declaring one dependency (the Gson JSON library) in 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:

Maven: clean old output, then compile, test and package into a JAR
mvn clean package

Note: 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

  1. Describe the project once in pom.xml (Maven) or build.gradle (Gradle): its name, version and dependencies.
  2. Declare a dependency by adding its groupId / artifactId / version — do not download libraries by hand.
  3. Run the build with mvn clean package or gradle build. The tool fetches dependencies, compiles, and tests.
  4. Get a JAR in the target (Maven) or build (Gradle) folder — a single file containing your packaged program.
  5. 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?

Answer: A build tool automates the project lifecycle: fetching the libraries you declared, compiling your code, running tests, and packaging everything into a runnable JAR — all from one command, instead of doing it by hand.

✍️ Practice

  1. Write the three-line dependency block (groupId, artifactId, version) you would add to a pom.xml to include a library, using made-up but realistic values.
  2. In your own words, explain what mvn clean package does at each step (clean, compile, test, package).

🏠 Homework

  1. 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.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →