DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Retrieval Augmented Generation With Spring AI 2.0, Claude, and PGvector
  • Fix Circular Dependencies in PostgreSQL Row-Level Security With SECURITY DEFINER Functions
  • Sharing SBOMs Securely Without Giving Too Much Away
  • Securing CI/CD Pipelines Against Supply Chain Attacks: Why Artifacts and Dependencies Matter More Than Ever

Trending

  • Why Your Test Automation Is Always Behind the Code And the Architecture That Fixes It
  • Agentic Test Creation: From Plain-Language Requirements to End-to-End Test Cases
  • Giving AI Agents Safe Access to Legacy Systems: Introducing legacy2mcp
  • Git Branching Strategies for Maintainable Test Automation

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Software Design and Architecture
  3. Security
  4. Dependency Management and Maven

Dependency Management and Maven

Achieving dependency management with Maven for multiple projects.

By 
Emmanouil Gkatziouras
DZone Core CORE ·
Nov. 03, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
14.4K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Maven is a great and mature build automation tool. There is always a solution on almost everything. The main case you might stumble on organization projects is dependency management. Instead of each project having its own dependencies you want a centralized way to inherit those dependencies.

In those cases, you declare on the parent prom the managed dependencies. In my example, I just want to include the Akka stream dependencies.

XML
 




x
41


 
1
<?xml version="1.0" encoding="UTF-8"?>
2
<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">
3
    <modelVersion>4.0.0</modelVersion>
4
 
5
    <groupId>org.example</groupId>
6
    <artifactId>maven-dependency-management</artifactId>
7
    <packaging>pom</packaging>
8
    <version>1.0-SNAPSHOT</version>
9
 
10
    <properties>
11
        <akka.version>2.5.31</akka.version>
12
        <akka.http.version>10.1.11</akka.http.version>
13
        <scala.binary.version>2.12</scala.binary.version>
14
    </properties>
15
 
16
    <modules>
17
        <module>child-one</module>
18
    </modules>
19
 
20
 
21
    <dependencyManagement>
22
        <dependencies>
23
            <dependency>
24
                <groupId>com.typesafe.akka</groupId>
25
                <artifactId>akka-stream_2.12</artifactId>
26
                <version>${akka.version}</version>
27
            </dependency>
28
            <dependency>
29
                <groupId>com.typesafe.akka</groupId>
30
                <artifactId>akka-http_2.12</artifactId>
31
                <version>${akka.http.version}</version>
32
            </dependency>
33
            <dependency>
34
                <groupId>com.typesafe.akka</groupId>
35
                <artifactId>akka-http-spray-json_2.12</artifactId>
36
                <version>${akka.http.version}</version>
37
            </dependency>
38
        </dependencies>
39
    </dependencyManagement>
40
 
41
</project>



What I use is the dependency management block.

Now the child project would be able to include those libraries without specifying the version. Having the version derived and managed is essential. Many unpleasant surprises can come if a version is incompatible.

Now on to the child module the versions are declared without the version since it is the child module.

XML
 




xxxxxxxxxx
1
27


 
1
<?xml version="1.0" encoding="UTF-8"?>
2
<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">
3
    <parent>
4
        <artifactId>maven-dependency-management</artifactId>
5
        <groupId>org.example</groupId>
6
        <version>1.0-SNAPSHOT</version>
7
    </parent>
8
    <modelVersion>4.0.0</modelVersion>
9
 
10
    <artifactId>child-one</artifactId>
11
 
12
    <dependencies>
13
        <dependency>
14
            <groupId>com.typesafe.akka</groupId>
15
            <artifactId>akka-stream_2.12</artifactId>
16
        </dependency>
17
        <dependency>
18
            <groupId>com.typesafe.akka</groupId>
19
            <artifactId>akka-http_2.12</artifactId>
20
        </dependency>
21
        <dependency>
22
            <groupId>com.typesafe.akka</groupId>
23
            <artifactId>akka-http-spray-json_2.12</artifactId>
24
        </dependency>
25
    </dependencies>
26
 
27
</project>



On another note sometimes we want to use another project’s dependency management without that project being our parent. Those are cases where you need to include the dependency management from a parent project when you already have a parent project.

XML
 




xxxxxxxxxx
1
36


 
1
<?xml version="1.0" encoding="UTF-8"?>
2
<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">
3
 
4
    <modelVersion>4.0.0</modelVersion>
5
 
6
    <groupId>org.example</groupId>
7
    <artifactId>independent-project</artifactId>
8
    <version>1.0-SNAPSHOT</version>
9
 
10
    <dependencyManagement>
11
        <dependencies>
12
            <dependency>
13
                <artifactId>maven-dependency-management</artifactId>
14
                <groupId>org.example</groupId>
15
                <version>1.0-SNAPSHOT</version>
16
                <type>pom</type>
17
                <scope>import</scope>
18
            </dependency>
19
        </dependencies>
20
    </dependencyManagement>
21
 
22
    <dependencies>
23
        <dependency>
24
            <groupId>com.typesafe.akka</groupId>
25
            <artifactId>akka-stream_2.12</artifactId>
26
        </dependency>
27
        <dependency>
28
            <groupId>com.typesafe.akka</groupId>
29
            <artifactId>akka-http_2.12</artifactId>
30
        </dependency>
31
        <dependency>
32
            <groupId>com.typesafe.akka</groupId>
33
            <artifactId>akka-http-spray-json_2.12</artifactId>
34
        </dependency>
35
    </dependencies>
36
</project>



As you can see in the block

XML
 




xxxxxxxxxx
1
12


 
1
<dependencyManagement>
2
    <dependencies>
3
        <dependency>
4
            <artifactId>maven-dependency-management</artifactId>
5
            <groupId>org.example</groupId>
6
            <version>1.0-SNAPSHOT</version>
7
            <type>pom</type>
8
            <scope>import</scope>
9
        </dependency>
10
    </dependencies>
11
</dependencyManagement>
12

          



We just included the dependency management from another project, which can be applied to inherit dependencies from multiple projects.

We just achieved managing the dependencies for multiple projects using either a parent project or just another project.

Dependency

Published at DZone with permission of Emmanouil Gkatziouras. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Retrieval Augmented Generation With Spring AI 2.0, Claude, and PGvector
  • Fix Circular Dependencies in PostgreSQL Row-Level Security With SECURITY DEFINER Functions
  • Sharing SBOMs Securely Without Giving Too Much Away
  • Securing CI/CD Pipelines Against Supply Chain Attacks: Why Artifacts and Dependencies Matter More Than Ever

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook