search
HomeJavajavaTutorialIn-depth analysis of detailed steps for Maven installation and configuration

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

    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

  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>

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

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

    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>
  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>

三, 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

    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

  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

    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>

  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"

    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!

Statement
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
How to add complex borders to Excel cells using GrapeCity Documents for Java library in Java?How to add complex borders to Excel cells using GrapeCity Documents for Java library in Java?Apr 19, 2025 pm 08:39 PM

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

How to use CompletableFuture to ensure the order consistency of batch interface request results?How to use CompletableFuture to ensure the order consistency of batch interface request results?Apr 19, 2025 pm 08:36 PM

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, is it reasonable for Dao layer to cache all personnel entity classes?In JavaWeb applications, is it reasonable for Dao layer to cache all personnel entity classes?Apr 19, 2025 pm 08:33 PM

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...

Which motorcycle and motorcycle system is better? Comparison of advantages and disadvantages between open Android system and closed self-developed systemWhich motorcycle and motorcycle system is better? Comparison of advantages and disadvantages between open Android system and closed self-developed systemApr 19, 2025 pm 08:30 PM

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...

How to get Java entity class attribute names elegantly to avoid hard-coded in MyBatis queries?How to get Java entity class attribute names elegantly to avoid hard-coded in MyBatis queries?Apr 19, 2025 pm 08:27 PM

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

How to efficiently query personnel data in MySql and ElasticSearch through natural language processing?How to efficiently query personnel data in MySql and ElasticSearch through natural language processing?Apr 19, 2025 pm 08:24 PM

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. ...

How to parse next-auth generated JWT token in Java and get information in it?How to parse next-auth generated JWT token in Java and get information in it?Apr 19, 2025 pm 08:21 PM

In processing next-auth generated JWT...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

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

SublimeText3 Mac version

God-level code editing software (SublimeText3)