框架学习Maven

声明:本文来源于黑马程序员PDF讲义

做为一名Java开发工程师,后端 Web开发技术是我们学习的重点,后端Web开发技术的学习,我们会先学习Java项目的构建工具:Maven

初识Maven

Maven是Apache旗下的一个开源项目,是一款用于管理和构建java项目的工具。

使用Maven能够做什么呢?

1. 依赖管理 2. 统一项目结构 3. 项目构建

当使用maven进行项目依赖(jar包)管理,则很方便的可以解决这个问题。 我们只需要在maven项目 的pom.xml文件中,添加一段如下图所示的配置即可实现。

XML 复制代码
<!-- 依赖声明 -->
    <dependencyManagement>
        <dependencies>

            <!-- SpringBoot的依赖配置-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.5.15</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!-- 阿里数据库连接池 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>${druid.version}</version>
            </dependency>

            <!-- 解析客户端操作系统、浏览器等 -->
            <dependency>
                <groupId>eu.bitwalker</groupId>
                <artifactId>UserAgentUtils</artifactId>
                <version>${bitwalker.version}</version>
            </dependency>

            <!-- pagehelper 分页插件 -->
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper-spring-boot-starter</artifactId>
                <version>${pagehelper.boot.version}</version>
            </dependency>

            <!-- 获取系统信息 -->
            <dependency>
                <groupId>com.github.oshi</groupId>
                <artifactId>oshi-core</artifactId>
                <version>${oshi.version}</version>
            </dependency>

            <!-- Swagger3依赖 -->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-boot-starter</artifactId>
                <version>${swagger.version}</version>
                <exclusions>
                    <exclusion>
                        <groupId>io.swagger</groupId>
                        <artifactId>swagger-models</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>

            <!-- io常用工具类 -->
            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
                <version>${commons.io.version}</version>
            </dependency>

            <!-- excel工具 -->
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>${poi.version}</version>
            </dependency>

            <!-- velocity代码生成使用模板 -->
            <dependency>
                <groupId>org.apache.velocity</groupId>
                <artifactId>velocity-engine-core</artifactId>
                <version>${velocity.version}</version>
            </dependency>

            <!-- collections工具类 -->
            <dependency>
                <groupId>commons-collections</groupId>
                <artifactId>commons-collections</artifactId>
                <version>${commons.collections.version}</version>
            </dependency>

            <!-- 阿里JSON解析器 -->
            <dependency>
                <groupId>com.alibaba.fastjson2</groupId>
                <artifactId>fastjson2</artifactId>
                <version>${fastjson.version}</version>
            </dependency>

            <!-- Token生成与解析-->
            <dependency>
                <groupId>io.jsonwebtoken</groupId>
                <artifactId>jjwt</artifactId>
                <version>${jwt.version}</version>
            </dependency>

            <!-- 验证码 -->
            <dependency>
                <groupId>pro.fessional</groupId>
                <artifactId>kaptcha</artifactId>
                <version>${kaptcha.version}</version>
            </dependency>

            <!-- 定时任务-->
            <dependency>
                <groupId>com.xxxx</groupId>
                <artifactId>xxxx-quartz</artifactId>
                <version>${xxxx.version}</version>
            </dependency>

            <!-- 代码生成-->
            <dependency>
                <groupId>com.xxxx</groupId>
                <artifactId>xxxx-generator</artifactId>
                <version>${xxxx.version}</version>
            </dependency>

            <!-- 核心模块-->
            <dependency>
                <groupId>com.xxxx</groupId>
                <artifactId>xxxx-framework</artifactId>
                <version>${xxxx.version}</version>
            </dependency>

            <!-- 系统模块-->
            <dependency>
                <groupId>com.xxxx</groupId>
                <artifactId>xxxx-system</artifactId>
                <version>${xxxx.version}</version>
            </dependency>

            <!-- 通用工具-->
            <dependency>
                <groupId>com.xxxx</groupId>
                <artifactId>xxxx-common</artifactId>
                <version>${xxxx.version}</version>
            </dependency>

            <!-- xxxxM模块-->
            <dependency>
                <groupId>com.xxxx</groupId>
                <artifactId>xxxx-main</artifactId>
                <version>${jcdm.version}</version>
            </dependency>

            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-pool2</artifactId>
                <version>2.12.0</version>
            </dependency>

        </dependencies>
    </dependencyManagement>

Maven模型

  • 项目对象模型 (Project Object Model)
  • 依赖管理模型(Dependency)
  • 构建生命周期/阶段(Build lifecycle & phases)

1). 构建生命周期/阶段(Build lifecycle & phases)

以上图中紫色框起来的部分,就是用来完成标准化构建流程 。当我们需要编译,Maven提供了一个编译插件供我们使用;当我们需要打包,Maven就提供了一个打包插件供我们使用等。

2). 项目对象模型 (Project Object Model)

以上图中紫色框起来的部分属于项目对象模型,就是将我们自己的项目抽象成一个对象模型,有自己专属的坐标,如下图所示是一个Maven项目:

坐标,就是资源(jar包)的唯一标识,通过坐标可以定位到所需资源(jar包)位置

3). 依赖管理模型(Dependency)

以上图中紫色框起来的部分属于依赖管理模型,是使用坐标来描述当前项目依赖哪些第三方jar包

之前我们项目中需要jar包时,直接就把jar包复制到项目下的lib目录,而现在书写在pom.xml文件中的坐标又是怎么能找到所要的jar包文件的呢?

答案:Maven仓库

仓库:用于存储资源,管理各种jar包

仓库的本质就是一个目录(文件夹),这个目录被用来存储开发中所有依赖(就是jar包)和插件

Maven仓库分为:

  • 本地仓库:自己计算机上的一个目录(用来存储jar包)
  • 中央仓库:由Maven团队维护的全球唯一的。仓库地址:https://repo1.maven.org/maven 2/
  • 远程仓库(私服):一般由公司团队搭建的私有仓库

当项目中使用坐标引入对应依赖jar包后,首先会查找本地仓库中是否有对应的jar包

  • 如果有,则在项目直接引用
  • 如果没有,则去中央仓库中下载对应的jar包到本地仓库

如果还可以搭建远程仓库(私服),将来jar包的查找顺序则变为:本地仓库 --> 远程仓库--> 中央仓库

Maven安装配置步骤:

1. 解压安装 2. 配置仓库 3. 配置Maven环境变量

IDEA集成Maven

POM配置详解

POM (Project Object Model) :指的是项目对象模型,用来描述当前的maven项目。

使用pom.xml文件来实现 pom.xml文件:

XML 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- POM模型版本 -->
<modelVersion>4.0.0</modelVersion>
<!-- 当前项目坐标 -->
<groupId>com.itheima</groupId>
<artifactId>maven_project1</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 打包方式 -->
<packaging>jar</packaging>
</project>

1、pom :用在父级工程或聚合工程中,用来做jar包的版本控制,必须指明这个聚合工程的打包方式为pom

2、jar:工程的默认打包方式,打包成jar用作jar包使用。存放一些其他工程都会使用的类,工具类。我们可以在其他工程的pom文件中去引用它

3、 war :将会打包成war,发布在服务器上,如网站或服务。用户可以通过浏览器直接访问,或者是通过发布服务被别的工程调用

Maven坐标详解

什么是坐标?

  • Maven中的坐标是 资源的唯一标识 , 通过该坐标可以唯一定位资源位置
  • 使用坐标来定义项目或引入项目中需要的依赖

Maven坐标主要组成

  • groupId:定义当前Maven项目隶属组织名称(通常是域名反写,例如:com.itheima)
  • artifactId:定义当前Maven项目名称(通常是模块名称,例如 order-service、goodsservice) version:定义当前项目版本号

如下图就是使用坐标表示一个项目:

依赖配置

依赖:指当前项目运行所需要的jar包。一个项目中可以引入多个依赖:

例如:在当前工程中,我们需要用到logback来记录日志,此时就可以在maven工程的pom.xml文件 中,引入logback的依赖。具体步骤如下:

  1. 在pom.xml中编写标签
  2. 在标签中使用引入坐标
  3. 定义坐标的 groupId、artifactId、version

注意事项:

1. 如果引入的依赖,在本地仓库中不存在,将会连接远程仓库 / 中央仓库,然后下载依赖 (这个过程会比较耗时,耐心等待)

2. 如果不知道依赖的坐标信息,可以到mvn的中央仓库 (https://mvnrepository.com/)中搜索

排除依赖

问题:之前我们讲了依赖具有传递性。那么A依赖B,B依赖C,如果A不想将C依赖进来,是否可以做到?

答案:在maven项目中,我们可以通过排除依赖来实现

什么是排除依赖?

排除依赖:指主动断开依赖的资源。(被排除的资源无需指定版本)

XML 复制代码
<dependency>
    <groupId>com.itheima</groupId>
    <artifactId>maven-projectB</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--排除依赖, 主动断开依赖的资源-->
    <exclusions>
         <exclusion>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
         </exclusion>
    </exclusions>
</dependency>

依赖范围

在项目中导入依赖的jar包后,默认情况下,可以在任何地方使用。

如果希望限制依赖的使用范围,可以通过标签设置其作用范围

作用范围:

  • 主程序范围有效(main文件夹范围内)
  • 测试程序范围有效(test文件夹范围内)
  • 是否参与打包运行(package指令范围内)

如上图所示,给junit依赖通过scope标签指定依赖的作用范围。 那么这个依赖就只能作用在测试环 境,其他环境下不能使用。

生命周期

Maven的生命周期是抽象的,这意味着生命周期本身不做任何实际工作。在Maven的设计中,实际任务 (如源代码编译)都交由插件来完成。

compile:

test:

package:

install:

clean:

相关推荐
dengqingrui1233 小时前
【树形DP】AT_dp_p Independent Set 题解
c++·学习·算法·深度优先·图论·dp
我的心永远是冰冰哒3 小时前
ad.concat()学习
学习
ZZZ_O^O3 小时前
二分查找算法——寻找旋转排序数组中的最小值&点名
数据结构·c++·学习·算法·二叉树
slomay5 小时前
关于对比学习(简单整理
经验分享·深度学习·学习·机器学习
hengzhepa5 小时前
ElasticSearch备考 -- Async search
大数据·学习·elasticsearch·搜索引擎·es
小小洋洋7 小时前
BLE MESH学习1-基于沁恒CH582学习
学习
Ace'8 小时前
每日一题&&学习笔记
笔记·学习
IM_DALLA8 小时前
【Verilog学习日常】—牛客网刷题—Verilog进阶挑战—VL25
学习·fpga开发·verilog学习
丶Darling.8 小时前
LeetCode Hot100 | Day1 | 二叉树:二叉树的直径
数据结构·c++·学习·算法·leetcode·二叉树
z樾10 小时前
Github界面学习
学习