SyntaxStudy
Sign Up
Java Multi-module Builds and Dependency Management
Java Beginner 1 min read

Multi-module Builds and Dependency Management

Both Maven and Gradle support multi-module (multi-project) builds, which are essential for large applications split into reusable modules. In Maven, the root pom.xml acts as a parent with packaging set to pom and lists child modules in a modules section. Each child module has its own pom.xml with a parent element pointing to the root. The parent POM typically defines dependency versions in dependencyManagement so child modules can declare dependencies without specifying versions — guaranteeing consistency across the project. In Gradle, the settings.gradle file declares all subprojects with include(":core"), include(":api"), include(":service"). Each subproject has its own build.gradle. To express that one module depends on another, you use project(":module-name") in the dependencies block: implementation project(":core"). Gradle resolves inter-project dependencies as part of its task graph, so building the root project triggers builds of all dependent subprojects in the correct order. Build profiles in Maven allow you to conditionally apply configuration based on environment (dev/test/prod). Gradle achieves similar results through project properties (-P flag) or environment variables. Both tools integrate with CI/CD pipelines and support publishing artifacts to repositories like Nexus or Artifactory. Understanding the basics of both tools is important for Java developers because the choice between them varies by team and project.
Example
// ---- Maven multi-module structure ----
// Root pom.xml
// <modules>
//   <module>core</module>
//   <module>api</module>
// </modules>
//
// core/pom.xml — child module (excerpt)
// <parent>
//   <groupId>com.example</groupId>
//   <artifactId>my-app</artifactId>
//   <version>1.0.0</version>
// </parent>
// <artifactId>core</artifactId>

// ============================================================
// Gradle multi-project: settings.gradle
// ============================================================
// rootProject.name = 'my-app'
// include(':core')
// include(':api')

// ============================================================
// api/build.gradle — depends on :core
// ============================================================
plugins {
    id 'java'
}

dependencies {
    implementation project(':core')          // inter-project dep
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.15.2'
    testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0'
}

// ============================================================
// Root build.gradle — shared config via allprojects / subprojects
// ============================================================
subprojects {
    apply plugin: 'java'

    java {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }

    repositories {
        mavenCentral()
    }

    test {
        useJUnitPlatform()
    }
}

This is the last lesson in this section.

Create a free account to earn a certificate