Java के Multi-Module Projects
In this page:
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?
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);
}
}
Login to try C/C++/Java/PHP code in the editor
Parent POM Structure
Maven में, parent module की packaging type jar के बजाय pom होती है, क्योंकि यह सिर्फ उन shared properties, dependency versions, और plugin configuration को declare करने के लिए मौजूद है जिन्हें हर child module inherit करता है।
उदाहरण: Parent POM Structure
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);
}
}
Login to try C/C++/Java/PHP code in the editor
Module Dependencies
एक ही multi-module project के अंदर modules एक-दूसरे पर बिल्कुल external libraries की तरह निर्भर हो सकते हैं, उन्हीं groupId, artifactId, और version coordinates का उपयोग करते हुए, build system को सही build order अपने आप track करने देते हुए।
उदाहरण: Module Dependencies
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
}
}
Login to try C/C++/Java/PHP code in the editor
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
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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
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);
}
}
Login to try C/C++/Java/PHP code in the editor
- parent की packaging को
pomके बजायjarछोड़ देना, जबकि Maven को सिर्फ modules group करने वाले parent के लिए इसकी ज़रूरत है। - दो modules के बीच एक circular dependency बनाना, जिसे Maven बनाने से मना कर देता है।
- 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: