Apache Maven Tutorial

Absolutely. Here’s a beginner-friendly Apache Maven tutorial covering installation, setup, and creating a simple Java project from scratch.

1. What is Maven?

Apache Maven is a build and dependency-management tool for Java projects.

Maven can help you:

  • Compile Java code
  • Download and manage dependencies
  • Run tests
  • Package applications into JAR/WAR files
  • Maintain a standard project structure
  • Automate common build tasks

The main Maven configuration file is:

pom.xml

POM means Project Object Model.


2. Prerequisites

You need:

  1. Java JDK
  2. Apache Maven
  3. A code editor/IDE such as IntelliJ IDEA, Eclipse, or VS Code
  4. A terminal/command prompt

First check whether Java is installed:

java -version

Also check the Java compiler:

javac -version

You should get output similar to:

java version "21..."
javac 21...

If java or javac isn’t recognized, install a JDK first.


3. Install Maven

Download Apache Maven from the official Maven website:

Apache Maven official site

Download the Binary zip archive, for example:

apache-maven-3.x.x-bin.zip

Extract it somewhere such as:

C:\Program Files\Apache\maven

or:

C:\apache-maven

4. Configure Maven on Windows

Assuming Maven was extracted to:

C:\apache-maven\apache-maven-3.x.x

Add the following environment variable:

MAVEN_HOME=C:\apache-maven\apache-maven-3.x.x

Then add Maven’s bin directory to your PATH:

%MAVEN_HOME%\bin

Check Maven

Open a new Command Prompt or PowerShell window:

mvn -version

You should see something similar to:

Apache Maven 3.x.x
Maven home: C:\apache-maven\apache-maven-3.x.x
Java version: 21
Java home: ...
OS name: Windows

If you see this, Maven is installed successfully.


5. Install Maven on Linux

On Ubuntu/Debian, you can install Maven with:

sudo apt update
sudo apt install maven

Then:

mvn -version

Alternatively, you can download Maven directly from the official Apache Maven site.


6. Install Maven on macOS

If you use Homebrew:

brew install maven

Then verify:

mvn -version

7. Create Your First Maven Project

Let’s create a very simple Java application.

Open your terminal and run:

mvn archetype:generate \
  -DgroupId=com.example \
  -DartifactId=hello-maven \
  -DarchetypeArtifactId=maven-archetype-quickstart \
  -DinteractiveMode=false

On Windows PowerShell, you can also put it on one line:

mvn archetype:generate -DgroupId=com.example -DartifactId=hello-maven -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Maven will create:

hello-maven

Go inside the project:

cd hello-maven

8. Understand the Maven Project Structure

You should have something similar to:

hello-maven
│
├── pom.xml
│
└── src
    ├── main
    │   └── java
    │       └── com
    │           └── example
    │               └── App.java
    │
    └── test
        └── java
            └── com
                └── example
                    └── AppTest.java

This structure is important because Maven follows conventions.

src/main/java

Contains your application source code.

src/test/java

Contains your test code.

pom.xml

Contains project configuration and dependencies.


9. Open pom.xml

You’ll see something similar to:

<project>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>hello-maven</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        ...
    </dependencies>
</project>

The three most important coordinates are:

<groupId>com.example</groupId>
<artifactId>hello-maven</artifactId>
<version>1.0-SNAPSHOT</version>

Think of them as the project’s identity.

groupId

Usually represents your organization or package:

com.example

artifactId

The name of your project:

hello-maven

version

The project version:

1.0-SNAPSHOT

10. Create a Simple Java Program

Open:

src/main/java/com/example/App.java

Replace its contents with:

package com.example;

public class App {

    public static void main(String[] args) {
        System.out.println("Hello, Maven!");
    }
}

11. Compile the Project

From the project directory:

mvn compile

Maven will:

  1. Read pom.xml
  2. Resolve required dependencies
  3. Compile your Java code
  4. Put compiled classes under:
target/classes

You should see:

BUILD SUCCESS

12. Run the Program

Maven itself primarily handles the build lifecycle. For a simple application, one convenient option is to use the Maven Exec plugin.

Add this to your pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>3.5.0</version>
            <configuration>
                <mainClass>com.example.App</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

Then run:

mvn exec:java

Output:

Hello, Maven!

13. Run Tests

The generated project contains a test.

Run:

mvn test

Maven will compile the test code and execute it.

You should eventually see:

Tests run: 1, Failures: 0, Errors: 0

and:

BUILD SUCCESS

14. Package the Application

Run:

mvn package

Maven performs the necessary lifecycle phases and creates a JAR.

You’ll find it inside:

target

For example:

target/hello-maven-1.0-SNAPSHOT.jar

The basic Maven lifecycle is:

validate
   ↓
compile
   ↓
test
   ↓
package
   ↓
verify
   ↓
install
   ↓
deploy

You don’t normally need to run every phase individually. For example:

mvn package

will run the earlier required phases too.


15. Clean the Project

Maven creates a target directory containing generated files.

To remove it:

mvn clean

Then:

target/

will be removed.

A very common command is:

mvn clean package

This means:

  1. Delete previous build output
  2. Compile
  3. Test
  4. Package

16. Maven Dependencies

One of Maven’s biggest advantages is dependency management.

For example, suppose you want to use Google’s Gson library.

You can add a dependency inside <dependencies>:

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.13.2</version>
</dependency>

Then Maven downloads the library automatically.

You don’t have to manually download a JAR and add it to your classpath.

Your pom.xml might look like:

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>hello-maven</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.release>21</maven.compiler.release>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.13.2</version>
        </dependency>
    </dependencies>

</project>

Then run:

mvn compile

Maven resolves the dependency and makes it available to your project.


17. Important Maven Commands

CommandPurpose
mvn -versionCheck Maven installation
mvn cleanDelete build output
mvn compileCompile source code
mvn testRun tests
mvn packageCreate JAR/WAR
mvn clean packageClean and package
mvn installInstall artifact into local Maven repository
mvn verifyRun verification checks
mvn dependency:treeDisplay project dependencies

18. Maven Local Repository

When Maven downloads dependencies, it stores them in your local Maven repository.

On Windows, this is normally:

C:\Users\<username>\.m2\repository

On Linux/macOS:

~/.m2/repository

For example, after downloading Gson, you may find it under something like:

~/.m2/repository/com/google/code/gson/

This prevents Maven from downloading the same dependency repeatedly.


19. Complete Beginner Workflow

A typical Maven development cycle looks like this:

Create project
     ↓
Edit pom.xml
     ↓
Write Java code
     ↓
mvn compile
     ↓
mvn test
     ↓
mvn package
     ↓
JAR created in target/

For everyday development, you’ll frequently use:

mvn clean package

and, when you want to install the artifact into your local Maven repository:

mvn clean install

20. The Most Important Things to Remember

If you’re learning Maven for the first time, focus on these five concepts:

  1. pom.xml — Maven’s project configuration file.
  2. src/main/java — your application source code.
  3. src/test/java — your test code.
  4. target/ — generated build output; don’t normally edit it manually.
  5. Maven lifecycle — commands such as compile, test, package, and install.

A good next step is to learn how to create the same Maven project manually in IntelliJ IDEA, then add a dependency such as JUnit and build a small Java application.

Leave a Reply

Your email address will not be published. Required fields are marked *