Java में Dependency Management
In this page:
What is a Dependency?
एक dependency एक external library है जिसकी किसी project को compile या run करने के लिए ज़रूरत होती है, build file में coordinates के साथ declared जो यह बताते हैं कि Maven Central जैसे किसी repository से बिल्कुल कौन सी library और version download करनी है।
उदाहरण: What is a Dependency?
public class Main {
public static void main(String[] args) {
String coordinates = "com.google.guava:guava:32.1.0"; // groupId:artifactId:version
System.out.println(coordinates);
}
}
Login to try C/C++/Java/PHP code in the editor
Direct vs Transitive Dependencies
एक direct dependency वह है जिसे build file में explicitly listed किया गया है, जबकि एक transitive dependency वह है जो अपने आप इसलिए आती है क्योंकि कोई direct dependency खुद उस पर निर्भर करती है, अक्सर जितनी कभी directly declare की गईं उससे कहीं ज़्यादा libraries classpath पर ला देते हुए।
उदाहरण: Direct vs Transitive Dependencies
public class Main {
public static void main(String[] args) {
String direct = "junit:junit:4.13"; // explicitly listed
String transitive = "hamcrest-core"; // pulled in automatically because junit depends on it
System.out.println(direct + " brings in " + transitive);
}
}
Login to try C/C++/Java/PHP code in the editor
Dependency Scopes
Dependency scopes नियंत्रित करते हैं कि कोई dependency कब और कहां उपलब्ध है -- compile scope हर जगह उपलब्ध है, test scope सिर्फ testing के दौरान, और provided scope यह मानता है कि runtime environment पहले से library प्रदान करता है।
उदाहरण: Dependency Scopes
public class Main {
public static void main(String[] args) {
String compileScope = "compile"; // available everywhere
String testScope = "test"; // only during testing
String providedScope = "provided"; // runtime environment already supplies it
System.out.println(compileScope + " " + testScope + " " + providedScope);
}
}
Login to try C/C++/Java/PHP code in the editor
Resolving Version Conflicts
Version conflicts तब होते हैं जब दो dependencies transitively एक ही library के अलग versions चाहती हैं, और build tools nearest-wins जैसे rules का उपयोग करके इसे अपने आप हल करते हैं, हालांकि dependencyManagement explicitly एक exact version pin कर सकता है।
उदाहरण: Resolving Version Conflicts
public class Main {
public static void main(String[] args) {
String pinned = "<dependencyManagement>...<version>2.0</version></dependencyManagement>"; // pins the exact version
System.out.println(pinned);
}
}
Login to try C/C++/Java/PHP code in the editor
Excluding Dependencies
कोई transitive dependency कभी-कभी अवांछित हो सकती है, जैसे कोई अनचाहा embedded server या एक conflicting logging library, और Maven और Gradle दोनों किसी declared dependency से specific transitive dependencies को explicitly exclude करने को support करते हैं।
उदाहरण: Excluding Dependencies
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
String exclusion = "<exclusions><exclusion><artifactId>unwanted-logger</artifactId></exclusion></exclusions>";
// Print a line to the console
System.out.println(exclusion);
}
}
Login to try C/C++/Java/PHP code in the editor
- बिना declare किए किसी transitive dependency पर भरोसा करना, जिससे जो library इसे लाई थी उसके इसे शामिल करना बंद करने पर build टूट जाती है।
- किसी test library को compile scope में declare करना, या runtime पर ज़रूरी किसी library को
testscope में, जिससे यह वहां गायब या bundled होती है जहां नहीं होनी चाहिए। - libraries के बीच version conflicts को नज़रअंदाज़ करना, जबकि आपको
mvn dependency:treeसे उन्हें inspect करना चाहिए और जान-बूझकर versions चुनना या exclude करना चाहिए।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: