← Back to Advanced Java Course | Chapter 12: Build Tools | Lesson 5 of 5

Java के Multi-Module Projects

एक multi-module project संबंधित sub-projects, जैसे API, service, और web layer, को एक parent के तहत group करता है ताकि वे configuration share कर सकें और एक unit के रूप में साथ build हो सकें।

What is a Multi-Module Project?

एक multi-module project कई संबंधित sub-projects, जैसे एक API layer, एक service layer, और एक web layer, को एक single parent project के तहत group करता है, उन्हें configuration share करने देते हुए और एक unit के रूप में साथ build होने देते हुए।

उदाहरण: What is a Multi-Module Project?

java
public class Main {
	public static void main(String[] args) {
		String[] modules = {"api", "service", "web"}; // share config, built together
		for (String m : modules) System.out.println(m);
	}
}

Parent POM Structure

Maven में, parent module की packaging type jar के बजाय pom होती है, क्योंकि यह सिर्फ उन shared properties, dependency versions, और plugin configuration को declare करने के लिए मौजूद है जिन्हें हर child module inherit करता है।

उदाहरण: Parent POM Structure

java
public class Main {
	public static void main(String[] args) {
		String parentPom = "<packaging>pom</packaging>"; // declares shared config only, not a jar itself
		System.out.println(parentPom);
	}
}

Module Dependencies

एक ही multi-module project के अंदर modules एक-दूसरे पर बिल्कुल external libraries की तरह निर्भर हो सकते हैं, उन्हीं groupId, artifactId, और version coordinates का उपयोग करते हुए, build system को सही build order अपने आप track करने देते हुए।

उदाहरण: Module Dependencies

java
public class Main {
	public static void main(String[] args) {
		String moduleDependency = "<groupId>com.example</groupId>\n<artifactId>api</artifactId>\n<version>1.0</version>";
		System.out.println(moduleDependency); // same coordinates as an external library
	}
}

Building All Modules Together

parent project के root से एक build command चलाना सही dependency order में हर module बनाता है, जबकि -pl और -am जैसे flags आपको पूरे project को फिर से build किए बिना किसी specific module और उसकी dependencies को target करने देते हैं।

उदाहरण: Building All Modules Together

java
public class Main {
	public static void main(String[] args) {
		String buildAll = "mvn install"; // builds every module in dependency order
		String buildOne = "mvn install -pl service -am"; // targets one module plus its dependencies
		System.out.println(buildAll + " / " + buildOne);
	}
}

Gradle Multi-Project Builds

Gradle multi-project builds को एक settings.gradle file के ज़रिए configure किया जाता है जो हर sub-project को list करती है, और एक sub-project पूरे external dependency coordinates के बजाय Gradle के project() function का उपयोग करके दूसरे पर निर्भर हो सकता है।

उदाहरण: Gradle Multi-Project Builds

java
public class Main {
	public static void main(String[] args) {
		String settingsGradle = "include 'api', 'service', 'web'";
		String dependency = "implementation project(':api')"; // internal, not external coordinates
		System.out.println(settingsGradle + " / " + dependency);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. parent की packaging को pom के बजाय jar छोड़ देना, जबकि Maven को सिर्फ modules group करने वाले parent के लिए इसकी ज़रूरत है।
  2. दो modules के बीच एक circular dependency बनाना, जिसे Maven बनाने से मना कर देता है।
  3. parent के <modules> में किसी नए module को list करना भूल जाना, जिससे यह बाकी के साथ नहीं बनता।
चैप्टर सारांश
  • Maven और Gradle build tools हैं जो Java projects को compile, test, और package करते हैं।
  • Maven plugins और profiles builds को customize करते हैं, और dependency management आपकी ज़रूरत की libraries लाती है।
  • Multi-module projects एक बड़े application को छोटे हिस्सों में बांटते हैं।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.