Maven
一、maven简介
目标
- 能够使用Maven进行项目的管理
- 能够完成Mybatis代理方式查询数据
- 能够理解Mybatis核心配置文件的配置
1.maven简介
提供了一套标准化的项目结构
提供了一套标准化的构建流程(编译,测试,打包,发布…)
提供了一套依赖管理机制
①标准化的项目结构:
能够使我们在不同集成开发软件中开发同一个项目,使项目整合更方便。
②标准化的构建流程
我们开发项目,要经过编译,测试,打包,发布…等固定流程,我们每次都手动会很麻烦,maven就提供了这些命令,直接帮我们完成这些操作。
③依赖管理机制
(依赖:就是下面需要的第三方资源:比如mysql的jar包,数 据库池德鲁伊的jar包)
传递性:引用别人项目,也会把别人的项目引用的依赖拿到
A–>B–>C,这种关系A也会拿到C的所有引用。
坐标性:就是在引用别人项目的时候,不用再导入jar包,而
是只需要写一个坐标
代码演示:
<!-- 定义依赖项 -->
<dependencies>
<!-- 单个依赖 -->
<dependency>
<!-- 依赖项的 groupId (放公司域名反写)-->
<groupId>com.alibaba</groupId>
<!-- 依赖项的 artifactId (放项目名字)-->
<artifactId>druid</artifactId>
<!-- 依赖项的版本 -->
<version>1.2.6</version>
<!-- 依赖项的作用范围 -->
<scope>test</scope>
</dependency>
</dependencies>
</project>
2.maven模型
①项目对象模型:
就是将自己抽象成一个对象,有自己专属的坐标
例如以下内容:
<groupId>org.example</groupId>//默认的org公司域名反写
<artifactId>maven2</artifactId>//项目名字
<version>1.0-SNAPSHOT</version>//默认版本号
②依赖管理模型:
就是用坐标描述当前依赖的第三方jar包
例如以下内容:
<!-- 依赖项的 groupId (放公司域名反写)-->
<groupId>com.alibaba</groupId>
<!-- 依赖项的 artifactId (放项目名字)-->
<artifactId>druid</artifactId>
<!-- 依赖项的版本 -->
<version>1.2.6</version>
③插件(生命周期):
我们要执行编译、测试、打包…等操作时,maven就提供了一个打包插件,简化我们这些操作。
例如:mvn test 、mvn clean
3.maven仓库
①本地仓库:
本地计算机上的目录,本地仓库一般自己选好位置,在配置文件中去配置好下载的jar包们安放的位置(D:\JAVA\apache-maven-3.6.1\mvn_repository)
②中央仓库:
由Maven团队维护的全球唯一的仓库,地址: https://repo1.maven.org/maven2/
③远程仓库:
公司搭建的,私服仓库,用来放一些公司购买的拥有版权的jar包,或者公司内部写的jar包
④坐标寻找:
坐标寻找顺序:本地仓库 --> 远程仓库 --> 中央仓库
4.maven安装配置
①下载解压:
在官网下载好,解压后必须解压到纯英文的路径下
比如:D:\JAVA\
②结构内容:
--bin目录:存放的是可执行命令。例如:mvn
--conf目录:存放Maven的配置文件。例如:settings.xml配置文件
--lib目录:存放Maven的jar包。maven也是使用java开发的,所以他也有依赖的jar包
③配置环境:
--打开系统的环境变量界面
--新建一个环境变量:MAVEN_HOME
--里面放上:maven存放的地址。例如:D:\JAVA\apache-maven-3.6.1
--再在path路径中配置%MAVEN_HOME%\bin
--打开cmd命令窗口
--输入mvn -version,出现版本号了则配置成功
④配置文件:
在conf目录,中的setting.xml文件中进行下列修改
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
1.在紧挨着上面注释代码处,往下面添加下列代码(请不要把该文字加进去)
!!!这个代码是修改jar包存放在那个目录位置
<localRepository>D:\JAVA\apache-maven-3.6.1\mvn_repository</localRepository>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
2.在紧挨着上面注释代码处,往下面添加下列代码(请不要把该文字加进去)
!!!这个代码是修改镜像站点
通过配置镜像,Maven可以从指定的镜像站点获取依赖项和插件等资源,而不是直接从原始的中央仓库(central repository)获取。使用镜像可以加快构建过程中的下载速度,并减轻中央仓库的负载。
因为外国服务器我们连接不够稳定,网速下载东西不行
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<!-- profile
| Specifies a set of introductions to the build process, to be activated using one or more of the
| mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
| or the command line, profiles have to have an ID that is unique.
|
| An encouraged best practice for profile identification is to use a consistent naming convention
| for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
| This will make it more intuitive to understand what the set of introduced profiles is attempting
| to accomplish, particularly when you only have a list of profile id's for debug.
|
| This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
<profile>
<id>jdk-1.4</id>
<activation>
<jdk>1.4</jdk>
</activation>
<repositories>
<repository>
<id>jdk14</id>
<name>Repository for JDK 1.4 builds</name>
<url>http://www.myhost.com/maven/jdk14</url>
<layout>default</layout>
<snapshotPolicy>always</snapshotPolicy>
</repository>
</repositories>
</profile>
-->
3.在紧挨着上面注释代码处,往下面添加下列代码(请不要把该文字加进去)
!!!配置maven构建流程时用的jdk版本(把里面所有的17改成自己版本的)
<profile>
<id>jdk-17</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>17</jdk>
</activation>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.compilerVersion>17</maven.compiler.compilerVersion>
</properties>
</profile>
5.maven基本使用
①常用命令:
--mvn compile:编译
--mvn clean:清除
--mvn test:测试
--mvn package:打包
--mvn install :安装
需在当前项目的位置,去cmd窗口运行以上命令
②打包完成:
完成打包后,会将maven项目导入本地仓库中,成为一个maven的项目模型,拥有自己的坐标,可以被引用。
6.maven生命周期:
①三个步骤:
maven对项目构建的执行流程就是他的生命周期。
总共被分为三套:clean 、default 、 site
clean:清理工作
default:核心工作,如:编译、测试、打包、安装
site:发布,这套无用,因为发布站点通常使用其他
②核心周期:
--为什么是核心?
因为我们用maven用到的主要是defalut周期,他的里面有很多命令
但是我们只经常用defalut中常用的命令,如:compile、test。
--default全过程
二、IDEA使用Maven
1.IDEA创建Maven环境
①选择版本:
图片有点小问题,这个图片第三步:要勾选好override才能修改内容
②运行环境:
选择好运行时的jdk环境,和解码的字符集
③编码版本:
jdk17版本可以选择15或者以上的编码版本号,但是2020只有15,所以我这里选择15
④全局配置:
以上操作可以在all setting中设置,这样每次新建的项目也能沿用这次的设置内容
2.IDEA创建Maven项目
①创建空项目:
创建一个空项目才行
②创建maven项目:
完成最后一张图片的运行,就代表创建成功了
3.导入maven项目
①添加项目:
4.扩展maven工具
①下载MavenHelper:
file–>setting–>图片步骤