Maven Tutorial For Java Developers
Maven Tutorial For Java Developers
Introduction to Maven
Maven is a widely-used build automation tool primarily for Java projects. It simplifies and
standardizes the build process by providing a uniform build system, dependency management,
and a central repository for libraries and plugins. Maven’s declarative approach, centered around
the Project Object Model (POM) file, reduces manual configuration and ensures consistency
across projects.
Core Concepts
1. POM File (pom.xml):
o The Project Object Model (POM) is an XML file that defines the project’s
configuration, dependencies, and build process.
o Key elements: groupId, artifactId, version, dependencies, plugins.
2. Dependency Management:
o Maven resolves dependencies from repositories (e.g., Maven Central) and
manages transitive dependencies (dependencies of dependencies).
o Scopes (e.g., compile, test, provided) control when dependencies are available.
3. Build Lifecycle:
o Maven defines a standard lifecycle: validate, compile, test, package,
install, deploy.
o Each phase executes specific goals (tasks) defined by plugins.
4. Plugins and Goals:
o Plugins extend Maven’s functionality (e.g., maven-compiler-plugin for
compilation).
o Goals are specific tasks within a plugin (e.g., compile:compile).
5. Repositories:
o Local Repository: Stores downloaded artifacts in ~/.m2/repository.
o Remote Repositories: Maven Central, Nexus, or custom repositories for
downloading dependencies.
Setting Up Maven
1. Install Maven:
o Download Maven from apache.org.
o Set M2_HOME environment variable and add mvn to your PATH.
o Verify installation: mvn -version.
2. Create a Maven Project:
Use the Maven archetype to generate a project:
3. mvn archetype:generate -DgroupId=com.example -DartifactId=maven-demo -
DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
maven-demo/
├── pom.xml
└── src
├── main
│ └── java
│ └── com/example/App.java
└── test
└── java
└── com/example/AppTest.java
<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</groupId>
<artifactId>maven-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Maven Demo Project</name>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- JUnit for testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Compiler plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Explanation
Main Class
Create src/main/java/com/example/App.java:
package com.example;
Test Class
Create src/test/java/com/example/AppTest.java:
package com.example;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
Output:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
package com.example;
import org.apache.commons.lang3.StringUtils;
1. Update pom.xml:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
package com.example;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
3. Run mvn package and execute the application, assuming a demo_db database with a
users table exists.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<configLocation>google_checks.xml</configLocation>
</configuration>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>parent-project</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>core</module>
<module>web</module>
</modules>
</project>
2. Create submodules (core and web) with their own pom.xml files, inheriting from the
parent.
3. Build all modules: mvn clean install.
Best Practices
Use Meaningful Coordinates: Choose clear groupId, artifactId, and version for
easy identification.
Centralize Dependencies: Use <dependencyManagement> in parent POM for multi-
module projects to manage versions.
Leverage Plugins: Use plugins like maven-surefire-plugin for tests or maven-shade-
plugin for executable JARs.
Keep POM Clean: Avoid unnecessary dependencies and configurations.
Use Repositories: Configure remote repositories (e.g., Nexus) for internal artifacts.
Automate with CI/CD: Integrate Maven with Jenkins or GitLab for automated builds
and deployments.
DevOps Integration
Maven is integral to DevOps pipelines for Java developers:
CI/CD: Use Maven in Jenkins or GitLab CI/CD to automate builds and tests.
Artifact Repositories: Deploy artifacts to Nexus or Artifactory for sharing across teams.
Docker: Package Maven artifacts into Docker images using docker-maven-plugin.
pipeline {
agent any
tools {
maven 'Maven'
}
stages {
stage('Build') {
steps {
sh 'mvn clean package'
}
}
}
}
Conclusion
Maven simplifies Java development by automating builds, managing dependencies, and
providing a standardized project structure. This tutorial covered setting up a Maven project,
configuring the POM file, writing and testing Java code, and applying Maven in real-world
scenarios like database integration and multi-module projects. By mastering Maven, Java
developers can streamline their workflows and integrate effectively with DevOps practices.