Maven教程(一) - Maven安装

本系列教程将在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包的流程图如下:

仓库中有Jar包
仓库中没有Jar包,继续向上请求Jar包
仓库中没有Jar包,继续向上请求Jar包
请求Jar包
仓库中有Jar包
仓库中有Jar包
请求Jar包
仓库中有Jar包
中央仓库
镜像仓库
镜像仓库
本地仓库
项目

从图中我们可以看到,Maven在查找Jar包时,会按照以下流程查找:

  1. 本地仓库中查找,如果找到了,则依赖到项目中,否则进入第二步
  2. 镜像仓库中查找,如果找到了,将Jar包下载到本地仓库,并且重复第一步,否则进入第三步
  3. 镜像仓库上级镜像仓库中查找,如果镜像仓库没有上级镜像仓库,则直接进入第四步,否则在上级镜像仓库中查找Jar包,如果找到了,则将Jar包下载到镜像仓库中,并且重复第二步,否则进入第四步.
  4. 中央仓库中查找,如果找到了,则下载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镜像仓库官网,如下图:
阿里云Maven镜像仓库官网
在仓库列表中找到Repositorypublic的仓库.然后复制后面的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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值