❌ نکتهی مهم : طبق این مراحل پیش نرید و صرفا اون رو بخونید (مخصوصا اگر از intelliJ idea استفاده میکنید) چرا که راههای بهتری برای انجام این کار هست و این مقاله صرفا برای آشنا کردن شما با کلیت و روال انجام کارها در Maven و از اون مهمتر، مرور اون در آیندهست.
بریم که شروع کنیم :))
A comprehensive guide on using Maven in your Java projects, starting from the basics and covering everything you need to know. Here’s a step-by-step approach to get you started with Maven :
Step 1: Install Maven
Download the latest version of Apache Maven from the official website (https://maven.apache.org/download.cgi).
Follow the installation instructions provided for your operating system.
Step 2: Project Structure
Create a new directory for your Java project.
Inside the project directory, create the following structure :
project
├── src
│ ├── main
│ │ └── java
│ │ └── com
│ │ └── yourcompany
│ │ └── yourproject
│ │ └── Main.java
│ └── test
│ └── java
│ └── com
│ └── yourcompany
│ └── yourproject
│ └── MainTest.java
└── pom.xml
Step 3: Define Project Configuration (pom.xml)
The Project Object Model (POM) is an XML file that defines your project’s configuration, dependencies, plugins, and more.
Create a file named pom.xml
in the root of your project directory.
Define the basic structure of the pom.xml
file, including the project details, such as group ID, artifact ID, version, and dependencies.
Example pom.xml
structure:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yourcompany</groupId>
<artifactId>yourproject</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- Add your dependencies here -->
</dependencies>
</project>
Step 4: Add Dependencies
Declare the dependencies your project requires in the <dependencies>
section of the pom.xml
file.
Specify the dependencies using their group ID, artifact ID, and version.
Maven will automatically download the required dependencies and manage their transitive dependencies.
Example dependency declaration:
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
</dependencies>
Step 5: Build and Compile
Open a terminal/command prompt and navigate to your project’s root directory (where the pom.xml
file is located).Run the following Maven command to build and compile your project:
mvn compile
Step 6: Run Tests
If you have test classes defined in the src/test/java
directory, you can run the tests using the following command:
mvn test
Step 7: Package Your Application
Maven can create an executable JAR or other distribution formats for your project.
To package your application, run the following command:
mvn package
Step 8: Execute Your Application
After packaging your application, you can execute it using the java -jar
command. The JAR fil will be located in the target
directory.Example execution command:
java -jar target/yourproject-1.0.0.jar
Step 9: Further Maven Usage
Maven offers a wide range of functionalities and plugins for tasks such as code quality checks, documentation generation, deployment, and more.Explore Maven’s documentation and various plugins to extend the capabilities of your build process.
Congratulations! 🙂
You now have a basic understanding of how to use Maven in your Java projects. Maven simplifies dependency management, build processes, and project configuration, making it a powerful tool for Java development.
0 دیدگاه