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

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Low-Code Development: Leverage low and no code to streamline your workflow so that you can focus on higher priorities.

DZone Security Research: Tell us your top security strategies in 2024, influence our research, and enter for a chance to win $!

Launch your software development career: Dive head first into the SDLC and learn how to build high-quality software and teams.

Open Source Migration Practices and Patterns: Explore key traits of migrating open-source software and its impact on software development.

Related

  • How To Validate Names Using Java
  • DDD and Spring Boot Multi-Module Maven Project
  • Custom Policy in Mule 4
  • Building Java Applications With Maven

Trending

  • Build an Advanced RAG App: Query Rewriting
  • Next-Gen Lie Detector: Stack Selection
  • Tenv v2.0: The Importance of Explicit Behavior for Version Manager
  • Building an Effective Zero Trust Security Strategy for End-To-End Cyber Risk Management
  1. DZone
  2. Coding
  3. Java
  4. Maven Dependency Scope Applied

Maven Dependency Scope Applied

Learn how to use Maven's dependency scope to adhere to the chosen architecture in a multi-module project.

By 
Maksim Kren user avatar
Maksim Kren
·
Jan. 22, 24 · Analysis
Like (3)
Save
Tweet
Share
3.6K Views

Join the DZone community and get the full member experience.

Join For Free

To get started, we are going to take an example of a typical three-layer app to analyze the module boundaries and the challenges faced in managing them. This specific architecture has been intentionally chosen, assuming that it is familiar to everyone. What should let us focus on Maven dependency management by resolving these issues?

Three-Layer App Sample

The source code for this can be found at the following link.

Three-Layer App Sample

In line with the three-layer architecture, the goal is to have layers that remain isolated from each other, with interactions occurring only through the APIs of nearby layers (modules). 

The typical Maven module configuration for this kind of structure would look something like this.

XML
 
<!--web module pom.xml -->
<dependencies>
  <dependency>
    <groupId>com.example</groupId>
    <artifactId>service</artifactId>
    <version>1.0.0</version>
  </dependency>  
</dependencies>

<!--service module pom.xml -->
<dependencies>
  <dependency>
    <groupId>com.example</groupId>
    <artifactId>dao</artifactId>
    <version>1.0.0</version>
  </dependency>
</dependencies>


Our main concern here is that this setup needs to provide the level of encapsulation we desire for our modules. For instance, it would still be possible to use any public classes declared in the dao module within both the service and the web modules. This could break down the initial design and result in a tightly-coupled application as it grows and evolves.

Given the above, let's have a look at how we can improve this solution to gain better control over module boundaries by restructuring our initial app sample a bit and updating its pom.xml files.

Improved Three-Layer App Sample

The source code for this can be found at the following link.

Improved Three-Layer App Sample

XML
 
<!--web module pom.xml -->
<dependencies>
  <dependency>
    <groupId>com.example</groupId>
    <artifactId>service-api</artifactId>
    <version>1.0.0</version>
  </dependency>
  <dependency>
    <groupId>com.example</groupId>
    <artifactId>service</artifactId>
    <version>1.0.0</version>
    <scope>runtime</scope>
  </dependency> 
</dependencies>

<!--service module pom.xml -->
<dependencies>
  <dependency>
    <groupId>com.example</groupId>
    <artifactId>dao-api</artifactId>
    <version>1.0.0</version>
  </dependency>
  <dependency>
    <groupId>com.example</groupId>
    <artifactId>dao</artifactId>
    <version>1.0.0</version>
    <scope>runtime</scope>
  </dependency>
  <dependency>
    <groupId>com.example</groupId>
    <artifactId>service-api</artifactId>
    <version>1.0.0</version>
  </dependency>
</dependencies>

<!--dao module pom.xml -->
<dependencies>
  <dependency>
    <groupId>com.example</groupId>
    <artifactId>dao-api</artifactId>
    <version>1.0.0</version>
  </dependency>
</dependencies>


Two new modules have been added in this improved version: the dao-api and the service-api. Moreover, apart from the default compile scope, some dependencies now have the runtime scope. According to Maven's Dependency Scope documentation:

  • Compile: This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.
  • Runtime: This scope indicates that the dependency is not required for compilation, but is for execution. Maven includes a dependency with this scope in the runtime and test classpaths but not the compile classpath.

The result of this configuration is that only the DAO interfaces from the dao-api are available in the service module, ensuring that there's no exposure of the dao internals. The same applies to the web module regarding the service.

While adding new modules might require more effort and might be considered as an extra complexity, it is a reasonable trade-off for improved encapsulation, which is essential to adhere to the chosen design.

Conclusion

Through this article, we have shed light on a simple yet frequently overlooked approach to managing dependencies in the multi-module project with Maven. While it may require a fair amount of effort, its application can result in a significant improvement in the control of module boundaries. This is particularly true when possible alternatives like the Java Platform Module System or ArchUnit are not viable for some reason.

Apache Maven Documentation Implementation application Module pattern Dependency

Opinions expressed by DZone contributors are their own.

Related

  • How To Validate Names Using Java
  • DDD and Spring Boot Multi-Module Maven Project
  • Custom Policy in Mule 4
  • Building Java Applications With Maven

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: