What Maven is and why you need it

Maven is a build automation tool that manages your project's dependencies, compiles your code, runs tests, and packages everything into a deployable format. If you're writing Java applications, Maven handles the repetitive work of downloading libraries, organizing folder structures, and coordinating build steps — so you don't have to do it manually each time.

Maven works by reading a single configuration file called pom.xml (Project Object Model). That file tells Maven what libraries your project needs, what version of Java to use, and what steps to run when you build. Once Maven is installed on your machine, any project with a pom.xml file can be built the same way, on any operating system.

You'll know you need Maven when you start a new Java project and realize you're downloading JAR files by hand, or when you join a team project that already uses it. Maven is standard in professional Java development, and most frameworks (like Spring) assume you have it.

Key Takeaways

  • Maven requires Java to be installed first — check your Java version before you start, because Maven won't work without it.
  • Download Maven from apache.org, extract it to a folder on your computer, and add that folder to your system PATH so you can run Maven from any terminal.
  • On Windows, you set PATH through System Properties; on Mac and Linux, you edit your shell configuration file (.bash_profile, .zshrc, or .bashrc).
  • After installation, run mvn --version in a terminal to confirm Maven is working and to see which Java version it's using.
  • If Maven doesn't run, the most common cause is that Java isn't installed or your PATH isn't set correctly — check both before troubleshooting further.

Check that Java is installed first

Maven runs on top of Java, so you must have Java installed before Maven will work. Open a terminal (Command Prompt on Windows, Terminal on Mac or Linux) and type:

java -version

If you see a version number like java version "17.0.1", you're ready to move forward. If you see "command not found" or "is not recognized", you need to install Java first. Download the Java Development Kit (JDK) from oracle.com or use a package manager like Homebrew on Mac (brew install openjdk) or apt on Linux (sudo apt install openjdk-17-jdk). Maven needs the JDK, not just the Java Runtime Environment (JRE), because it compiles code.

Maven works with Java 8 and newer versions. If you have an older version and can't upgrade, some older Maven versions may still work, but you'll run into compatibility problems with modern libraries. Check your project's documentation to see what Java version it requires.

Download and extract Maven

Go to maven.apache.org/download.cgi and download the binary ZIP file (not the source). The file is named something like apache-maven-3.9.6-bin.zip. The version number changes, but always download the latest stable release.

Extract the ZIP file to a folder on your computer. On Windows, many people use C:\Program Files\Apache\maven or C:\maven. On Mac and Linux, a common location is /usr/local/maven or ~/maven (in your home directory). The exact location doesn't matter — what matters is that you remember the path, because you'll need it in the next step.

Inside the extracted folder, you'll see a bin directory. That's where the mvn command lives. Your PATH needs to point to this bin folder so your terminal can find it.

Add Maven to your PATH on Windows

Right-click "This PC" or "My Computer" on your desktop and select "Properties". Click "Advanced system settings" (or search for "environment variables" in the Start menu). Click "Environment Variables" at the bottom of the window.

Under "System variables", click "New" and create a variable named MAVEN_HOME with the value set to your Maven folder path — for example, C:\Program Files\Apache\maven. Click OK.

Now find the "Path" variable in the System variables list, click it, and click "Edit". Click "New" and add %MAVEN_HOME%\bin. Click OK on all windows to save. You must restart any open terminal windows or Command Prompt for the change to take effect.

Add Maven to your PATH on Mac and Linux

Open a terminal and edit your shell configuration file. If you're using the default shell (which is usually zsh on newer Macs and bash on Linux), type:

nano ~/.zshrc (Mac) or nano ~/.bashrc (Linux)

If you use a different shell, edit the corresponding file — ~/.bash_profile for bash on older Macs, or ~/.config/fish/config.fish for Fish shell.

Add these two lines at the end of the file, replacing /path/to/maven with your actual Maven folder path:

export MAVEN_HOME=/path/to/maven export PATH=$MAVEN_HOME/bin:$PATH

Save the file (in nano, press Ctrl+O, then Enter, then Ctrl+X). Close the terminal completely and open a new one — the new terminal will load your updated configuration.

Verify Maven is installed and working

Open a terminal (a new one on Windows if you just set the PATH) and type:

mvn --version

You should see output like:

Apache Maven 3.9.6 (...) Maven home: /path/to/maven Java version: 17.0.1 Java home: /path/to/java

If you see this, Maven is installed correctly. The Java version shown here is the one Maven will use for your builds. If you see "command not found" or "is not recognized", your PATH isn't set correctly — go back and double-check the steps for your operating system. Make sure you restarted your terminal after changing the PATH.

If Maven runs but shows a Java version older than what you need, you may have multiple Java versions installed. Maven uses whichever Java is first in your PATH. You can override this by setting a JAVA_HOME environment variable to point to a specific Java installation, using the same method you used to set MAVEN_HOME.

Frequently Asked Questions

Do I need to install Maven if I'm using an IDE like Eclipse or IntelliJ?

Most IDEs include Maven built-in, so you don't need to install it separately to use Maven projects in the IDE. However, installing Maven on your computer is still useful if you want to run builds from the command line, use Maven in scripts, or work on projects outside the IDE.

What if I get a "JAVA_HOME is not defined" error?

Maven couldn't find your Java installation. Set the JAVA_HOME environment variable to point to your JDK folder (not the JRE). On Windows, add a new System variable named JAVA_HOME with the value C:\Program Files\Java\jdk-17 (or your actual JDK path). On Mac and Linux, add export JAVA_HOME=/path/to/jdk to your shell configuration file.

Can I have multiple versions of Maven installed?

Yes. Extract each version to a different folder and switch between them by changing your MAVEN_HOME variable or by using the full path to a specific Maven binary. Most people only need one version, but this is useful if you're working on older projects that require an older Maven version.

Why does Maven download so many files when I first run it?

Maven is downloading your project's dependencies and its own plugins to a local cache folder (usually ~/.m2/repository). This happens once per dependency — after that, Maven uses the cached copy. The first build of a new project takes longer because of this, but subsequent builds are faster.