Java Maven की मूल बातें
In this page:
<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?
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");
}
}
Login to try C/C++/Java/PHP code in the editor
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
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
}
}
Login to try C/C++/Java/PHP code in the editor
Maven Build Lifecycle
Maven का default build lifecycle phases का एक निश्चित sequence है -- validate, compile, test, package, verify, install, और deploy -- और कोई भी phase चलाना पहले उस sequence में उससे पहले की हर phase अपने आप चला देता है।
उदाहरण: Maven Build Lifecycle
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
}
}
Login to try C/C++/Java/PHP code in the editor
Adding Dependencies
Dependencies को pom.xml के dependencies element के अंदर declare किया जाता है, और Maven उन्हें, उनकी अपनी dependencies (जिन्हें transitive dependencies कहा जाता है) सहित, Maven Central जैसे एक central repository से अपने आप download कर लेता है।
उदाहरण: Adding Dependencies
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
}
}
Login to try C/C++/Java/PHP code in the editor
Running Maven Commands
आम Maven commands में source code बनाने के लिए mvn compile, tests चलाने के लिए mvn test, एक distributable artifact produce करने के लिए mvn package, और नई शुरुआत करने से पहले पिछला build output हटाने के लिए mvn clean शामिल हैं।
उदाहरण: Running Maven Commands
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);
}
}
Login to try C/C++/Java/PHP code in the editor
- बिना
pom.xmlवाले folder मेंmvnचलाना, जो 'no POM in this directory' के साथ fail हो जाता है। - यह उम्मीद करना कि
mvn packageartifact को local repository में install करेगा, जबकि इसके लिएmvn installचाहिए। - किसी dependency के
groupId,artifactIdया version की गलत spelling, जिससे Maven इसे resolve नहीं कर पाता।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: