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

Java Maven के Plugins और Profiles

Plugins Maven के behavior को extend करते हैं, और profiles build configuration को environment जैसे context के आधार पर बदलने देते हैं, दोनों निश्चित lifecycle को किसी project की असली ज़रूरतों के अनुकूल बनाते हैं।

What are Maven Plugins?

Plugins Maven के built-in lifecycle से आगे यह extend करते हैं कि यह क्या कर सकता है, और Maven का हर मायने रखने वाला action, source code compile करना भी, असल में किसी plugin द्वारा दिए गए goal के रूप में implement किया जाता है जो किसी lifecycle phase से bound है।

उदाहरण: What are Maven Plugins?

java
public class Main {
	public static void main(String[] args) {
		// Even "compile" is implemented by maven-compiler-plugin's compile goal
		System.out.println("Every Maven action is a plugin goal bound to a lifecycle phase");
	}
}

The Compiler Plugin

maven-compiler-plugin नियंत्रित करता है कि Java source code कैसे compile होता है, और इसकी source और target properties configure करना तय करता है कि build किस Java language version की उम्मीद करता है और किसके लिए bytecode produce करता है।

उदाहरण: The Compiler Plugin

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		String config = "<plugin>\n  <artifactId>maven-compiler-plugin</artifactId>\n  <configuration><source>17</source><target>17</target></configuration>\n</plugin>";
		// Print a line to the console
		System.out.println(config);
	}
}

The Surefire Plugin for Tests

maven-surefire-plugin test phase के दौरान unit tests चलाता है और इसे किसी specific test class को चलाने के लिए configure या target किया जा सकता है, यह control करने के लिए कि failures build को रोकें या नहीं, या तेज़ build के लिए tests पूरी तरह skip करें।

उदाहरण: The Surefire Plugin for Tests

java
public class Main {
	public static void main(String[] args) {
		String command = "mvn test -Dtest=MyTestClass"; // targets a specific test class via surefire
		System.out.println(command);
	}
}

Build Profiles

एक build profile pom.xml configuration को context के आधार पर बदलने देता है, जैसे development बनाम production के लिए अलग settings, और एक profile तभी लागू होता है जब इसे explicitly activate किया जाए, अक्सर command line पर नाम से।

उदाहरण: Build Profiles

java
public class Main {
	public static void main(String[] args) {
		String activation = "mvn install -Pproduction"; // profile only applied when explicitly activated
		System.out.println(activation);
	}
}

Custom Plugin Configuration

Plugins को किसी project की ज़रूरतों के लिए specific custom settings से configure किया जा सकता है, जैसे किसी jar के manifest में एक main class embed करना ताकि इसे सीधे चलाया जा सके, या development के दौरान exec plugin के ज़रिए किसी specific class को invoke करना।

उदाहरण: Custom Plugin Configuration

java
public class Main {
	public static void main(String[] args) {
		String manifest = "<mainClass>com.example.Main</mainClass>"; // embedded in the jar's manifest
		System.out.println(manifest);
	}
}
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. test classes को ऐसे नाम देना जिन्हें Surefire default रूप से नहीं पकड़ता, जिससे tests skip हो जाते हैं; *Test या *Tests जैसे नामों का उपयोग करें।
  2. कोई profile define करना और उसके अपने आप चलने की उम्मीद करना, जबकि इसे -P profileName या किसी activation rule से activate करना चाहिए।
  3. compiler source और target versions सेट करना जो उपयोग किए जा रहे JDK से मेल न खाएं, जो build errors का कारण बनता है।
🔒

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.