What Is Maven? Purpose, Advantages and Disadvantages
A build.bat script compiles a project on Windows with javac -cp "lib/gson.jar;lib/slf4j-api.jar" -d out src/com/example/*.java. Copy the same command into build.sh on Linux and it fails with error: package com.google.gson does not exist: on Linux and macOS classpath entries are separated by a colon, not a semicolon. Maven removes this whole class of problems: one pom.xml file and one mvn package command work the same way on any operating system.
Maven (Apache Maven) is a build automation and dependency management tool for Java projects. It downloads the libraries your project needs, compiles the code, runs the tests, packages the result into a jar or war file and generates documentation. The project is described declaratively in a pom.xml file: you state what you want to get, and Maven already knows how to do it thanks to standard conventions.
What Is Maven in Simple Terms
In simple terms, Maven is an automatic builder for a Java project: you describe the project and its libraries once in pom.xml, and from then on a single command such as mvn package downloads the dependencies, compiles the code, runs the tests and produces a ready-to-use jar. No more hand-written build scripts and no more lib folder full of jar files.
Maven is developed by the Apache Software Foundation, written in Java and distributed under the Apache License 2.0. The first version was released in 2004. The name comes from Yiddish and means "expert" or "accumulator of knowledge".
Without a build tool, a small program can be compiled by hand with javac and jar, as shown in the lesson Creating and Running a Java Application. As the project grows, the build command gets longer and longer, so it ends up in a bat or sh script. Such scripts are platform-specific, have to be maintained manually and get copied from project to project. Maven takes a different approach: instead of describing build steps, it follows the principle of convention over configuration. If your code sits in the standard directories, there is almost nothing to configure:
my-app/
├── pom.xml # project description
├── src/
│ ├── main/
│ │ ├── java/ # source code
│ │ └── resources/ # resources (properties, xml, ...)
│ └── test/
│ └── java/ # tests
└── target/ # build output (created by Maven) What Maven Is Used For
Maven in Java covers the whole path from source code to a finished artifact: downloading libraries, compiling, running tests, packaging into a jar or war, installing into the local repository, publishing and generating reports. All of it runs with one command and behaves the same on a developer's laptop and on a CI server.
- Dependency management - libraries and their own dependencies are downloaded automatically using the coordinates in
pom.xml. - A uniform build process - build stages are standardized, so
mvn testormvn packageworks in any Maven project. - A standard project layout - a developer opening someone else's Maven project for the first time immediately knows where the code, resources and tests are.
- Server-side automation - Jenkins, GitHub Actions and GitLab CI run exactly the same command as the developer.
- Reports and quality checks - Javadoc, test coverage and static analysis are added through plugins.
- Multi-module projects - a single parent
pom.xmlbuilds several related modules in the correct order.
Key Maven Concepts
To work with Maven you need seven concepts: POM, artifact, GAV coordinates, repository, dependency, build lifecycle and plugin. Each one is explained in a single line below; the details are covered in separate lessons of this module.
| Concept | What it is | Example | Covered in |
|---|---|---|---|
| POM (Project Object Model) | The project description in pom.xml: coordinates, dependencies, plugins, build settings | pom.xml in the project root | Lesson on the pom.xml file |
| Artifact | Any file Maven produces or downloads: jar, war, pom, sources, Javadoc | gson-2.11.0.jar | Lesson on Maven repositories |
| GAV coordinates | The triple groupId, artifactId, version that uniquely identifies an artifact | com.google.code.gson:gson:2.11.0 | Lesson on the pom.xml file |
| Repository | Storage for artifacts: local (~/.m2/repository), central (Maven Central) or corporate | https://repo.maven.apache.org/maven2 | Lesson on Maven repositories |
| Dependency | A third-party library declared in pom.xml by its GAV coordinates | JUnit, Jackson, a JDBC driver | Section below |
| Build lifecycle | An ordered sequence of build phases; running a phase also runs all the phases before it | compile → test → package → install | Lesson on the Maven build lifecycle |
| Plugin | A module that does the actual work; lifecycle phases only invoke plugin goals | maven-compiler-plugin, maven-surefire-plugin | Lesson on Maven plugins |
One architectural detail is worth remembering: Maven itself can do almost nothing. Compilation is done by maven-compiler-plugin, tests by maven-surefire-plugin, packaging by maven-jar-plugin. Maven only knows which plugin to call in which phase.
A Minimal pom.xml and Your First Build
A minimal working pom.xml contains the POM model version and the project's GAV coordinates; the Java version is set with the maven.compiler.release property. Everything else - directories, jar packaging, standard plugins - is inherited from the built-in parent called the Super POM.
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.examclouds</groupId>
<artifactId>hello-maven</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.release>21</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project> Put a class into src/main/java and run the build command in the project root. Maven compiles the code, runs the tests (if there are any) and creates a jar file:
mvn package # Output (fragment):
[INFO] Building jar: /home/user/hello-maven/target/hello-maven-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------ For Java 17, replace 21 with 17. If the installed JDK is older than the requested version (for example, JDK 17 with release 21), the build stops with Fatal error compiling: error: release version 21 not supported. The meaning of the packaging, build and dependencies tags and of the -SNAPSHOT suffix is covered in a separate lesson of this module on the pom.xml file.
Gotcha: release instead of source/target
Older tutorials set the Java version with the pair maven.compiler.source and maven.compiler.target. That pair checks the language syntax but not the API. The call List.of(1, 2, 3).getFirst() (the method was added in Java 21) compiles fine on JDK 21 with source/target 17, but fails at runtime on Java 17: java.lang.NoSuchMethodError: 'java.lang.Object java.util.List.getFirst()'. With maven.compiler.release set to 17, the same mistake is caught at compile time: cannot find symbol: method getFirst(). On Java 9 and later, use release.
Dependencies: What They Are and Why They Matter
A Maven dependency is a third-party library your project uses; you only need to declare it in pom.xml by its GAV coordinates. Maven downloads it from Maven Central into the local repository ~/.m2/repository and adds it to the classpath for compilation, tests and packaging.
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies> Along with the library, Maven also fetches the library's own dependencies - these are called transitive dependencies. The <scope> element defines where a library is needed: the default compile scope means everywhere, while test means only for tests. To see the full dependency tree of a project, run:
mvn dependency:tree Current Versions and the Maven Wrapper
The current stable line is Maven 3.9.x; running it requires Java 8 or newer, while the Java version used to compile your project is set in pom.xml. The latest release number is always listed on the Apache Maven download page.
Two more facts that often contradict older articles:
- Since January 2020, Maven Central accepts requests over HTTPS only. A URL like
http://repo1.maven.org/maven2in an oldpom.xmlorsettings.xmlresults in a501 HTTPS Requirederror. - Starting with Maven 3.8.1, external HTTP repositories are blocked by default (by the built-in mirror
maven-default-http-blocker).
Installing Maven and verifying the installation with mvn -version is covered in a separate lesson of this module.
Maven Wrapper: one Maven version for the whole team
The command mvn wrapper:wrapper adds the mvnw and mvnw.cmd scripts and the .mvn/wrapper/maven-wrapper.properties file with the Maven version to your project. From then on the project is built with ./mvnw package (on Windows, mvnw.cmd package): the required Maven version is downloaded automatically, and nobody has to install Maven by hand. Commit the scripts and the .mvn directory to Git so that the CI server and every developer build the project with exactly the same version.
Advantages of Maven
The main advantages of Maven are an OS-independent build, automatic dependency management, a uniform project structure and a huge ecosystem of libraries and plugins. That is why Maven remains one of the two standard Java build tools, alongside Gradle.
- Operating system independence. The project builds on Windows, Linux and macOS from the same
pom.xml- no separate bat and sh scripts. - Dependency management. Hardly any project is written without third-party libraries, and those libraries depend on other libraries in different versions. Maven builds the complete dependency tree and resolves version conflicts automatically: if a library appears several times, the version closest to your project in the tree wins ("nearest wins"). This keeps conflicts under control and makes upgrading libraries easy.
- Command-line builds. A single command (
mvn package,mvn verify) runs the whole build - exactly what a CI server needs for automated builds. - Good IDE integration. IntelliJ IDEA, Eclipse (m2e plugin) and VS Code (Extension Pack for Java) open
pom.xmlas a project: they fetch dependencies, set up source folders and run build phases. There is no need to keep IDE-specific settings files in the repository. An overview of these IDEs is in Java Developer Tools. - Declarative project description (POM).
pom.xmldescribes what you want to get, not a sequence of steps. It is easy to read and easy to reuse across projects. - A huge, actively maintained artifact repository. Practically every open-source Java library is published to Maven Central, and Gradle and other build tools use it too.
- Modular architecture and a large number of plugins. Compilation, tests, packaging, code analysis, documentation and Docker image generation are all added through plugins (plugin configuration is covered in a separate lesson of this module).
- Shared conventions. The same directory layout and the same commands in every project make it easier to join a new codebase.
Disadvantages of Maven
The main disadvantages of Maven are a learning curve, "hidden" behavior caused by default conventions, verbose XML and hard-to-read error messages. In addition, Maven is less flexible than Gradle for non-standard build logic, and its incremental builds are slower.
- Learning curve. To use Maven confidently, you need to understand the lifecycle, plugins, dependency scopes and POM inheritance.
- Non-obvious behavior. A lot happens behind the scenes: every
pom.xmlinherits settings from the Super POM, and plugins are bound to phases by default. The commandmvn help:effective-pomshows the final configuration. - Too many plugins. One task often has several plugins of varying quality and maintenance status, which makes choosing hard.
- Hard to troubleshoot when something breaks. Error messages can be long and unhelpful. The
-eflag (exception stack trace) and-Xflag (debug output) give more detail. - Requires internet access or your own artifact repository. The first build downloads dependencies and plugins. Offline mode
mvn -oworks only if everything needed is already in the local repository. - Verbose XML. Even a simple dependency takes five lines, and a large
pom.xmleasily grows to hundreds of lines. - Limited flexibility. Custom build steps must be implemented through existing plugins or by writing your own plugin, whereas in Gradle you can put the logic directly in the build script.
Maven vs Ant vs Gradle
Ant, Maven and Gradle are the three main Java build tools. Ant describes build steps by hand and has no built-in dependency management, Maven defines a standard process and layout through a declarative pom.xml, and Gradle combines Maven's conventions with the flexibility of Groovy or Kotlin scripts and fast incremental builds.
| Criterion | Ant | Maven | Gradle |
|---|---|---|---|
| First released | 2000 | 2004 | 2012 (version 1.0) |
| Build file | build.xml (XML) | pom.xml (XML) | build.gradle / build.gradle.kts (Groovy or Kotlin DSL) |
| Approach | Imperative: you describe every step | Declarative: you describe the project, the lifecycle defines the steps | Declarative, with the option to write logic in the script |
| Dependency management | Not built in (added via Apache Ivy) | Built in, Maven Central | Built in, uses the same Maven repositories |
| Standard project layout | None | Strict (src/main/java etc.) | Same as Maven, but easy to change |
| Rebuild speed | Depends on the script | Medium; parallel module builds with -T | High: incremental builds, build cache, daemon |
| Learning curve | Low for simple tasks | Medium | Steeper: you need to know the DSL |
| Where you see it today | Legacy projects | Enterprise projects, Spring Boot, libraries | Android (official build tool), Spring Boot, large multi-module projects |
When to choose Maven. Maven is a good fit when the project follows the standard "compile, test, package" flow, when predictability and consistency matter, and when the team has mixed experience levels. Gradle wins in large multi-module projects where build speed is critical, and in Android development. For a beginner, Maven is a sensible starting point: Gradle reuses its conventions and terminology (GAV, scope, repositories).
Frequently Asked Questions
Is Maven a framework or a library?
Neither: Maven is a build tool that you run from the command line or an IDE. Unlike frameworks such as Spring, it is not part of your application's jar and is never called from your code.
Do I need to install Maven if IntelliJ IDEA can already build Maven projects?
Not if you only work inside the IDE: IntelliJ IDEA and Eclipse ship with a bundled Maven. A separate installation or the Maven Wrapper is needed to run mvn in a terminal and on a CI server. Step-by-step installation is covered in a separate lesson of this module.
Why does an old project fail with "501 HTTPS Required"?
Its pom.xml or settings.xml points to Maven Central over HTTP, and since January 2020 Maven Central works over HTTPS only. Change the URL to https://repo.maven.apache.org/maven2 or remove the explicit central repository declaration - Maven adds it automatically.
Can I build a Maven project without internet access?
Yes, if all dependencies and plugins are already in the local repository ~/.m2/repository: run the build with the -o (offline) flag. The command mvn dependency:go-offline downloads everything you need in advance. Companies without internet access use a corporate repository manager instead.
Comments