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
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Securing CI/CD Pipelines Against Supply Chain Attacks: Why Artifacts and Dependencies Matter More Than Ever
  • Clean Code: Package Architecture, Dependency Flow, and Scalability, Part 4
  • C/C++ Is Where Vulnerability Programs Go to Guess
  • Tracking Dependencies Beyond the Build Stage

Trending

  • Zero-Downtime Deployments for Java Apps on Kubernetes
  • Building Production-Grade GenAI on GCP with Vertex AI Agent Builder
  • Product-Led Software Delivery: Intelligent Platforms for DevOps at Scale
  • DevOps and Platform Engineering Readiness Checklist: Everything Needed for a Scalable, Secure, High-Velocity Delivery Platform
  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 user avatar
Emmanouil Gkatziouras
DZone Core CORE ·
Nov. 03, 20 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
14.1K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • Securing CI/CD Pipelines Against Supply Chain Attacks: Why Artifacts and Dependencies Matter More Than Ever
  • Clean Code: Package Architecture, Dependency Flow, and Scalability, Part 4
  • C/C++ Is Where Vulnerability Programs Go to Guess
  • Tracking Dependencies Beyond the Build Stage

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • 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