Maven Build Tool: Complete Beginner and Interview Guide

What Is a Software Build?

A software build converts source code into a usable application.

For a Java project, the build process normally:

  1. Validates the project
  2. Compiles the source code
  3. Runs unit tests
  4. Packages the code into a JAR or WAR file
  5. Stores or publishes the final artifact

A build tool automates these steps so that developers and CI/CD systems produce consistent results.

What Is Maven?

Apache Maven is a build automation and project management tool used mainly for Java projects.

Maven reads a file named pom.xml, downloads required dependencies, runs build plugins, executes tests, and creates an artifact such as a JAR or WAR. (Apache Maven)

Simple Maven workflow

Source code + pom.xml
        ↓
Download dependencies
        ↓
Compile and test
        ↓
Package application
        ↓
Create JAR or WAR

Why Is Maven Used?

Maven provides:

  • Automated compilation, testing, and packaging
  • Dependency management
  • Standard project structure
  • Repeatable builds
  • Plugin-based automation
  • Easy CI/CD integration
  • Support for multi-module projects

Maven follows convention over configuration, meaning standard projects require less manual build configuration.

Maven Project Structure

my-application/
├── pom.xml
├── src/
│   ├── main/
│   │   ├── java/
│   │   └── resources/
│   └── test/
│       ├── java/
│       └── resources/
└── target/
  • src/main/java: Application source code
  • src/main/resources: Configuration and resource files
  • src/test/java: Unit tests
  • pom.xml: Maven project configuration
  • target: Compiled files, reports, JARs, and WARs

Maven uses these default directories unless the project overrides them. (Apache Maven)

What Is pom.xml?

POM means Project Object Model.

The pom.xml file can define:

  • Project name and version
  • Packaging type
  • Dependencies
  • Plugins
  • Build configuration
  • Parent and child modules
  • Repository information

Minimal POM example

<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.techinshortly</groupId>
    <artifactId>demo-app</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

</project>

The combination of groupId, artifactId, and version uniquely identifies a Maven artifact. (Apache Maven)

Maven Build Lifecycle

Maven has three built-in lifecycles:

  • Default: Builds and publishes the project
  • Clean: Removes previous build output
  • Site: Generates project documentation

The most important default lifecycle phases are:

PhasePurpose
validateChecks the project configuration
compileCompiles source code
testRuns unit tests
packageCreates a JAR or WAR
verifyPerforms additional quality checks
installCopies the artifact to the local repository
deployPublishes the artifact to a remote repository

When you run a later phase, Maven automatically runs the earlier phases in order. For example, mvn install also runs validation, compilation, testing, packaging, and verification. (Apache Maven)

Essential Maven Commands

mvn --version
mvn clean
mvn compile
mvn test
mvn package
mvn verify
mvn install
mvn deploy
mvn clean install
mvn dependency:tree

Most important command

mvn clean install
  • clean deletes the previous target directory.
  • install builds, tests, packages, verifies, and copies the artifact into the local Maven repository.

Package vs. Install vs. Deploy

mvn package

Creates the JAR or WAR inside the target directory.

mvn install

Copies the packaged artifact into the local Maven repository so other local projects can use it.

mvn deploy

Publishes the artifact to a configured remote repository such as Nexus or Artifactory.

Important: mvn deploy normally publishes an artifact to a Maven repository. It does not automatically deploy the application to Tomcat, Kubernetes, or a production server. Application deployment requires a pipeline step or a specialized plugin. (Apache Maven)

Maven Repositories

Maven has two repository categories:

  • Local repository: Usually located at ~/.m2/repository
  • Remote repository: Maven Central or a company repository such as Nexus or Artifactory

Maven checks the local repository first. If the dependency is unavailable, Maven downloads it from a configured remote repository. (Apache Maven)

Repository credentials, mirrors, and proxy settings should be configured in settings.xml or supplied securely through the CI/CD system—not committed inside pom.xml. (Apache Maven)

Maven Plugins and Goals

Maven is plugin-based.

A phase represents a stage in the lifecycle, such as compile or test.

A goal is a specific task performed by a plugin.

Examples:

compile phase → compiler:compile goal
test phase    → surefire:test goal

Plugins perform the actual work, while lifecycle phases determine when that work runs. (Apache Maven)

Snapshot vs. Release

1.0.0-SNAPSHOT = Development version
1.0.0          = Release version

Snapshot artifacts can change as development continues. Release artifacts should remain unchanged. (Apache Maven)

Multi-Module Maven Project

A multi-module project divides a large application into smaller modules.

parent-project/
├── pom.xml
├── service-module/
│   └── pom.xml
└── web-module/
    └── pom.xml

The parent POM normally uses:

<packaging>pom</packaging>

<modules>
    <module>service-module</module>
    <module>web-module</module>
</modules>

One Maven command from the parent directory can build all modules.

Maven with Jenkins

Maven and Jenkins perform different jobs:

  • Maven: Compiles, tests, and packages the application
  • Jenkins: Triggers and coordinates the CI/CD pipeline

Example Jenkins stage:

stage('Build and Test') {
    steps {
        sh './mvnw -B clean verify'
    }
}

Jenkins checks out the code and starts Maven. Maven then performs the actual Java build. (Jenkins)

Using the Maven Wrapper—mvnw or mvnw.cmd—helps developers and CI agents use the Maven version defined by the project. (Apache Maven)

Common Maven Errors and Solutions

1. mvn: command not found

Confirm that Java and Maven are installed and available through PATH.

java -version
mvn --version

2. Dependency could not be resolved

Check:

  • groupId, artifactId, and version
  • Internet or proxy connection
  • Repository credentials
  • Maven settings.xml
  • Whether the artifact exists in the remote repository

3. Java version or release error

Make sure the JDK version matches the compiler configuration in pom.xml. Modern projects should configure the required Java release explicitly. (Apache Maven)

4. Tests failed

Review:

target/surefire-reports/

Do not permanently use -DskipTests simply to hide failing tests. Maven Surefire runs unit tests during the test phase and writes reports under this directory. (Apache Maven)

Maven Tasks in a DevOps Job

A DevOps or build engineer may:

  • Run manual and automated Maven builds
  • Integrate Maven with Jenkins
  • Maintain POM and plugin configurations
  • Troubleshoot dependency and compilation failures
  • Review build logs
  • Communicate failures to developers and testers
  • Publish artifacts to Nexus or Artifactory
  • Resolve build-related Jira tickets

Maven Interview Answer

What is Maven?

Maven is a build automation and project management tool used mainly for Java applications. It reads the pom.xml file, resolves dependencies, runs plugin goals through lifecycle phases, executes tests, and packages the application into a JAR or WAR. In CI/CD, Jenkins triggers the pipeline, while Maven performs the application build.


Leave a Comment

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

Scroll to Top