last modified June 9, 2025
In this article we show how to use Maven for build automation and
dependency management in Java projects.
Maven is a powerful build automation and project management tool
primarily used for Java projects. It simplifies the build process by providing
a standard directory layout, dependency management, and a comprehensive set of
plugins for various tasks.
Maven uses a declarative approach where you describe what your project is and
what it needs, rather than how to build it. This is done through an XML file
called pom.xml (Project Object Model) that contains project
information and configuration details.
Key benefits of Maven include automated dependency management, standardized project structure, integration with IDEs, and extensive plugin ecosystem for testing, packaging, and deployment.
Before installing Maven, ensure you have Java Development Kit (JDK) 8 or later installed on your system. Maven requires Java to run.
On Windows:
https://maven.apache.org/download.cgiC:\Program Files\Apache\mavenbin directory to your PATH environment variableM2_HOME environment variable to Maven installation directoryOn macOS:
$ brew install maven
On Ubuntu/Debian:
$ sudo apt update $ sudo apt install maven
After installation, verify Maven is working correctly:
$ mvn --version Apache Maven 3.9.6 Maven home: /usr/share/maven Java version: 21.0.1, vendor: Eclipse Adoptium
Understanding these core concepts is essential for working with Maven effectively.
The pom.xml file is the heart of any Maven project. It contains
project information, dependencies, build configuration, and plugin definitions.
Every Maven project must have a POM file in its root directory.
Maven uses coordinates to uniquely identify projects and dependencies. The coordinates consist of:
Maven follows a standard directory structure that promotes consistency across projects:
my-project/ ├── pom.xml ├── src/ │ ├── main/ │ │ ├── java/ # Application source code │ │ └── resources/ # Application resources │ └── test/ │ ├── java/ # Test source code │ └── resources/ # Test resources └── target/ # Compiled output (generated)
Maven has three built-in lifecycles: default, clean,
and site. The default lifecycle includes phases like compile,
test, package, and install.
Let's create a simple Maven project using the archetype plugin, which generates project templates.
Use the following command to create a new Maven project:
$ mvn archetype:generate -DgroupId=com.example.myapp \ -DartifactId=my-first-maven-project \ -DarchetypeArtifactId=maven-archetype-quickstart \ -DinteractiveMode=false
This command creates a new directory with the project structure and a basic
pom.xml file.
After running the command, you'll see the following structure:
my-first-maven-project/ ├── pom.xml └── src/ ├── main/ │ └── java/ │ └── com/ │ └── example/ │ └── myapp/ │ └── App.java └── test/ └── java/ └── com/ └── example/ └── myapp/ └── AppTest.java
Navigate to the project directory and build it:
$ cd my-first-maven-project $ mvn compile
To run tests and package the application:
$ mvn package
This creates a JAR file in the target directory.
Execute the generated JAR file:
$ java -cp target/my-first-maven-project-1.0-SNAPSHOT.jar com.example.myapp.App Hello World!
The application prints "Hello World!" to the console, indicating that your
Maven project is set up and running successfully. The -cp (option
stands for "classpath") option specifies the location of the JAR file to run
the application. The com.example.myapp.App is the main class
that contains the main method.
The Project Object Model (POM) is Maven's fundamental unit. Let's examine the
structure of a basic pom.xml file.
<?xml version="1.0" encoding="UTF-8"?> <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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example.myapp</groupId> <artifactId>my-first-maven-project</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>my-first-maven-project</name> <url>http://www.example.com</url> <properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.11.0</version> <configuration> <source>11</source> <target>11</target> </configuration> </plugin> </plugins> </build> </project>
This pom.xml file defines a simple Maven project with the following
key elements:
Let's break down the key elements of the pom.xml file:
<modelVersion>4.0.0</modelVersion>
Specifies the POM model version. Currently, version 4.0.0 is the only supported version for Maven 2 and 3.
<groupId>com.example.myapp</groupId> <artifactId>my-first-maven-project</artifactId> <version>1.0-SNAPSHOT</version>
These three elements form the project coordinates that uniquely identify your project in the Maven ecosystem.
<packaging>jar</packaging>
Defines the packaging type. Common values include jar,
war, ear, and pom.
<properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties>
Properties section allows you to define values that can be reused throughout the POM. Here we specify Java version 11 for compilation.
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> </dependencies>
Dependencies section lists external libraries your project needs. The
scope element defines when the dependency is available. Test scope
means it's only available during testing.
Here are essential Maven commands for daily development:
$ mvn clean # Clean previous builds $ mvn compile # Compile source code $ mvn test # Run tests $ mvn package # Create JAR/WAR file $ mvn install # Install to local repository $ mvn dependency:tree # Show dependency tree
These commands help you manage your project lifecycle efficiently. The
clean command removes the target directory, which
contains compiled code and artifacts from previous builds. The
compile command compiles the source code, the test
command runs unit tests, and thepackage command creates a JAR or
WAR file in the target directory.
The install command installs the
built artifact into your local Maven repository, making it available for other
projects. The dependency:tree command displays the project's
dependency tree, helping you understand the relationships between dependencies.
In this article we have shown how to use Maven for build automation and dependency management in Java projects.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all Java tutorials.