MAVEN installation and configuration tutorial (super detailed version)

Preface: Before the installation and configuration of MAVEN begins, let me introduce MAVEN to you.

1. Understanding of MAVEN

1.1 What is MAVEN?

Maven is a project construction and management tool. The development team can automatically complete the basic construction configuration of the project without spending much time. Maven uses a standard directory structure to realize the unification of project structure in different development tools. Maven provides commands such as cleaning, compiling, testing, installing, packaging, publishing, etc., which allow us to build projects more conveniently. At the same time, pom. Copy the jar to the project, which greatly liberates the programmer's labor.

1.2 Advantages of MAVEN

  • Automated builds: clean, compile, test, install, package, publish

  • jar package dependency management: automatically download jar and its dependent jar packages

  • Contract programming: unification of project structure can also be realized in various development tools

1.3 Functions of MAVEN

Using Maven can complete many corresponding functions for us: clean up code, automatically compile, complete testing, generate site reports, package (jar, war), and project deployment.

  • Clean up the code: equivalent to the effect of running clear

  • Automatic compilation: it is more convenient to realize the management of the project

  • Packaging: ordinary projects are packaged as jar packages, web projects are packaged as war packages

  • Project deployment: Only one line of command can realize the deployment of the project

2. MAVEN installation

2.1 MAVEN Download

Click Maven official website to jump to the official website and click Download

Windows users directly download bin.zip

After the download is complete, decompress the compressed package, and store the decompressed package in a disk other than the C drive to obtain the following:

Copy the path address of apache-maven-3.3.9 in the path address: D:\apache-maven-3.3.9

2.2 MAVEN environment variable configuration

Note: Before configuring the Maven environment variable, you must ensure that there is no problem with the jdk configuration

Click the win key - enter: view advanced system settings - click: environment variables - configure in the following system variables

MAVEN_HOME:

D:\apache-maven-3.3.9

path:

%MAVEN_HOME%\bin

Open the windows terminal and enter:

mvn -v    // 查看maven版本号

若出现这几段字母代表Maven安装完成,接下来要进行Maven的其他配置

3. MAVEN的配置

3.1 理解MAVEN仓库

Maven会自动为我们添加相应的jar包,而这个jar包会先在本地仓库中查找,如果本地仓库中不到,则会去中央仓库(非本地)中进行下载。

3.2配置本地仓库

  1. 打开Windows终端输入

mvn    // 初始化maven

Maven初始化后,会在C盘User文件夹-本地账号文件夹中自动创建.m2文件夹,包含一个空的名为Repository的文件夹,在这里,我们不推荐将本地仓库存放在C盘,所以我们需要将Repository文件夹删除,保留.m2即可。

  1. 在C盘之外的一个盘符中创建一个文件夹用来当作Maven的本地仓库,例如:D:\devtools\Maven-Repository

  1. 进入Maven安装目录,在config文件夹中找到一个settings.xml 配置文件,用记事本或者VSCode打开,在localRepository的注释标签下面添加

<loaclRepository>D:\devtools\Maven-Repository</localRepository>

编辑完 ctrl + s 保存一下,先不用急着关闭该文件。

3.3 配置中央仓库

在配置中央仓库之前,我们需要知道Maven作为项目构建及管理工具是如何管理jar包的。

首先每当我们开发一个项目的时候,需要导入一些项目需要的依赖,也就是相关的jar包,只有导入这些依赖,项目才能正常,高效地运行起来。

当我们需要某一个jar包的时候,我们需要在项目的pom.xml文件中去配置所需要的依赖以进行导入到项目中,则此时,Maven会先去本地仓库中查找是否存在相应的jar包,如果有,配置就能直接用,如果Maven在本地仓库中没有找到,则Maven会去中央仓库中下载相应的jar包存放到本地仓库在进行导入到项目中。

配置中央仓库只需要将下面的一段代码直接复制粘贴到settings.xml文件的<mirrors></mirrors>中即可。

<mirror>
    <id>nexus-aliyun</id>
    <mirrorOf>central</mirrorOf>
    <name>Nexus aliyun</name>
    <url>http://maven.aliyun.com/nexus/content/groups/public</url> 
</mirror>

接下来是很关键的一个步骤:将settings.xml文件复制粘贴到C:\User\用户名\.m2\中。

4. MAVEN的使用

4.1 MAVEN项目结构

首先不得不重复提一下,MAVEN是一个项目构建及管理工具,开发团队几乎不用花多少时间就能够自动完成工程的基础构建配置, MAVEN 使用了一个标准的目录结构在不同开发工具中也能实现项目结构的统一。

4.1.1 MAVEN项目目录展示

ProjectStructure

  • src

  • main

  • java

  • test

  • java

  • pom.xml

4.1.2 项目根目录pom.xml
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <!-- maven模型版本 -- >
        <modelVersion>4.0.0</modelVersion>
        <!-- 项目的groupId(项目组织唯一标识符),一般使用项目域名倒写-->
        <groupId>com.zfl19</groupId>
        <!-- 项目的 artifactId(项目的唯一的标识符,对应项目名),groupId+artifactId 组成完成的项目坐标->
        <artifactId>Maven_Hello</artifactId>
        <!--项目版本-->
        <!-- SNAPSHOT:快照,开发版本 -->
        <!-- RELEASE:释放, 稳定版本 --> 
        <version>0.0.1-SNAPSHOT</version>
        <!--项目名-->
        <name>Hello</name>

        <dependencies>
            <!--导入一个junit包-->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.9</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </project>

4.2 MAVEN命令

  • mvn compile : 编译命令,可以重新编译源代码为字节码文件,如果有jar包没下载完成,这个命令会先把需要的jar包下载完成后再编译

  • mvn clean : 清理命令,会把项目结构中的target文件夹中的字节码文件删除,可以组合 : mvn clean compile

  • mvn test :测试命令会帮我们执行测试代码

  • mvn package : 打包命令,可以把项目打包成一个jar,该命令会先执行clean,test,compile,如果不想执行测试命令可以跳过:mvn package -Dmaven.test.skip=true

  • mvn install : 项目打包后安装到本地仓库

  • mvn source:jar : 生成项目的源码包

  • mvn clean site : 生成文档

4.3 MAVEN管理jar包

如果项目需要导入某个jar包,则需要再pom.xml中添加依赖的坐标

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>3.0.5.RELEASE</version>
    <scope>compile</scope>
</dependency>

切记,这里没必要死记硬背,只需要知道我们要在项目中导入某个依赖要填写什么东西,能理解这几句代码的意思,就行了,基本上要导入某个jar包都是ctrl + c和ctrl + v解决,接下来会有一个方法方便我们使用MAVEN导入jar包。

4.4 如何搜索jar包

点击进入MAVEN中央仓库

如果点进来看到这个画面

点击的单选框就行了。

搜索框输入你想要的jar包,这里以junit为例子,点击第二个就行了

找到自己所需要的版本,点进去就可以看到导入maven项目的jar坐标

5. MAVEN配置jdk版本

在MAVEN安装目录的config文件夹打开settings.xml文件,在<profiles></profiles>加入即可。

<profile>
      <id>jdk-1.8</id>
      <activation>
        <activeByDefault>true</activeByDefault>
        <jdk>1.8</jdk>
      </activation>
      <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
      </properties>
  </profile>

Guess you like

Origin blog.csdn.net/m0_53692627/article/details/128822560