Maven is a popular build automation tool used primarily for Java projects. It is designed to manage a project's build process, including the managing dependencies, packaging, deployment and generating reports.
Maven uses an XML file called a Project Object Model
pom.xml to define the project's configuration, including its dependencies, plugins, and other settings.
The POM file is typically stored in the project's root directory.
Maven is a popular build tool for Java projects that provides a number of benefits, including:
Dependency management: Maven makes it easy to manage dependencies for your project, including downloading and including libraries from remote repositories.
Build automation: Maven automates the build process, including compiling the code, running tests, and packaging the application into a distributable format.
Consistent project structure: Maven provides a standard project structure that makes it easy to organize your code and resources.
Plugin system: Maven has a powerful plugin system that can be used to extend the functionality of the build process, such as generating documentation or deploying the application to a server.
Easy project setup: Maven provides archetypes that can be used to quickly set up a new project with a predefined structure and dependencies.
Reproducible builds: Maven provides a way to create reproducible builds, which means that the build process is consistent across different machines and environments.
Integration with IDEs: Maven integrates with popular Java IDEs, such as Eclipse and IntelliJ, making it easy to import and work with Maven projects.
Overall, Maven can help improve the productivity of developers and make it easier to manage complex Java projects by automating common tasks and providing a standardized way to structure and build projects.
Maven projects are identified by 3 main identifiers/coordinates:
Group Id: Unique identifier for the organization. Typically, this is a reversed domain name.
Artifact Id: Used to generate the build (JAR /WAR) files.
Version: A version number e.g 1.0.0
Maven uses a centralised repository to manage project dependencies. The Maven Repository is divided into three types: Local, Central and Remote.
1. Maven Local Repository
The local repository is a directory on the developer's machine where all the dependencies for a specific project are stored.
The local repository in Maven is usually located in the user's home directory under the .m2 folder.
2. Central Maven Repository
The Central Repository is the default remote repository used by Maven.
It contains a vast collection of open-source libraries and dependencies, making it a one-stop shop for most projects. When Maven needs to download a dependency, it first looks in the local repository and then in the Central Repository.
The central maven repository is managed by Sonatype. There are almost all java libraries that exist in it.
When building a project, if it can not find any dependent jars in the local maven repository it will search in the central maven repository and download those JARs if found. You can also search for an artifact in central.sonatype.com if you want to download the artifact’s pom or jars.
To search/browse in the central repository you can use: central.sonatype.com
3. Remote Repository is a repository of dependencies located on a remote server, typically managed by third party organizations.
Here's an example of how to use a remote Maven repository in a Java project:
<repositories> <repository> <id>reporitory-id</id> <name>Repository Name</name> <url>your remote repository url</url> </repository> </repositories>
For every dependency defined in
pom.xml file, it first search dependency in the local repository, followed by the remote repositories in the same order as defined in the
If the dependency is not found in any of the remote repositories, Maven reports a build error.
- Create a new project directory: Create a new directory for your project. For example, you can create a directory called "my-maven-project".
- Navigate to the project directory: Navigate to the newly created directory.
- Create a new Maven project:
Create a new Maven project using the
mvn archetype:generatecommand. This command generates a new Maven project based on a template or archetype.
mvn archetype:generate -DgroupId=com.example.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
By the way, you don’t have to remember each of these parameters provided above. Just use the
interactiveMode=true, so that Maven asks for all the required parameters.
In this command, the
artifactId are the unique identifiers for your project. The
archetypeArtifactId specifies the archetype to use, which in this case is the
maven-archetype-quickstart archetype, which creates a simple Java project with a main class.
Learn more about Maven build process, repository and build profiling here.
Whether you're a beginner or an experienced developer, this articles will help you to up to speed with Maven.