一、常用构建命令
mvn compile 编译
test 测试
package 打包
clean 删除target
install 安装jar包到本地仓库
二、自动创建目录骨架
1.groupId=组织名, 公司网址的反写+项目名
artifactId=项目名-模块名
version=版本号
package=代码所存在的包
三、Maven中的坐标与仓库
"构件" 通过 "坐标" 作为其唯一的标识
"仓库":
本地仓库和远程仓库
1.本地仓库是指存在于我们本机的仓库,在我们加入依赖时候,首先会跑到我们的本地仓库去找,如果找不到则会跑到远程仓库中去找。默认情况下,Maven的本地仓库默认为系统用户的 .m2 目录文件夹
2.远程仓库是指其他服务器上的仓库,包括全球中央仓库,公司内部的私服,又或者其他公司提供的公共库
"镜像仓库"
如果仓库X可以提供仓库Y存储的所有内容,那么就可以认为X是Y的一个镜像。换句话说,任何一个可以从仓库Y获得的构件,都胡够从它的镜像中获取。
"更改仓库位置"在conf中的settings.xml中的settings标签中设置 <localRepository>标签进行更改
四、Maven的生命周期和插件
完整的项目构建过程包括:
清理, 编译,测试, 打包, 集成测试, 验证, 部署
Maven生命周期:
clean 清理项目
default 构建项目(最核心)
site 生成项目站点
五、 pom.xml用于管理项目依赖和构建过程
<!-- 基本设置 -->
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<packaging>...</packaging> 打包方式 默认是jar
<dependencies>...</dependencies>
<parent>...</parent>
<dependencyManagement>...</dependencyManagement>
<modules>...</modules>
<properties>...</properties> <!-- 构建过程的设置 -->
<build>...</build>
<reporting>...</reporting> <!-- 项目信息设置 -->
<name>...</name> 项目的描述名
<description>...</description> 项目描述
<url>...</url> 项目的地址
<inceptionYear>...</inceptionYear>
<licenses>...</licenses> 许可证信息
<organization>...</organization>
<developers>...</developers> 开发人员信息
<contributors>...</contributors> <!-- 环境设置 -->
<issueManagement>...</issueManagement>
<ciManagement>...</ciManagement>
<mailingLists>...</mailingLists>
<scm>...</scm>
<prerequisites>...</prerequisites>
<repositories>...</repositories>
<pluginRepositories>...</pluginRepositories>
<distributionManagement>...</distributionManagement>
<profiles>...</profiles> pom.xml常用元素
modelVersion 指定了当前pom版本
groupId 反写的公司网址+项目名
artifactId 项目名+模块名
version 版本号 0.0.1snapshot快照
scope 依赖范围
optional 设置依赖是否可选
dependencyManagement 依赖管理 不会被运行<!-- 用于继承父模块的依赖-->
<parent></parent> <!--定义多个模块 一起进行编译-->
<moduals>
<modual></modual>
……
</moduals> <plugins>
<plugin>插件定位 </plugin>
……
</plugins>
依赖范围
三种classpath
scope
compile:默认,编译、测试、运行都有效
provided:编译、测试有效
runtime:测试、运行时有效
test:测试时有效
system:编译、测试有效;与本机系统相关联,可移植性差
import:只应用在<dependencyManagement>中,表示从其他pom中继承来的依赖
依赖传递
传递依赖:a依赖b,b依赖c,则a间接依赖c,c会出现在a的依赖目录里。
排除依赖:exclution
依赖冲突
1. 短路优先
2. 先声明先优先
聚合和继承
聚合将多个项目整合到一个项目中,在这个项目中声明依赖就可以免去在其他项目中的声明过程
六、plugin与dependency引入的区别
dependency引入的东西
作用:代码编译/运行时所需要的东西
打包:项目打包后这些东西基本都在(一般都在)。
例如:JSON工具包GSON(com.google.code.gson),不仅开发时要用,项目运行时也要用,就需要打包进项目中;
plugin引入的东西
作用:插件,作为开发/编译/打包时的一种辅助工具
打包:一般不会打包进项目中。
例如:使用 maven-source-plugin 插件将API包的源码一起打包,方便发布至Maven仓库,而这个插件不会在打包后的项目中出现。