|
| 1 | +# Maven |
| 2 | + |
| 3 | +Maven 是基于**项目对象模型(POM)**,可以通过一小段描述信息来管理项目的构建、报告和文档的软件项目管理工具。 |
| 4 | + |
| 5 | +[Maven 下载](http://maven.apache.org/download.cgi) |
| 6 | + |
| 7 | +[Maven 官网](https://maven.apache.org/) |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +## Maven 目录结构 |
| 12 | + |
| 13 | +```html |
| 14 | +src |
| 15 | + |- main |
| 16 | + |--java |
| 17 | + |-- packages |
| 18 | + |-- resources |
| 19 | + |- test |
| 20 | + |--java |
| 21 | + |-- packages |
| 22 | +``` |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +## Maven 坐标和仓库 |
| 27 | + |
| 28 | +### 坐标 |
| 29 | + |
| 30 | +- groupID |
| 31 | + |
| 32 | + **项目组织**的唯一标识符,实际对应 java 的包的结构,即 main目录里 java 的目录结构。 |
| 33 | + |
| 34 | +- artifactID |
| 35 | + |
| 36 | + **项目**的唯一标识符,实际对应项目的名称,就是项目根目录的名称。 |
| 37 | + |
| 38 | +groupId 和 artifactId 被统称为**坐标**。构件通过坐标作为其唯一标识。 |
| 39 | + |
| 40 | +groupId 一般为 `组织名+公司地址反写+项目名`,比如 `com.southeast.myprojects` |
| 41 | + |
| 42 | +artifactId 一般为 `项目名-模块名`,比如`myprojects-demo` |
| 43 | + |
| 44 | +则相应的 package 为 `com.southeast.myprojects.demo` |
| 45 | + |
| 46 | +### 镜像仓库 |
| 47 | + |
| 48 | +setting.xml 文件中配置: |
| 49 | + |
| 50 | +```html |
| 51 | +<mirror> |
| 52 | + <id>maven.net.cn</id> |
| 53 | + <mirrorOf>central</mirrorOf> |
| 54 | + <name>central mirror in china</name> |
| 55 | + <url>http://maven.aliyun.com/nexus/content/groups/public/</url> |
| 56 | +</mirror> |
| 57 | +``` |
| 58 | + |
| 59 | +### 修改本地仓库位置 |
| 60 | + |
| 61 | +setting.xml 文件中配置: |
| 62 | + |
| 63 | +```html |
| 64 | +<localRepository>F:/Java/apache-maven-3.6.2-bin/repo</localRepository> |
| 65 | +``` |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +## Maven 常用构建命令 |
| 70 | + |
| 71 | +### 1. 查看版本 |
| 72 | + |
| 73 | +```html |
| 74 | +mvn - v |
| 75 | +``` |
| 76 | + |
| 77 | +### 2. 编译 |
| 78 | + |
| 79 | +```html |
| 80 | +mvn compile |
| 81 | +``` |
| 82 | + |
| 83 | +### 3. 测试 |
| 84 | + |
| 85 | +```html |
| 86 | +mvn test |
| 87 | +``` |
| 88 | + |
| 89 | +### 4. 打包 |
| 90 | + |
| 91 | +```html |
| 92 | +mvn package |
| 93 | +``` |
| 94 | + |
| 95 | +### 5. 清除 target |
| 96 | + |
| 97 | +```html |
| 98 | +mvn clean |
| 99 | +``` |
| 100 | + |
| 101 | +### 6. 安装 jar |
| 102 | + |
| 103 | +```html |
| 104 | +mvn install |
| 105 | +``` |
| 106 | + |
| 107 | +### 7. 自动创建目录骨架 |
| 108 | + |
| 109 | +```html |
| 110 | +mvn archetype:generate |
| 111 | +# 按照提示进行选择 |
| 112 | +``` |
| 113 | + |
| 114 | +```html |
| 115 | +mvn archetype:generate -DgroupId=组织名,公司网址反写+项目名 |
| 116 | + -DartifactId=项目名-模块名 |
| 117 | + -Dversion=版本号 |
| 118 | + -Dpackage=代码所存在的包名 |
| 119 | +``` |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | +## pom.xml 解析 |
| 124 | + |
| 125 | +- 指定当前 pom 版本 |
| 126 | + |
| 127 | + ```html |
| 128 | + <modelVersion> |
| 129 | + ``` |
| 130 | + |
| 131 | +- 坐标 |
| 132 | + |
| 133 | + ```html |
| 134 | + <groupId>反写公司网址+项目名</groupId> |
| 135 | + <artifactId>项目名+模块名</artifactId> |
| 136 | + <!-- |
| 137 | + 第一个 0 表示大版本号 |
| 138 | + 第二个 0 表示分支版本号 |
| 139 | + 第三个 0 表示小版本号 |
| 140 | + snapshot 快照 |
| 141 | + alpha 内部测试 |
| 142 | + beta 公测 |
| 143 | + Release 稳定 |
| 144 | + GA 正式发布 |
| 145 | + --> |
| 146 | + <version>0.0.1snapshot</version> |
| 147 | + <!-- |
| 148 | + 默认是 jar |
| 149 | + war |
| 150 | + zip |
| 151 | + pom |
| 152 | + --> |
| 153 | + <packaging>jar</packaging> |
| 154 | + <!-- |
| 155 | + 项目描述名 |
| 156 | + --> |
| 157 | + <name></name> |
| 158 | + <!-- |
| 159 | + 项目地址 |
| 160 | + --> |
| 161 | + <url></url> |
| 162 | + ``` |
| 163 | + |
| 164 | +- 依赖列表 |
| 165 | + |
| 166 | + ```html |
| 167 | + <dependencies> |
| 168 | + <dependency> |
| 169 | + <groupId></groupId> |
| 170 | + <artifactId></artifactId> |
| 171 | + <version></version> |
| 172 | + <type></type> |
| 173 | + <!-- |
| 174 | + 依赖范围: |
| 175 | + compile 默认的范围,编译测试运行都有效 |
| 176 | + provided 编译测试时有效 |
| 177 | + runtime 测试运行时有效 |
| 178 | + test 只在测试时有效 |
| 179 | + system 与本机系统相关联,可移植性差 |
| 180 | + import 导入的范围,只使用在 dependencyManagement 中,表示从其他的 pom 中导入 dependency 的配置 |
| 181 | + --> |
| 182 | + <scope>test</scope> |
| 183 | + <!-- |
| 184 | + 设置依赖是否可选 |
| 185 | + --> |
| 186 | + <optional></optional> |
| 187 | + <!-- |
| 188 | + 排除依赖 |
| 189 | + --> |
| 190 | + <exclusions> |
| 191 | + <exclusion> |
| 192 | + <!-- |
| 193 | + 坐标 |
| 194 | + --> |
| 195 | + </exclusion> |
| 196 | + </exclusions> |
| 197 | + </dependency> |
| 198 | + </dependencies> |
| 199 | + ``` |
| 200 | + |
| 201 | +- 依赖管理 |
| 202 | + |
| 203 | + ```html |
| 204 | + <dependencyManagement> |
| 205 | + <dependencies> |
| 206 | + <dependency></dependency> |
| 207 | + </dependencies> |
| 208 | + </dependencyManagement> |
| 209 | + ``` |
| 210 | + |
| 211 | +- build 标签 |
| 212 | + |
| 213 | + ```html |
| 214 | + <build> |
| 215 | + <!-- |
| 216 | + 插件列表 |
| 217 | + --> |
| 218 | + <plugins> |
| 219 | + <plugin> |
| 220 | + <groupId></groupId> |
| 221 | + <artifactId></artifactId> |
| 222 | + <version></version> |
| 223 | + </plugin> |
| 224 | + </plugins> |
| 225 | + </build> |
| 226 | + ``` |
| 227 | + |
| 228 | +- parent 标签(继承) |
| 229 | + |
| 230 | + ```html |
| 231 | + <parent> |
| 232 | + <!-- |
| 233 | + 坐标 |
| 234 | + --> |
| 235 | + </parent> |
| 236 | + ``` |
| 237 | + |
| 238 | +- modules 标签(聚合) |
| 239 | + |
| 240 | + ```html |
| 241 | + <modules> |
| 242 | + <module></module> |
| 243 | + </modules> |
| 244 | + ``` |
| 245 | + |
| 246 | +## 依赖冲突 |
| 247 | + |
| 248 | +### 1. 短路优先 |
| 249 | + |
| 250 | +```html |
| 251 | +路径1:A -> B -> C -> X(jar) |
| 252 | +路径2:A -> D -> X(jar) |
| 253 | +``` |
| 254 | + |
| 255 | +这里优先选择路径 2。 |
| 256 | + |
| 257 | +### 2. 先声明先优先 |
| 258 | + |
| 259 | +如果路径长度相同,则谁先声明,先解析谁。 |
| 260 | + |
| 261 | +、 |
| 262 | + |
| 263 | + |
| 264 | + |
| 265 | + |
| 266 | + |
0 commit comments