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

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

Maven Java के लिए एक build automation और dependency management tool है, जो किसी project के coordinates, dependencies, और build lifecycle को describe करने के लिए एक declarative pom.xml file का उपयोग करता है।
Syntax
java
<dependency>
    <groupId>group.id</groupId>
    <artifactId>artifact-id</artifactId>
    <version>1.0.0</version>
</dependency>

What is Maven?

Maven Java projects के लिए एक build automation और dependency management tool है, जो यह describe करने के लिए pom.xml नाम की एक declarative XML file उपयोग करता है कि किसी project को क्या चाहिए, इसे कैसे बनाना है, और यह किन external libraries पर निर्भर है।

उदाहरण: What is Maven?

java
public class Main {
	public static void main(String[] args) {
		// pom.xml declares what the project needs, how to build it, and its dependencies
		System.out.println("Maven manages builds and dependencies declaratively via pom.xml");
	}
}

The pom.xml File

pom.xml file, जो Project Object Model का संक्षिप्त रूप है, हर Maven project का दिल है, इसके unique coordinates (groupId, artifactId, version), इसका packaging type, इसकी dependencies, और कोई भी custom build configuration declare करते हुए।

उदाहरण: The pom.xml File

java
public class Main {
	public static void main(String[] args) {
		String pomSnippet = "<groupId>com.example</groupId>\n<artifactId>app</artifactId>\n<version>1.0</version>";
		System.out.println(pomSnippet); // unique coordinates declared in pom.xml
	}
}

Maven Build Lifecycle

Maven का default build lifecycle phases का एक निश्चित sequence है -- validate, compile, test, package, verify, install, और deploy -- और कोई भी phase चलाना पहले उस sequence में उससे पहले की हर phase अपने आप चला देता है।

उदाहरण: Maven Build Lifecycle

java
public class Main {
	public static void main(String[] args) {
		String[] phases = {"validate", "compile", "test", "package", "verify", "install", "deploy"};
		for (String phase : phases) System.out.println(phase); // running one runs every phase before it too
	}
}

Adding Dependencies

Dependencies को pom.xml के dependencies element के अंदर declare किया जाता है, और Maven उन्हें, उनकी अपनी dependencies (जिन्हें transitive dependencies कहा जाता है) सहित, Maven Central जैसे एक central repository से अपने आप download कर लेता है।

उदाहरण: Adding Dependencies

java
public class Main {
	public static void main(String[] args) {
		String dependency = "<dependency>\n  <groupId>junit</groupId>\n  <artifactId>junit</artifactId>\n</dependency>";
		System.out.println(dependency); // Maven also downloads its transitive dependencies automatically
	}
}

Running Maven Commands

आम Maven commands में source code बनाने के लिए mvn compile, tests चलाने के लिए mvn test, एक distributable artifact produce करने के लिए mvn package, और नई शुरुआत करने से पहले पिछला build output हटाने के लिए mvn clean शामिल हैं।

उदाहरण: Running Maven Commands

java
public class Main {
	public static void main(String[] args) {
		String[] commands = {"mvn compile", "mvn test", "mvn package", "mvn clean"};
		for (String cmd : commands) System.out.println(cmd);
	}
}
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. बिना pom.xml वाले folder में mvn चलाना, जो 'no POM in this directory' के साथ fail हो जाता है।
  2. यह उम्मीद करना कि mvn package artifact को local repository में install करेगा, जबकि इसके लिए mvn install चाहिए।
  3. किसी dependency के groupId, artifactId या version की गलत spelling, जिससे Maven इसे resolve नहीं कर पाता।
🔒

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.