根标签下的基本配置信息
<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">
<!-- 指定了当前pom的版本 -->
<modelVersion>4.0.0</modelVersion>
<!-- 主项目标识,包名,定义当前pom属于哪个实际的项目 -->
<groupId>com.lh.maventest</groupId>
<!-- 模块标识,项目名,项目 -->
<artifactId>maventest</artifactId>
<!-- 版本号 大版本号.分支版本号.小版本号 snapshot 快照 alpha 内测 beta 公测 release 稳定 ga 正式 -->
<version>0.0.1-SNAPSHOT</version>
<!-- 默认jar;还有:war zip pom ejb rar par maven-plugin -->
<packaging>jar</packaging>
<!-- 项目描述名,生成文档时候会用到 -->
<name>maventest</name>
<!-- 项目地址 -->
<url>http://maven.apache.org</url>
<!-- 项目描述 -->
<description>这是项目描述</description>
<!-- 开发人员信息 -->
<developers>
<developer>
test
</developer>
</developers>
<!-- 许可证信息 -->
<licenses>
<license>
用到的开源框架许可证信息
</license>
</licenses>
<!-- 组织信息 -->
<organization>这是一个组织信息</organization>
<!-- 统一管理的参数都写在其中,调用方法:${别名} -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<test>xxx</test>
</properties>
parent标签
parent 标签下是子项目引用的父项中的内容,一般用于多个子项目共同使用到的资源,统一放在父项目中,所有子项目都继承这个父项目。
<!-- 这个父类只有依赖库,所有的项目都可以依赖它 -->
<parent>
<groupId>com.test.maventextends</groupId>
<artifactId>mavenextends</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
依赖列表dependencies
依赖项dependency
基本结构
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<!-- 版本号 -->
<version>3.8.1</version>
<!-- 依赖范围 -->
<!--1,编译(compile)。 编译、运行、测试。默认。-->
<!--2,提供(provided)。编译、测试。-->
<!--3,运行时(runtime)。测试、运行。-->
<!--4,测试(test)。测试。-->
<!--5,系统(system)。与本机系统相关联,可移植性差。-->
<!--6,导入(import)。从其他pom中继承过来的依赖,version >= Maven 2.0.9。-->
<scope>test</scope>
<!--设置依赖是否可选。
false,默认;子项目默认继承false;
true,要重写 -->
<type>false</type>
<!-- 假如你的Project A的某个依赖D设置了true,
当别人通过pom依赖Project A的时候,D不会被传递依赖进来 -->
<optional>true</optional>
<!-- 排除依赖传递列表;a依赖b,b依赖c;a对于c来说就是传递依赖。这个标签可以排除这种传递依赖 -->
<exclusions>
<exclusion>
<!-- 解除依赖的标识 -->
<groupId>xxxxxx</groupId>
<artifactId>xxxxxx</artifactId>
<version>0.0.0-xxx</version>
</exclusion>
</exclusions>
</dependency>
</dependencies>
关于依赖冲突
1,短路优先
例如:
A > B > C > D
A > B > D
这种情况下,A就会优先依赖于B项目下的D
2,位置优先
两个 <dependency>
标签,在pom文件的代码中,谁写在谁前面,就依赖谁其中的冲突依赖项
依赖管理dependencyManagement
<!-- 依赖管理 -->
<dependencyManagement>
<!-- 依赖列表,不会被引用到实际的依赖中。定义在父模块中,用于子模块继承 -->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
引用方法
<project>
<parent>
<groupId>xxx</groupId>
<artifactId>xxx</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>XXX</groupId>
<artifactId>XXX</artifactId>
</dependency>
</dependencies>
</project>
插件plugins
<build>
<!-- 插件 -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<!-- 将源码打包插件配置到生命周期的某个步骤下 -->
<executions>
<execution>
<!-- maven主要运行命令-->
<!-- compile 编译-->
<!-- test 测试-->
<!-- package 打包-->
<!-- install 安装jar包到本地文件-->
<!-- clean 清除maven生成的target文件-->
<!-- deploy 部署-->
<!-- 在打包(package)成功后运行run-->
<phase>package</phase>
<goals>
<!--运行标识-->
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
指定多个模块一起编译
<modules>
<module>../xxx1</module>
<module>../xxx2</module>
<module>../xxx3</module>
</modules>
Maven的生命周期
Validate
generate-sources
process-sources
generate-resources
process-resources 复制并处理资源文件,至目标目录,准备打包。
compile 编译项目的源代码
process-classes
generate-test-sources
process-test-sources
generate-test-resources
process-test-resources 复制并处理资源文件,至目标测试目录。
test-compile 编译测试源代码。
process-test-classes
test 使用合适的单元测试框架运行测试。这些测试代码不会被打包或部署。
prepare-package
package 接受编译好的代码,打包成可发布的格式,如 JAR 。
pre-integration-test
integration-test
post-integration-test
verify
install 将包安装至本地仓库,以让其它项目依赖。
deploy 将最终的包复制到远程的仓库,以让其它开发人员与项目共享。