此篇将介绍Maven的3个重要的入门知识:Maven坐标、Maven运行、Maven镜像配置
一、Maven坐标认识
groupId: 是项目或组织的唯一标识,用点号去标注本仓库的架构,如下图所示:
artifactId: 项目名称,是groupId路径下的项目文件名,与groupId结合,构成库的唯一标识
version: 标识版本号
packaging: 打包类型(产出类型:pom, jar, maven-plugin, ejb, war, ear, rar, par)
二、运行Maven
运行方法有多种,以下总结3种方法,个人倾向使用第一种命令方式,快捷方便。
- 命令行窗口输入命令运行
例:执行package不执行test:
mvn clean package -Dmaven.test.skip=true
-
通过环境菜单Liftcycle下的命令,双击运行,或右键去运行 (也可以多选几个命令后,将命令集存储为你命名的命令)
-
在右上角的运行按钮,下拉菜单下选择“Edit Configurations …”,弹出“Run/Debug Configurations”对话框。
在Defaults菜单中选中“Maven”点击“+”按钮,添加Maven,得到如下配置弹窗。输入命令行,点击“OK”即可。你可以在Run菜单下找到你的这个配置命令。
三、Maven设置
由于Maven仓库是海外的,下载极慢,一般都会使用国内阿里的Maven仓库:
配置步骤:
- 找到Settings弹窗,Maven选项卡:
- 将Maven安装路径下的setting.xml文件拷贝到.m2文件夹下(如果已存在则不需要拷贝啦)。
- 编辑setting.xml文件:
1). 放开localRepository节点注释,设置为本地仓库,这样你的程序jar包就会存放在你设置的路径下
<localRepository>D:\...\repository</localRepository>
2). 镜像配置阿里云的,这样下载插件下载jar依赖就很快啦
<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>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
</mirrors>
阿里云仓库查询地址:
http://maven.aliyun.com/mvn/search
拿artifactId去查询时,会得到很多仓库项,找到对应项双击后复制出现在右侧的maven依赖代码。
这里解释一下这些仓库项的作用或含义:
- .pom只是对应的pom配置文件,
- -sources.jar是对应代码的注解等信息文件,
- 而真正要的依赖项是纯.jar项。