Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 41 ยท Getting Started

What Is Maven?

Learn what Apache Maven is, which build problems it solves, how convention over configuration works, and how it compares with Ant and Gradle.

Apache Maven is a build tool for Java projects. You describe your project in one XML file, the POM (Project Object Model), and Maven knows how to compile it, test it, package it into a JAR and publish it. It also downloads the libraries your code depends on, so you never copy .jar files into a folder by hand.

Maven has been the default way to build Java for about two decades. Almost every open-source Java library is published in a Maven repository, almost every IDE understands a pom.xml, and most CI servers run mvn out of the box. Even if you later move to Gradle, you will keep meeting Maven coordinates and Maven repositories. This lesson explains the problems Maven solves, the one big idea behind it, and what you can and cannot run on this site.

What a build tool does

Compile Turn source files into classes.
Test Run the tests and stop on failure.
Package Bundle classes into a JAR or WAR.
Dependencies Download the libraries you use.
1 / 2

The problem a build tool solves

Imagine a small Java program with ten source files. You can compile it by running javac yourself. Now imagine a hundred files, three libraries, a test suite and a colleague who uses a different operating system. You would need to answer a lot of questions:

  • Where are the source files, and where do the compiled classes go?
  • Which libraries does the code need, and which versions? Do those libraries need other libraries?
  • How do you run the tests, and how do you know they passed?
  • How do you produce the final JAR, and what goes into it?
  • How does a new teammate (or a CI server) do all of that with one command?

Before build tools, every team answered these questions with its own shell scripts. Each project had a different folder structure and a different way of running things. Moving between projects meant relearning everything.

Convention over configuration

Maven's answer is convention over configuration. Instead of asking you to describe every step, Maven assumes a sensible default for everything:

  • Your production code lives in src/main/java.
  • Your tests live in src/test/java.
  • Compiled classes go into target/classes.
  • The finished JAR goes into target/.
  • A project has a fixed set of steps (compile, test, package, ...) called the build lifecycle.

If your project follows these conventions, your POM can be tiny. You only write down what is specific to your project: its name, its version and its dependencies. Here is a complete, valid POM for a library that compiles and packages:

XML
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example</groupId>
  <artifactId>greeter</artifactId>
  <version>1.0.0</version>
</project>

With only that file and a src/main/java folder, mvn package produces target/greeter-1.0.0.jar. There is no compile target, no classpath, no jar task. They all come from conventions built into Maven.

The trade-off is flexibility. If you want a different folder layout, Maven allows it, but you pay in configuration and in surprise for the next person who opens the project. In practice, almost everybody accepts the defaults, and that is exactly why a Maven project you have never seen before is usually easy to read.

The three things Maven manages

Maven is built around three ideas that the rest of this tutorial explores in detail.

  1. The project model. The POM describes what the project is: its coordinates (groupId, artifactId, version), its packaging and its dependencies. See The POM File.
  2. The build lifecycle. A fixed sequence of phases such as compile, test and package. You ask for a phase, and Maven runs every phase before it. See Lifecycles and Phases.
  3. Plugins and goals. Maven itself does almost no work. Each phase runs goals provided by plugins: the compiler plugin compiles, the Surefire plugin runs tests, the JAR plugin builds the archive. See Plugins and Goals.

On top of those sits dependency management: you declare a library by its coordinates, and Maven downloads it (and everything it needs) from a repository such as Maven Central. See Adding Dependencies.

Maven compared with Ant and Gradle

You will hear two other names in every discussion of Java builds.

Apache Ant is the older tool. An Ant build file lists targets (compile, jar, clean) and the exact tasks inside each one. Ant is flexible and has no conventions, so every Ant build is different. It has no built-in dependency download; that came later through an add-on called Ivy. Maven was created largely as a reaction to this: standard layout, standard lifecycle, standard dependency handling.

Gradle is the newer tool. Its build files are programs, written in Groovy or Kotlin, and it models a build as a graph of tasks. Gradle is very flexible and often faster on large builds because of its incremental design. It also reuses Maven repositories and coordinates, so a Gradle project depends on libraries the same way a Maven project does.

Ant Maven Gradle
Build file XML targets and tasks XML POM (declarative) Groovy or Kotlin script
Default layout none src/main/java, src/test/java same as Maven
Dependencies add-on (Ivy) built in built in
Style you describe the steps you describe the project you program the build

Neither Maven nor Gradle is "the correct one". Maven's strength is predictability: a declarative file that tools and humans can read without running anything. You will compare the two properly in Maven vs Gradle.

What is real and what is a stand-in on this site

Maven is a command-line program that needs a JDK installed, writes to your disk and downloads files from the internet. A web page cannot run it. So this tutorial uses two kinds of code blocks, and it is honest about which is which.

  • Plain code blocks hold the real thing: XML for pom.xml, shell commands such as mvn package and sample console output. You copy these to your own machine. They are read-only here.
  • Runnable <TryIt> blocks are stand-ins: small Java programs that model one idea from inside, for example "map file paths to Maven's conventions" or "derive the steps from the packaging". They run on the site, but they are teaching models, not Maven itself. Each lesson tells you what the model leaves out.

The first stand-in is a toy "build tool" that applies Maven's folder conventions to a list of file names. It does not read your disk; it only shows how much a convention can decide from a path alone.

Java A toy convention engine

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Notice that no file needed any extra configuration: the path alone decided what to do. That is the whole idea of convention over configuration, reduced to five if lines.

The lifecycle follows from the packaging

The second idea is that Maven decides the steps for you. Each packaging type (jar, war, pom) comes with a default list of goals. The model below mirrors the real default bindings for three packagings. The goal names (compiler:compile, surefire:test, jar:jar) are real Maven goals.

Java Packaging decides the steps

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The real Maven also binds install:install and deploy:deploy to later phases, which the model leaves out. You will meet the full table in Plugins and Goals.

What Maven is not

A few clarifications save confusion later.

  • Maven is not a Java framework. Your code does not import Maven; Maven only builds it.
  • Maven is not an IDE. IntelliJ IDEA, Eclipse and VS Code read your pom.xml and use Maven behind the scenes, but the build also works from a plain terminal.
  • Maven does not run your application in production. It produces the artifact (a JAR or WAR), and you deploy that.

You will see the words "Maven project" used loosely. A project is just a folder with a pom.xml in it. If you can run mvn package there and get a JAR, it is a Maven project.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does "convention over configuration" mean in Maven?

    Maven ships with defaults for folders, lifecycle and plugins. You describe only what is specific to your project, such as its coordinates and dependencies.
  2. 2. Where does Maven expect your production Java source files by default?

    The standard layout puts production code in src/main/java and tests in src/test/java. target/classes is where compiled output goes.
  3. 3. Which statement about Maven and Gradle is true?

    Both tools resolve libraries from Maven repositories such as Maven Central. Maven uses a declarative XML POM, while Gradle uses a Groovy or Kotlin script.
  4. 4. On this site, what is a runnable Java TryIt in the Maven tutorial?

    Maven needs a JDK, a disk and a network, so it cannot run here. The TryIts are small Java models, and each lesson says what they leave out.

Summary

  • Maven is a build tool that compiles, tests, packages and publishes Java projects, and downloads their dependencies.
  • Its central idea is convention over configuration: standard folders, a standard lifecycle and a short POM.
  • The POM describes the project; the lifecycle gives the steps; plugins do the actual work.
  • Ant has no conventions, Gradle uses build scripts, and Maven uses a declarative POM; all three can use Maven repositories.
  • On this site, real commands are read-only and the TryIts are honest Java stand-ins.

Next, set up your machine: Installing Maven and the Wrapper.