Java Maven के Plugins और Profiles
In this page:
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?
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");
}
}
Login to try C/C++/Java/PHP code in the editor
The Compiler Plugin
maven-compiler-plugin नियंत्रित करता है कि Java source code कैसे compile होता है, और इसकी source और target properties configure करना तय करता है कि build किस Java language version की उम्मीद करता है और किसके लिए bytecode produce करता है।
उदाहरण: The Compiler Plugin
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
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);
}
}
Login to try C/C++/Java/PHP code in the editor
Build Profiles
एक build profile pom.xml configuration को context के आधार पर बदलने देता है, जैसे development बनाम production के लिए अलग settings, और एक profile तभी लागू होता है जब इसे explicitly activate किया जाए, अक्सर command line पर नाम से।
उदाहरण: Build Profiles
public class Main {
public static void main(String[] args) {
String activation = "mvn install -Pproduction"; // profile only applied when explicitly activated
System.out.println(activation);
}
}
Login to try C/C++/Java/PHP code in the editor
Custom Plugin Configuration
Plugins को किसी project की ज़रूरतों के लिए specific custom settings से configure किया जा सकता है, जैसे किसी jar के manifest में एक main class embed करना ताकि इसे सीधे चलाया जा सके, या development के दौरान exec plugin के ज़रिए किसी specific class को invoke करना।
उदाहरण: Custom Plugin Configuration
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);
}
}
Login to try C/C++/Java/PHP code in the editor
- test classes को ऐसे नाम देना जिन्हें Surefire default रूप से नहीं पकड़ता, जिससे tests skip हो जाते हैं;
*Testया*Testsजैसे नामों का उपयोग करें। - कोई profile define करना और उसके अपने आप चलने की उम्मीद करना, जबकि इसे
-P profileNameया किसी activation rule से activate करना चाहिए। - compiler source और target versions सेट करना जो उपयोग किए जा रहे JDK से मेल न खाएं, जो build errors का कारण बनता है।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: