maven 安装
- 首先现在maven安装包,安装到某个目录
/Library/apache-maven-3.5.3
- 打开
~/.bash_profile
文件,配置环境变量
export M2_HOME=/Library/apache-maven-3.5.3
export PATH=$PATH:$M2_HOME/bin
- 执行mvn -v
maven项目骨架
- src
- main
-java
- test
-java
maven生命周期
maven生命周期归纳为 clean, default, site。
- clean 真正构建之前的一些清理工作
- default 构建的核心部分,compile,test,install等
- site 生成站点报告,发布等
生命周期(lifecycle)由多个阶段(phase)组成,每个阶段(phase)会挂接一个到多个goal
,goal
是maven定义的最小任务单元
生命周期 | 阶段 | goal |
---|---|---|
clean | clean | maven-clean-plugin:clean |
default | process-resources | maven-resources-plugin:resources |
default | compile | maven-compiler-plugin:compile |
default | test | maven-surefire-plugin:test |
liftecycle VS phases VS goals
首先我们要清楚 liftecycle由phases组成,phases由plugin的goals
liftecycle可以认为最佳实践的一种抽象。
pom.xml
最小的 pom
一个pom文件最少包含以下几个节点
- project
- modelVersion
- groupId
- artifactId
- version
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wuyue</groupId>
<artifactId>mamba</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
依赖配置
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
parent
当多个模块有些共同的依赖时候,可以使用parent
定义parent模块
<groupId>com.wuyue</groupId>
<artifactId>mamba</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
使用时候
<parent>
<groupId>com.wuyue</groupId>
<artifactId>mamba</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<!-- 继承自父的pom文件 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
maven 仓库
maven 首先会寻找本地仓库,如果没有找到则会去中心仓库寻找包