Home > Java > javaTutorial > In-depth analysis of detailed steps for Maven installation and configuration

In-depth analysis of detailed steps for Maven installation and configuration

WBOY
Release: 2024-01-05 14:21:12
Original
1351 people have browsed it

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:

  1. 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.
  2. Extract Maven
    Extract the downloaded binary file to any directory on your computer, for example: D: pache-maven-3.8.4.
  3. Configure environment variables
    Add Maven's bin directory (for example: D: pache-maven-3.8.4 in) to the system's environment variable PATH.
  4. Verify installation
    Open the command line window and enter the following command to verify whether Maven is installed successfully:

    mvn -v
    Copy after login

    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"
    Copy after login

2. Maven configuration

  1. Modify settings.xml
    In the Maven installation directory, find the settings.xml file in the conf folder, and Open with a text editor.
  2. Modify the local warehouse path
    Find the following code segment in the settings.xml file:

    <!-- 本地仓库路径 -->
    <localRepository>${user.home}/.m2/repository</localRepository>
    Copy after login

    Modify ${user.home} Set the path to the local warehouse you want to set, for example:

    <!-- 本地仓库路径 -->
    <localRepository>D:/maven/repository</localRepository>
    Copy after login
  3. 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>
    Copy after login

    In<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>
    Copy after login
  4. 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>
    Copy after login

三, Configuration and use of Maven project

  1. 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
    Copy after login

    The above command will create a sample project using Maven's Quick Start Prototype.

  2. 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
    Copy after login

  3. 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
    Copy after login

    Above The command will compile the project source code and package the results generated by the compilation.

  4. 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>
    Copy after login

  5. 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"
    Copy after login
    The above command will execute the main method in App.java.

Summary:

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template