Skip to content

Checking your reading list…

Retry verification

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Understanding Maven: Dependency Management, Repositories, and Build Lifecycle

Understanding Maven: Dependency Management, Repositories, and Build Lifecycle

This article explains Maven's role as a Java build and dependency management tool, covering its repository concepts, mirrors, lifecycle phases, coordinates, conflict resolution strategies, aggregation and inheritance mechanisms, plugin configuration, and common command-line usage to improve developer productivity.

Maven is a powerful build automation and dependency management tool for Java projects, replacing manual handling of JAR files placed in a /lib directory.

Repositories in Maven are locations that store artifacts. There are two main types: local repositories (default at ${user.home}/.m2/repository) and remote repositories such as the central repository ( repo.maven.apache.org/maven2) or private corporate repositories.

To change the local repository location, modify settings.xml:

<localRepository>/anotherDirectory/.m2/repository</localRepository>

Mirrors act as proxies for remote repositories, improving download speed. Example mirror configuration:

<mirrors>
  <mirror>
    <id>maven.net.cn</id>
    <mirrorOf>central</mirrorOf>
    <name>one of the central mirrors in china</name>
    <url>http://maven.net.cn/content/groups/public/</url>
  </mirror>
</mirrors>

The build lifecycle consists of ordered phases grouped into three built‑in lifecycles: default, clean, and site. Executing mvn install runs all phases up to install.

Coordinates (GAV) uniquely identify an artifact using groupId, artifactId, and version.

Dependency management handles transitive dependencies, conflict resolution (short‑circuit vs. declaration priority), and exclusions. Example of excluding a transitive dependency:

<exclusions>
  <exclusion>
    <groupId>excluded.groupId</groupId>
    <artifactId>excluded-artifactId</artifactId>
  </exclusion>
</exclusions>

When conflicts arise, Maven chooses the nearest dependency (short‑circuit) unless declaration order forces a different version. The article demonstrates resolving a NoSuchMethodError by adjusting dependency order or using exclusions.

Aggregation allows building multiple modules together using a parent pom with <modules>. Example:

<packaging>pom</packaging>
<modules>
  <module>module-1</module>
  <module>module-2</module>
  <module>module-3</module>
</modules>

Inheritance lets child projects inherit dependencies from a parent POM via <parent> and <dependencyManagement> sections.

Plugins define goals that execute during lifecycle phases. Example plugin configuration:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-source-plugin</artifactId>
      <version>2.2.1</version>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>jar-no-fork</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

Common Maven commands include mvn clean install, mvn dependency:tree (with filters), and profile activation using -P. Example to build with a specific profile: mvn package -P dev Learning Maven improves productivity, simplifies JAR management, enables sharing of custom artifacts, and empowers developers to resolve dependency conflicts without external assistance.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

Report an issue with this article
build automationdependency managementMavenpluginsRepositoryJava Build ToolMaven Lifecycle

Reader feedback

Was this useful?

Useful0

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion