一、pom文件格式
<!-- Maven pom对象模型版本号 -->
<modelVersion></modelVersion>
<!-- 打包方式,web工程打包为war,java工程打包为jar(默认) -->
<packaging></packaging>
<!-- 组织id -->
<groupId></groupId>
<!-- 项目id -->
<artifactId></artifactId>
<!-- 版本号:release(完成版)snapshot(开发版) -->
<version></version>
<!-- 项目名称 -->
<name></name>
<!-- 项目描述 -->
<description></description>
<!-- 项目运行依赖的资源 -->
<dependencies>
<dependencie></dependencie>
</dependencies>
<!-- 项目构建 -->
<build>
<!-- 项目插件 -->
<plugins>
<plugin></plugin>
</plugins>
</build>
二、maven命令
mvn complie 编译源代码
mvn deploy 发布项目(私服)
mvn test 运行测试单元
mvn clean 清除编译结果
mvn package 将项目工程打包成JAR或者其他格式的包
mvn assembly 自定义打包
mvn install 将打包的程序放入仓库
三、mvn创建工程模板
mvn archetype:generate
-DgroupId=com.star
-DartifactId=maven-stu
-DarchetypeArtifactId=maven-archetype-quickstart # 工程模板
-DinteractiveMode=false
四、依赖管理
1、依赖传递
依赖具有传递性。
直接依赖:在当前项目中通过依赖配置建立的依赖关系。
间接依赖:被资源的资源如果依赖其他资源,当前项目间接依赖其他资源。
2、依赖冲突问题
路径优先:当依赖中出现相同的资源时,层级越深,优先级越低,层级越浅,优先级越高。
生命优先:当资源在相同层级被依赖时,配置顺序靠前的覆盖顺序配置靠后的。
特殊优先:当同级配置了相同资源的不同版本,后配置的覆盖先配置的。
3、可选依赖
可选依赖指对外隐藏当前依赖的资源--不透明
<dependency>
<groupId></groupId>
<artifactId></artifactId>
<version></version>
<!-- 隐藏当前依赖的资源 -->
<optional>true</optional>
</dependency>
4、排除依赖
排除依赖指主动断开依赖的资源,并排除的资源无需指定版本。
<dependency>
<groupId></groupId>
<artifactId></artifactId>
<version></version>
<!-- 排除依赖 -->
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclustion>
</exclusions>
</dependency>
5、依赖作用范围
依赖的jar默认情况可以在任何地方使用,可以通过scope标签设定其作用范围。
作用范围:
主程序范围有效
测试程序范围有效
是否参与打包
compile(默认) | 主代码有效 | 测试代码有效 | 打包有效 |
test | 主代码无效 | 测试代码有效 | 打包无效 |
provided | 主代码有效 | 测试代码有效 | 打包无效 |
runtime | 主代码无效 | 测试代码无效 | 打包有效 |
五、maven生命周期与插件
1、生命周期
2、插件
<build>
<!-- 插件 -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</group>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<!-- 插件的执行器 -->
<executions>
<execution>
<!-- 插件的执行类型 -->
<goals>
<goal>jar</goal>
</goals>
<!-- 插件的生命周期 -->
<phase>generate-test-resources</phase>
</execution>
<executions>
</plugin>
</plugins>
</build>
六、多模块聚合
构建用于一次性管理多个maven工程,只保留pom文件。
<!-- 定义工程用于构建管理 -->
<packaging>pom</packaging>
<!-- 管理的工程列表 -->
<modules>
<module>../module1</module>
<module>../module2</module>
</modules>
注:参与聚合操作的模块最终执行顺序与模块间的依赖关系有关,与配置顺序无关。
七、多模块继承
1、构建管理工程
<!-- 声明此处进行依赖管理 -->
<dependencyManagement>
<!-- 项目运行依赖的资源 -->
<dependencies>
<dependencie></dependencie>
</dependencies>
</dependencyManagement>
<!-- 声明此处进行插件管理 -->
<pluginManagement>
<!-- 项目运行使用的插件 -->
<plugins>
<plugin></plugin>
</plugins>
</pluginManagement>
2、构建被管理工程
<!-- 声明父工程 -->
<parent>
<groupId></groupId>
<artifactId></artifactId>
<version></version>
<!-- 父工程相对路径 -->
<relativePath>../ssm/pom.xml</relativePath>
</parent>
注:子工程应该与父工程的组织id、版本号相同,故不建议写。
注:如果父工程中存在版本号,子工程的dependencie和plugin无需指定版本号
3、可以继承的资源
4、继承和聚合对比:
作用:
聚合用于快速构建项目。
继承用于快速配置。
相同点:
聚合与继承的pom.xml文件打包方式均为pom,可以将两种关系制作到同一个pom文件中。
聚合与继承均属于设计性模块,并无实际的模块内容。
不同点:
集合实在当前模块中配置关系,聚合可以感知参与聚合的模块有哪些。
聚合时在子模块中配置关系,父模块无法感知那些子模块继承了自己。
八、属性
1、定义属性
<properties>
<spring.version>5.1.9.REPLEASE</spring.version>
</properties>
注:spring.version 属性名 5.1.9.REPLEASE 属性值
2、使用属性
<dependency>
<groupId></groupId>
<artifactId></artifactId>
<version>${spring.version}</version>
</dependency>
3、属性类别
①自定义属性
properties中定义的属性
②内置属性
${basedir}
${version}
③Setting属性
使用Maben配置文件setting.xml中的标签属性
${settings.localRepository}
④Java系统属性
${user.home}
⑤环境变量属性
九、配置资源文件
<!-- 指定资源文件路径 -->
<resources>
<resource>
<directory>ssm_dao/src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
十、多环境配置
1、创建多环境
<!-- 创建多环境(开发环境和测试环境直接切换) -->
<profiles>
<profile>
<!-- 环境名称 -->
<id>pro_env</id>
<!-- 环境中使用的属性值 -->
<properties>
<jdbc.url>jdbc:mysql://172.0.0.1:3306/ssm_db</jdbc.url>
</properties>
<!-- 设置默认启动 -->
<activation>
<activeByDefault>true</activeByDefault>
</activetion>
</profile>
<profile>
<id>dev_env</id>
<properties>
<jdbc.url>jdbc:mysql://172.0.0.2:3306/ssm_db</jdbc.url>
</properties>
</profile>
</profiles>
2、使用指定环境
mvn install -P 环境名称
十一、跳过测试环节
1、点击取消test
2、mvn install -D shipTests
3、通过配置跳过
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<!-- 跳过测试 -->
<skipTests>true</skipTests>
<!-- 包含指定的测试用例 -->
<includes>
<incude>**/User*Test.java</inlude>
</includes>
<!-- 排除指定的测试用例 -->
<excludes>
<exclude>**、User*TestCase.java</exclude>
</excludes>
</configuration>
</plugin>
注:笔记整理于B站黑马Maven课程,致谢!