What Maven does and why

Maven · Essentials · 7 min

Every Maven command makes sense once you know the chores it takes off your hands.

So far you have compiled programs with javac and run them with java. That works for a few files. A real project has hundreds of files, uses code other people wrote, and must be built the same way by everyone on the team. Typing those commands by hand stops working fast.

A library is ready-made code someone else wrote, such as code that reads spreadsheets or sends email, which you use from your own program instead of writing it yourself. When your project needs a library, that library is one of its dependencies. Libraries often need other libraries in turn.

Java libraries are shipped as JAR files (Java ARchive): one zip file holding compiled .class files. To compile and run code that uses a library, javac and the JVM must be told where its JAR is. That list of places to look for classes is the classpath, given with the -cp option.

Maven is a build tool: a program that does all of this from one command. You describe your project once, in a file called pom.xml: its name, its version and the libraries it needs. Maven then downloads those libraries, compiles your code, runs your tests and packs the result into a JAR of your own.

Where do the libraries come from? A store of published libraries is called a repository. The main one is Maven Central, a huge public online repository that anyone can download from.

Maven saves every file it downloads in a folder on your computer, the local repository: ~/.m2/repository (~ means your own user folder). Each version is downloaded only once, and all your projects share it.

is the other common Java build tool, but Maven is the most widespread, and nearly every Java tool understands its way of naming and storing libraries.

Example

# By hand: name every library JAR yourself, in every command
# (-cp gives the classpath, -d says where to put the .class files)
javac -cp libs/csv-reader.jar:libs/text-utils.jar -d out src/App.java
java  -cp out:libs/csv-reader.jar:libs/text-utils.jar App

# With Maven: libraries are listed once in pom.xml
mvn package            # download libraries, compile, test, build a JAR
ls ~/.m2/repository    # every library Maven has downloaded so far
mvn --version          # check Maven is installed

Remember

  • A library your project needs is a dependency; Java libraries ship as JAR files
  • Maven downloads dependencies, compiles, tests and builds a JAR with one command
  • Libraries come from Maven Central and are kept in the local repository, ~/.m2/repository

Watch out for

  • Copying library JAR files into the project folder by hand; list them in pom.xml and let Maven fetch them.
New words in this lesson (2)
Gradle
A build tool for Java and Kotlin, configured with a script (build.gradle.kts). It downloads libraries, compiles, runs tests and packages a project. Learn it →
POM
Project Object Model: the pom.xml file in a project's top folder. It tells Maven the project's name and version, which libraries it needs, and how to build it. Learn it →

Check yourself

  1. Your program uses library A, and A itself uses library B. What does Maven do when you build?
  2. Where does Maven keep the libraries it has downloaded?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: maven.apache.org/what-is-maven.html

Builds on

Nothing: this is a foundation.

Unlocks