In-depth analysis of detailed steps for Maven installation and configuration
Deeply explore the detailed steps of Maven installation and configuration
Abstract:
Maven is a powerful tool for project construction and dependency management. This article will introduce the installation and configuration steps of Maven in detail and provide specific code examples.
Keywords: Maven, installation, configuration, project construction, dependency management
1. Maven installation
Maven installation is very simple, just follow the following steps:
- Download Maven
Visit the Maven official website (https://maven.apache.org/download.cgi) and download the latest version of the Maven binary file suitable for your operating system. Here we use Windows is an example. - Extract Maven
Extract the downloaded binary file to any directory on your computer, for example: D: pache-maven-3.8.4. - Configure environment variables
Add Maven's bin directory (for example: D: pache-maven-3.8.4 in) to the system's environment variable PATH. -
Verify installation
Open the command line window and enter the following command to verify whether Maven is installed successfully:mvn -v
If you see information similar to the following, the installation is successful:
Apache Maven 3.8.4 Java version: 11.0.11, vendor: Oracle Corporation, runtime: C:Program FilesJavajdk-11.0.11injava.exe Default locale: en_US, platform encoding: GBK OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
2. Maven configuration
- Modify settings.xml
In the Maven installation directory, find the settings.xml file in the conf folder, and Open with a text editor. -
Modify the local warehouse path
Find the following code segment in the settings.xml file:<!-- 本地仓库路径 --> <localRepository>${user.home}/.m2/repository</localRepository>
Modify
${user.home}
Set the path to the local warehouse you want to set, for example:<!-- 本地仓库路径 --> <localRepository>D:/maven/repository</localRepository>
-
Configuring the image
Find the following code snippet in the settings.xml file:<!-- 镜像 --> <mirrors> <mirror> <id>aliyun-central</id> <name>aliyun central</name> <mirrorOf>central</mirrorOf> <url>https://maven.aliyun.com/repository/central</url> </mirror> </mirrors>
In
<mirrors></mirrors>
Add the following code snippet under the node:<!-- 阿里云仓库 --> <mirror> <id>aliyun-public</id> <name>aliyun public</name> <mirrorOf>central</mirrorOf> <url>https://maven.aliyun.com/repository/public</url> </mirror>
Configure proxy
If you use a proxy server to access the Internet, you can # in the settings.xml file ##Add the following code snippet under the node, replacing proxy.host and proxy.port with your proxy server host name and port number:
<!-- 代理服务器 --> <proxy> <id>proxy</id> <active>true</active> <protocol>http</protocol> <host>proxy.host</host> <port>proxy.port</port> <username>proxy.username</username> <password>proxy.password</password> <nonProxyHosts>localhost|127.0.0.1</nonProxyHosts> </proxy>
- Create Maven project
In the command line window, enter the directory where you want to create the Maven project, and execute the following command to create the Maven project:
mvn archetype:generate -DgroupId=com.example -DartifactId=my-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
The above command will create a sample project using Maven's Quick Start Prototype. - Directory structure of the project
After successful creation, you will see a new folder my-project under the directory, which will contain the following directories and files:
my-project ├── pom.xml └── src ├── main │ └── java │ └── com │ └── example │ └── App.java └── test └── java └── com └── example └── AppTest.java
- Compile and build the project
In the command line window, enter the root directory of the project, my-project, and execute the following commands to compile and build the project:
mvn compile mvn package
Above The command will compile the project source code and package the results generated by the compilation. - Import dependencies
In the pom.xml file, use the
node to add the dependency libraries required by the project. For example, add the following code snippet to import JUnit dependencies:
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.1</version> <scope>test</scope> </dependency> </dependencies>
- Run the project
In the command line window, go to the root directory of the project my-project and execute the following command to run the project :
mvn exec:java -Dexec.mainClass="com.example.App"
The above command will execute the main method in App.java.
This article introduces the installation and configuration steps of Maven and provides specific code examples. By correctly installing and configuring Maven, we can easily build and manage the dependency libraries of Java projects. I hope this article is helpful to you using Maven.
The above is the detailed content of In-depth analysis of detailed steps for Maven installation and configuration. For more information, please follow other related articles on the PHP Chinese website!

Using POI library in Java to add borders to Excel files Many Java developers are using Apache...

Efficient processing of batch interface requests: Using CompletableFuture to ensure that concurrent calls to third-party interfaces can significantly improve efficiency when processing large amounts of data. �...

In JavaWeb applications, the feasibility of implementing entity-class caching in Dao layer When developing JavaWeb applications, performance optimization has always been the focus of developers. Either...

The current status of motorcycle and motorcycle systems and ecological development of motorcycle systems, as an important bridge connecting knights and vehicles, has developed rapidly in recent years. Many car friends...

When using MyBatis-Plus or tk.mybatis...

How to query personnel data through natural language processing? In modern data processing, how to efficiently query personnel data is a common and important requirement. ...

In processing next-auth generated JWT...

In IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)