Java Gradle की मूल बातें
In this page:
What is Gradle?
Gradle Java (और दूसरी languages) के लिए एक modern build automation tool है जो एक fixed XML format के बजाय एक programmable build script उपयोग करता है, Maven से ज़्यादा flexibility और आमतौर पर तेज़ incremental builds प्रदान करते हुए।
उदाहरण: What is Gradle?
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Print a line to the console
System.out.println("Gradle uses a programmable build script instead of fixed XML");
}
}
Login to try C/C++/Java/PHP code in the editor
The build.gradle File
एक Gradle project को एक build.gradle (या build.gradle.kts) file के ज़रिए configure किया जाता है, जो standard tasks जोड़ने के लिए java जैसे plugins apply करता है, dependencies ढूंढने के लिए repositories declare करता है, और project की dependencies list करता है।
उदाहरण: The build.gradle File
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
String buildScript = "plugins { id 'java' }\nrepositories { mavenCentral() }\ndependencies { implementation 'com.example:lib:1.0' }";
// Print a line to the console
System.out.println(buildScript);
}
}
Login to try C/C++/Java/PHP code in the editor
Gradle Tasks
Gradle काम को tasks में organize करता है, जैसे compileJava, test, और build, और tasks एक-दूसरे पर निर्भर हो सकते हैं ताकि एक चलाना अपने आप पहले चाहिए वाले tasks को trigger करे, Maven के lifecycle phases के समान।
उदाहरण: Gradle Tasks
public class Main {
public static void main(String[] args) {
String[] tasks = {"compileJava", "test", "build"}; // build depends on test, which depends on compileJava
for (String t : tasks) System.out.println(t);
}
}
Login to try C/C++/Java/PHP code in the editor
Kotlin DSL vs Groovy DSL
Gradle build scripts Groovy DSL (build.gradle) में लिखे जा सकते हैं, जो dynamically typed और संक्षिप्त है, या Kotlin DSL (build.gradle.kts) में, जो statically typed है और ज़्यादा मज़बूत IDE support व compile-time checking देता है।
उदाहरण: Kotlin DSL vs Groovy DSL
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
String groovy = "build.gradle (Groovy, dynamically typed)";
String kotlin = "build.gradle.kts (Kotlin, statically typed)";
// Print a line to the console
System.out.println(groovy + " vs " + kotlin);
}
}
Login to try C/C++/Java/PHP code in the editor
Gradle vs Maven
Gradle और Maven एक ही core समस्या हल करते हैं -- किसी project को उसकी dependencies के साथ compile, test, और package करना -- लेकिन Gradle का script-based configuration आमतौर पर ज़्यादा flexible है और इसका incremental build caching आमतौर पर तेज़ है।
उदाहरण: Gradle vs Maven
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Print a line to the console
System.out.println("Both compile/test/package; Gradle's caching tends to be faster");
}
}
Login to try C/C++/Java/PHP code in the editor
- build file में
compileconfiguration का उपयोग करना, जो नए Gradle versions में हटा दिया गया;implementationयाapiका उपयोग करें। - Groovy syntax को Kotlin DSL file में copy करना, जिसे
implementation("group:name:1.0")जैसे parentheses और double quotes चाहिए। - project के
./gradlewwrapper के बजाय installedgradleचलाना, जो एक अलग Gradle version उपयोग कर सकता है।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: