Backend Development 17 min read

Master Maven Dependencies, Lifecycle, and Plugins for Efficient Java Projects

This article explains how to manage Maven dependencies, understand transitive, optional, and excluded dependencies, configure dependency scopes, navigate the Maven build lifecycle, use plugins, and set up module aggregation, inheritance, properties, versioning, environment profiles, and a private Nexus repository for Java projects.

Ops Development Stories
Ops Development Stories
Ops Development Stories
Master Maven Dependencies, Lifecycle, and Plugins for Efficient Java Projects

Dependencies

Maven dependencies are declared in the

dependencies

tag, allowing automatic download of required JARs from remote repositories without manual handling.

<code>&lt;dependencies&gt;
  &lt;!-- servlet package --&gt;
  &lt;dependency&gt;
    &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
    &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;
  &lt;/dependency&gt;
&lt;/dependencies&gt;</code>

After adding a dependency, refresh Maven to fetch the corresponding JAR files.

Transitive Dependencies

Dependencies are transitive: adding one brings in all of its own dependencies. For example, adding

druid-spring-boot-starter

automatically pulls in several indirect dependencies, as shown in the diagram. When direct and transitive versions conflict, Maven resolves them by giving higher priority to the outermost declaration; if at the same level, the earlier declaration wins.

Transitive dependency illustration
Transitive dependency illustration
Conflict resolution rules
Conflict resolution rules

Optional Dependencies

Optional dependencies hide transitive resources from downstream consumers. Setting

optional

to

true

disables the indirect dependencies.

<code>&lt;dependency&gt;
  &lt;groupId&gt;junit&lt;/groupId&gt;
  &lt;artifactId&gt;junit&lt;/artifactId&gt;
  &lt;version&gt;4.12&lt;/version&gt;
  &lt;optional&gt;true&lt;/optional&gt;
&lt;/dependency&gt;</code>

Excluding Dependencies

Exclusions actively cut off transitive dependencies. The following example excludes

hamcrest-core

from the JUnit dependency.

<code>&lt;dependency&gt;
  &lt;groupId&gt;junit&lt;/groupId&gt;
  &lt;artifactId&gt;junit&lt;/artifactId&gt;
  &lt;version&gt;4.12&lt;/version&gt;
  &lt;exclusions&gt;
    &lt;exclusion&gt;
      &lt;groupId&gt;org.hamcrest&lt;/groupId&gt;
      &lt;artifactId&gt;hamcrest-core&lt;/artifactId&gt;
    &lt;/exclusion&gt;
  &lt;/exclusions&gt;
&lt;/dependency&gt;</code>

Optional dependencies are defined by the dependency provider, while exclusions are configured by the consumer.

Dependency Scope

The

scope

tag limits where a JAR is used (e.g.,

compile

,

test

,

provided

,

runtime

). The diagram illustrates the effect on main code, test code, and packaging.

Scope illustration
Scope illustration

Lifecycle and Plugins

Project Build Lifecycle

Maven defines three lifecycle phases: clean (cleanup), default (core tasks such as compile, test, package, deploy), and site (report generation). The default phase is illustrated with a detailed diagram.

Default lifecycle diagram
Default lifecycle diagram
IDE lifecycle view
IDE lifecycle view

Plugins

Plugins are defined under

build

plugins

and bind custom goals to specific lifecycle phases. The example adds the Maven Compiler Plugin with Java 1.8 source and target settings.

<code>&lt;build&gt;
    &lt;plugins&gt;
        &lt;plugin&gt;
            &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
            &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
            &lt;version&gt;3.1&lt;/version&gt;
            &lt;configuration&gt;
                &lt;source&gt;1.8&lt;/source&gt;
                &lt;target&gt;1.8&lt;/target&gt;
                &lt;encoding&gt;UTF-8&lt;/encoding&gt;
            &lt;/configuration&gt;
        &lt;/plugin&gt;
    &lt;/plugins&gt;
&lt;/build&gt;</code>

Plugins execute when their bound phase runs. Typical plugin benefits include extending Maven with custom actions.

Plugin bound to lifecycle phase – executes at the designated phase.

Default Maven bindings provide common functionality.

Custom plugins enable additional tasks.

<code>&lt;build&gt;
  &lt;plugins&gt;
    &lt;plugin&gt;
      &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
      &lt;artifactId&gt;maven-source-plugin&lt;/artifactId&gt;
      &lt;version&gt;2.2.1&lt;/version&gt;
      &lt;executions&gt;
        &lt;execution&gt;
          &lt;goals&gt;
            &lt;goal&gt;jar&lt;/goal&gt;
          &lt;/goals&gt;
          &lt;phase&gt;generate-test-resources&lt;/phase&gt;
        &lt;/execution&gt;
      &lt;/executions&gt;
    &lt;/plugin&gt;
  &lt;/plugins&gt;
&lt;/build&gt;</code>

Module Aggregation

Aggregating multiple modules enables a single build that processes all listed projects. The Ruoyi example lists modules such as

ruoyi-admin

,

ruoyi-framework

, etc., and sets

packaging

to

pom

for the parent.

Ruoyi module aggregation
Ruoyi module aggregation

Module Inheritance

Inheritance shares configuration from a parent POM to child modules, avoiding duplicate version declarations. Example parent declaration and a

dependencyManagement

section are shown.

<code>&lt;parent&gt;
  &lt;artifactId&gt;ruoyi&lt;/artifactId&gt;
  &lt;groupId&gt;com.ruoyi&lt;/groupId&gt;
  &lt;version&gt;3.8.1&lt;/version&gt;
&lt;/parent&gt;</code>
<code>&lt;dependencyManagement&gt;
  &lt;dependencies&gt;
    &lt;!-- Spring Boot dependencies --&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-dependencies&lt;/artifactId&gt;
      &lt;version&gt;2.5.8&lt;/version&gt;
      &lt;type&gt;pom&lt;/type&gt;
      &lt;scope&gt;import&lt;/scope&gt;
    &lt;/dependency&gt;
    &lt;!-- Alibaba Druid pool --&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;com.alibaba&lt;/groupId&gt;
      &lt;artifactId&gt;druid-spring-boot-starter&lt;/artifactId&gt;
      &lt;version&gt;${druid.version}&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;!-- MyBatis Spring Boot starter --&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.mybatis.spring.boot&lt;/groupId&gt;
      &lt;artifactId&gt;mybatis-spring-boot-starter&lt;/artifactId&gt;
      &lt;version&gt;${mybatis-spring-boot.version}&lt;/version&gt;
    &lt;/dependency&gt;
  &lt;/dependencies&gt;
&lt;/dependencyManagement&gt;</code>

Properties

Maven supports several property types: custom, built‑in, settings, Java system, and environment variables. Examples demonstrate defining a custom property and referencing it, as well as using built‑in properties like

${basedir}

and

${version}

.

<code>&lt;properties&gt;
  &lt;ruoyi.version&gt;3.8.1&lt;/ruoyi.version&gt;
&lt;/properties&gt;

&lt;groupId&gt;com.ruoyi&lt;/groupId&gt;
&lt;artifactId&gt;ruoyi&lt;/artifactId&gt;
&lt;version&gt;${ruoyi.version}&lt;/version&gt;</code>
<code>${basedir}
${version}</code>

Version Management

Project version numbers are defined in the

version

element of the POM and follow conventional patterns, as illustrated by screenshots of version listings.

Version list
Version list
Version scheme
Version scheme

Environment Configuration

Different environments (development, test, production) are handled via Maven profiles. The IDE shows profile selection, and the

application.yml

can be adjusted based on the active profile.

Maven profiles in IDE
Maven profiles in IDE

Private Repository (Nexus)

Nexus provides a hosted Maven repository for internal artifacts. The article walks through creating a Maven2 hosted repository, adding it to a repository group, and configuring

settings.xml

with server credentials, profiles, and distribution management for deployment.

Nexus download page
Nexus download page
<code>&lt;servers&gt;
  &lt;server&gt;
    &lt;id&gt;ticknet-release&lt;/id&gt;
    &lt;username&gt;admin&lt;/username&gt;
    &lt;password&gt;admin&lt;/password&gt;
  &lt;/server&gt;
  &lt;server&gt;
    &lt;id&gt;ticknet-snapshots&lt;/id&gt;
    &lt;username&gt;admin&lt;/username&gt;
    &lt;password&gt;admin&lt;/password&gt;
  &lt;/server&gt;
&lt;/servers&gt;</code>
<code>&lt;profiles&gt;
  &lt;profile&gt;
    &lt;id&gt;artifactory&lt;/id&gt;
    &lt;repositories&gt;
      &lt;repository&gt;
        &lt;snapshots&gt;&lt;enabled&gt;false&lt;/enabled&gt;&lt;/snapshots&gt;
        &lt;id&gt;repo&lt;/id&gt;
        &lt;name&gt;repo&lt;/name&gt;
        &lt;url&gt;xxxx&lt;/url&gt;
      &lt;/repository&gt;
      &lt;repository&gt;
        &lt;snapshots/&gt;
        &lt;id&gt;snapshots&lt;/id&gt;
        &lt;name&gt;snapshots-only&lt;/name&gt;
        &lt;url&gt;xxxx&lt;/url&gt;
      &lt;/repository&gt;
    &lt;/repositories&gt;
  &lt;/profile&gt;
&lt;/profiles&gt;</code>
<code>&lt;distributionManagement&gt;
  &lt;repository&gt;
    &lt;id&gt;ticknet-release&lt;/id&gt;
    &lt;url&gt;http://localhost:8081/repository/ticknet-release/&lt;/url&gt;
  &lt;/repository&gt;
  &lt;snapshotRepository&gt;
    &lt;id&gt;ticknet-snapshots&lt;/id&gt;
    &lt;url&gt;http://localhost:8081/repository/ticknet-release/&lt;/url&gt;
  &lt;/snapshotRepository&gt;
&lt;/distributionManagement&gt;</code>

Running the

deploy

phase uploads artifacts to the configured Nexus repository.

JavaBuild ToolMavenlifecyclePluginsdependencies
Ops Development Stories
Written by

Ops Development Stories

Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.