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

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
Partner Zones Build AI Agents That Are Ready for Production
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
Partner Zones
Build AI Agents That Are Ready for Production

Just dropped: New 2026 “Cloud-Native Foundations” Trend Report. See how teams are tackling complexity, cost & reliability.

AI can investigate. Engineers still decide. See how both work together across incident response in this DZone + Datadog webinar on Oct. 29.

Related

  • How To Validate Names Using Java
  • Retrieval Augmented Generation With Spring AI 2.0, Claude, and PGvector
  • Tracking Dependencies Beyond the Build Stage
  • Automating Maven Dependency Upgrades Using AI

Trending

  • Event-Driven AI Systems With Kafka and Autonomous Agents
  • Inside terraform-provider-archive: A Memory Pattern From 2016 That Scales With Your Lambdas
  • From ETL, ELT, and EtLT to Agent: What Is Changing in Enterprise Data Engineering?
  • Your Terraform Monolith Isn't Too Big. It's Tightly Coupled.
  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
Likes (3)
Comment
Save
Tweet
Share
5.1K 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
 


  
    com.example
    service
    1.0.0
    




  
    com.example
    dao
    1.0.0
  


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
 


  
    com.example
    service-api
    1.0.0
  
  
    com.example
    service
    1.0.0
    runtime
   




  
    com.example
    dao-api
    1.0.0
  
  
    com.example
    dao
    1.0.0
    runtime
  
  
    com.example
    service-api
    1.0.0
  




  
    com.example
    dao-api
    1.0.0
  


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
  • Retrieval Augmented Generation With Spring AI 2.0, Claude, and PGvector
  • Tracking Dependencies Beyond the Build Stage
  • Automating Maven Dependency Upgrades Using AI

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