Home >Java >javaTutorial >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:
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 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>
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=falseThe above command will create a sample project using Maven's Quick Start Prototype.
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
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 packageAbove The command will compile the project source code and package the results generated by the compilation.
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>
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!