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

Java Gradle की मूल बातें

Gradle एक modern build automation tool है जो Maven के fixed XML format के बजाय एक programmable build script -- Groovy या Kotlin DSL में -- उपयोग करता है, ज़्यादा flexibility और आमतौर पर तेज़ builds प्रदान करते हुए।

What is Gradle?

Gradle Java (और दूसरी languages) के लिए एक modern build automation tool है जो एक fixed XML format के बजाय एक programmable build script उपयोग करता है, Maven से ज़्यादा flexibility और आमतौर पर तेज़ incremental builds प्रदान करते हुए।

उदाहरण: What is Gradle?

java
// 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");
	}
}

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

java
// 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);
	}
}

Gradle Tasks

Gradle काम को tasks में organize करता है, जैसे compileJava, test, और build, और tasks एक-दूसरे पर निर्भर हो सकते हैं ताकि एक चलाना अपने आप पहले चाहिए वाले tasks को trigger करे, Maven के lifecycle phases के समान।

उदाहरण: Gradle Tasks

java
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);
	}
}

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

java
// 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);
	}
}

Gradle vs Maven

Gradle और Maven एक ही core समस्या हल करते हैं -- किसी project को उसकी dependencies के साथ compile, test, और package करना -- लेकिन Gradle का script-based configuration आमतौर पर ज़्यादा flexible है और इसका incremental build caching आमतौर पर तेज़ है।

उदाहरण: Gradle vs Maven

java
// 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");
	}
}
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. build file में compile configuration का उपयोग करना, जो नए Gradle versions में हटा दिया गया; implementation या api का उपयोग करें।
  2. Groovy syntax को Kotlin DSL file में copy करना, जिसे implementation("group:name:1.0") जैसे parentheses और double quotes चाहिए।
  3. project के ./gradlew wrapper के बजाय installed gradle चलाना, जो एक अलग Gradle version उपयोग कर सकता है।
🔒

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.