本系列教程将在windows,Eclipse环境下进行讲解
安装
进入官网进行下载安装包,如下图:
从图中可以看到标记的俩个地方,下载源和可选文件列表
- 下载源: 决定从哪里下载文件,这个选项决定了你的下载速度,国内用户建议选择
https://mirrors.ocf.berkeley.edu/apache/
- 可选文件列表: 分为俩种文件后缀,分别为
.zip
和.tar.gz
俩种,请按照自己的需求下载,里面的文件是一模一样的,建议windows下载.zip
,其他平台下载.tar.gz
- 二进制安装包: 以
-bin
结尾(不含文件后缀) - 源代码:以
-src
结尾(不含文件后缀)
- 二进制安装包: 以
下载下来以后,解压
安装包到你想安装的位置(安装路径最好不要包含中文
和空格),解压后的目录如下:
其中大家需要知道的目录有三个:
- bin: Maven的
可执行文件
,存放于该目录 - conf: Maven的
配置文件
,存放于该目录 - lib: Maven的
依赖包
,存放于该目录
Maven Jar包依赖流程
本节将讲解Maven如何将Jar包依赖到项目的流程
Maven依赖Jar包的流程图如下:
从图中我们可以看到,Maven在查找Jar包时,会按照以下流程查找:
- 到
本地仓库
中查找,如果找到了,则依赖到项目中,否则进入第二步
- 到
镜像仓库
中查找,如果找到了,将Jar包下载到本地仓库
,并且重复第一步
,否则进入第三步
- 到
镜像仓库
的上级镜像仓库
中查找,如果镜像仓库
没有上级镜像仓库
,则直接进入第四步
,否则在上级镜像仓库
中查找Jar包,如果找到了,则将Jar包下载到镜像仓库
中,并且重复第二步
,否则进入第四步
. - 到
中央仓库
中查找,如果找到了,则下载Jar包到上级镜像仓库
,如果没有上级镜像仓库
,则直接下载镜像仓库
中,并且重复第三步
(上级镜像仓库
存在)或第二步
(上级镜像仓库
不存在).如果没有找到Jar包,则说明Jar包不存在.
名词解析
仓库
在Maven
中仓库
是用来集中存放Jar包的地方,它可以是文件夹,也可以是服务器.
仓库分为本地仓库
,镜像(代理)仓库
和中央仓库
中央仓库
中央仓库
用于存储所有的Jar包.使用Maven
时,所有的Jar包(私有Jar包除外)都是从中央仓库
下载,但是由于中央仓库
服务器是在国外的,所有下载速度通常都是比较慢的(除非你有梯子😂)
镜像(代理)仓库
刚刚我们了解了中央仓库
的作用,从中我们也了解到中央仓库
的下载速度是比较慢的.为了解决这个问题,就有了镜像(代理)仓库
.镜像(代理)仓库
的原理就是定时从中央仓库
同步所有的Jar包,基于Jar包依赖流程得知,我们大部分Jar包都可以在镜像(代理)仓库
中找到,这样的话我们也不需要访问中央仓库
了,从而加快Jar包的下载速度.至于镜像(代理)仓库
的配置,我们将在下文讲解.
本地仓库
本地仓库的作用是缓存既往使用过的Jar包,避免每次使用都需要在线下载的问题,从而提高依赖效率.简而言之就是,Jar包只要被使用过一次,再次使用将不需要在线下载
配置
本节讲解的配置都是在
conf/settings.xml
文件中进行配置,打开配置文件时请使用专业的文本编辑器打开(如:Notepad++,EditPlus,Atom,Sublime Text 等等),请勿使用windows自带的编辑器打开(如记事本,写字板,word等)
本地仓库配置
此配置是可选的,如果不进行此项配置,本地仓库的默认路径 为:
${user.home}/.m2/repository
.由于${user.home}
通常位于系统盘(C盘)
,所以建议大家配置一下本地仓库,并且路径位于系统盘
之外.
打开配置文件找到如下注释代码:
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
然后在这段注释代码下方加入以下代码:
仓库路径为你本地磁盘的任意文件夹,路径中最好
不要
包含中文
和空格
,且在系统盘
之外
<localRepository>仓库路径</localRepository>
配置后的完整代码如下:
此处我以仓库放在D盘为例
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<localRepository>D://repository</localRepository>
**
注意:
**在配置时,请不要将<localRepository></localRepository>
标签写到<!-- -->
注释中了,否则配置是不生效的.并且不要漏写结束标签(</localRepository>)
镜像(代理)仓库配置
本节讲解如何配置镜像仓库,以阿里云Maven镜像仓库为例
获得镜像URL
进入阿里云Maven镜像仓库官网,如下图:
在仓库列表中找到Repository
为public
的仓库.然后复制后面的Path
路径.
或者在此复制:
https://maven.aliyun.com/repository/public
打开conf/settings.xml
配置文件,找到<mirrors></mirrors>
标签,如下所示:
<!-- mirrors
| This is a list of mirrors to be used in downloading artifacts from remote repositories.
|
| It works like this: a POM may declare a repository to use in resolving certain artifacts.
| However, this repository may have problems with heavy traffic at times, so people have mirrored
| it to several places.
|
| That repository definition will have a unique id, so we can create a mirror reference for that
| repository, to be used as an alternate download site. The mirror site will be the preferred
| server for that repository.
|-->
<mirrors>
<!-- 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>
-->
</mirrors>
然后在<mirrors></mirrors>
中添加如下代码:
<!-- 配置阿里云 -->
<mirror>
<!-- 为你配置的镜像取一个ID -->
<id>aliyun</id>
<!-- 为你配置的镜像取一个Name -->
<name>aliyun</name>
<!-- 设置将中央仓库的请求转发至阿里云镜像 -->
<mirrorOf>central</mirrorOf>
<!-- 将复制的Path粘贴到此处 -->
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
配置完成后的代码如下:
<!-- mirrors
| This is a list of mirrors to be used in downloading artifacts from remote repositories.
|
| It works like this: a POM may declare a repository to use in resolving certain artifacts.
| However, this repository may have problems with heavy traffic at times, so people have mirrored
| it to several places.
|
| That repository definition will have a unique id, so we can create a mirror reference for that
| repository, to be used as an alternate download site. The mirror site will be the preferred
| server for that repository.
|-->
<mirrors>
<!-- 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>
-->
<!-- 配置阿里云 -->
<mirror>
<!-- 为你配置的镜像取一个ID -->
<id>aliyun</id>
<!-- 为你配置的镜像取一个Name -->
<name>aliyun</name>
<!-- 设置将中央仓库的请求转发至阿里云镜像 -->
<mirrorOf>central</mirrorOf>
<!-- 将复制的Path粘贴到此处 -->
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
</mirrors>
JDK版本配置
由于
Maven
默认的JDK
版本为1.5
,而我们通常开发版本都是大于1.5
的.每次在开发时我们都是需要重新设置JDK版本的.
工程下JDK版本配置
本节建议新手略过,学习后续章节后在回来看
打开pom.xml
文件,找到如下标签代码(如没有请,手动补全代码):
<build>
<plugins>
</plugins>
</build>
在<plugins></plugins>
标签中添加编译插件,我们在此设置为1.7
.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!-- 源代码版本 -->
<source>1.7</source>
<!-- 编译版本 -->
<target>1.7</target>
</configuration>
</plugin>
修改后的完整代码如下:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!-- 源代码版本 -->
<source>1.7</source>
<!-- 编译版本 -->
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
修改默认JDK版本
打开conf/settings.xml
配置文件,找到<profiles></profiles>
标签,在标签中添加以下代码:
<profile>
<!-- 为你的配置取一个ID -->
<id>development</id>
<!--activation用来指定激活方式,可以根据jdk环境,环境变量,文件的存在或缺失-->
<activation>
<!--配置默认激活-->
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!-- 源代码版本 -->
<maven.compiler.source>1.7</maven.compiler.source>
<!-- 编译版本 -->
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>
</properties>
</profile>
配置后的完整代码如下:
<profiles>
<profile>
<!-- 为你的配置取一个ID -->
<id>development</id>
<!--activation用来指定激活方式,可以根据jdk环境,环境变量,文件的存在或缺失-->
<activation>
<!--配置默认激活-->
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!-- 源代码版本 -->
<maven.compiler.source>1.7</maven.compiler.source>
<!-- 编译版本 -->
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>