Maven Tutorial
Maven in IDE
Maven coordinates uniquely identify a specific version of an artifact (e.g., JAR, WAR) in a Maven repository. They consist of four key components: groupId
, artifactId
, version
, and an optional packaging
type. These coordinates are used to declare dependencies and locate artifacts within a repository. In this tutorial, you'll learn more about each component of Maven coordinates and see examples of how they are used.
1. groupId
groupId
is a unique identifier for a project, usually represented by a reverse domain name (similar to a Java package name). The groupId
helps to prevent naming conflicts among different projects.
Example:
<groupId>com.example.myproject</groupId>
2. artifactId
artifactId
is the name of the artifact (e.g., a library or a web application) produced by the project. This name should be descriptive and unique within a groupId
.
Example:
<artifactId>my-library</artifactId>
3. version
version
is the current version of the artifact. Maven uses the version number to track different releases of the same artifact. The version number typically follows a format like major.minor.patch
, with optional qualifiers (e.g., -SNAPSHOT
, -RELEASE
, -RC1
).
Example:
<version>1.0.0</version>
4. packaging (optional)
packaging
specifies the type of artifact produced by the project (e.g., JAR, WAR, POM). If not specified, Maven assumes the packaging type is JAR.
Example:
<packaging>war</packaging>
Maven Coordinates in pom.xml
In a Maven project, the coordinates are defined in the pom.xml
file within the <project>
element:
<project> <modelVersion>4.0.0</modelVersion> <groupId>com.example.myproject</groupId> <artifactId>my-library</artifactId> <version>1.0.0</version> <packaging>war</packaging> ... </project>
Using Maven Coordinates to Declare Dependencies
To declare a dependency on an artifact in another Maven project, you need to specify its Maven coordinates in the dependencies
section of your pom.xml
:
<dependencies> <dependency> <groupId>com.example.otherproject</groupId> <artifactId>other-library</artifactId> <version>2.1.0</version> </dependency> </dependencies>
In conclusion, Maven coordinates are essential for uniquely identifying artifacts and managing dependencies in Maven projects. By understanding how to use Maven coordinates, you can effectively manage and share artifacts within the Maven ecosystem.
How to set Maven coordinates in pom.xml
:
groupId
, artifactId
, and version
. These coordinates uniquely identify a project's artifact.pom.xml
file.<project>
element:<groupId>com.example</groupId> <artifactId>my-project</artifactId> <version>1.0.0</version>
Resolving dependencies using Maven coordinates:
pom.xml
and resolved based on their coordinates.<dependencies>
section:<dependencies> <dependency> <groupId>com.example</groupId> <artifactId>dependency-artifact</artifactId> <version>1.2.3</version> </dependency> </dependencies>
Working with Maven snapshot versions and coordinates:
<version>1.0.0-SNAPSHOT</version>
Resolving conflicts in Maven coordinates:
dependencyManagement
section to enforce versions:<dependencyManagement> <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>conflict-artifact</artifactId> <version>2.0.0</version> </dependency> </dependencies> </dependencyManagement>