出现背景
传统项目中存在依赖多个模块,如果其中一个模块改动了,那么整个项目就有可能运行不起来,所以一旦有改动就要整个项目重新在服务器上编译测试打包,维护起来极其繁琐,maven的出现就是为了解决这样的问题
- 一图说明Maven是什么
主要功能
统一开发结构
结构图
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>项目组织</groupId>
<artifactId>项目名称</artifactId>
<version>项目版本</version>
<packaging>项目打包方式</packaging>
</project>
依赖管理
方便快捷地管理项目依赖的资源(jar包),避免资源间的版本冲突问题
依赖传递
直接依赖
-
间接依赖
引入的依赖如果依赖其他资源,那么就是当前项目间接依赖那些资源
依赖冲突
路径优先:项目中出现相同依赖时,层级越深,优先级越低
- 声明优先:当前资源在相同层级被依赖时,配置顺序靠前的覆盖配置顺序靠后的
特殊优先:当同级配置了相同资源的不同版本,后配置的覆盖先配置的
阻断依赖
上游对下游中断依赖
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <!--optional设置为true时,依赖该项目的其他项目引入不了该依赖--> <optional>true</optional> </dependency>
下游主动排除上游依赖
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <exclusions> <exclusion> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-core</artifactId> </exclusion> </exclusions> </dependency>
依赖作用范围
通过scope标签设定其作用范围
- scope为compile时,依赖在main中有效,在test中有效,打包时会将依赖的jar包放入其中
- scope为test时,依赖在main中无效,在test中有效,打包时不会将依赖的jar包放入其中
- scope为provided时,依赖在main中有效,在test中有效,打包时不会将依赖的jar包放入其中
scope为runtime时,依赖在main中无效,在test中无效,打包时会将依赖的jar包放入其中
依赖版本统一管理
通常在父工程中指定依赖的jar包版本进行统一管理,子工程指定父工程,然后引入依赖时子工程不用指定依赖的版本
父工程
<?xml version="1.0" encoding="UTF-8"?> <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>pers.luchuan</groupId> <artifactId>parent-project</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>project</module> </modules> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <junit.version>4.12</junit.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> </dependencyManagement> </project>
子工程
<?xml version="1.0" encoding="UTF-8"?> <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"> <parent> <artifactId>project</artifactId> <groupId>pers.luchuan</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>project</artifactId> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> </dependencies> </project>
项目构建
提供标准的、跨平台的自动化项目构建方式
maven构建命令
运行mvn命令需要在有pom文件的目录下进行,mvn的命令运行需要各种插件的支持,如果没有则会到远程仓库中进行下载
编译
mvn compile
编译的效果是生成target目录,并将src下java包中的文件进行编译,并将编译好的字节码文件放入到target目录下的classes目录下
测试
mvn test
测试的效果是在编译的基础上对test目录下的java目录进行编译和测试,并将编译好的文件放入test-classes目录下
打包
mvn package
-
安装到本地仓库
mvn install
-
清理
mvn clean
-
项目构建生命周期
compile->test->install->deploy
maven的本质
maven在构建项目时的每个动作其实都是由插件完成的,所以maven插件的使用才是重点
插件创建工程
在一个空文件中运行插件命令
创建java工程
mvn archetype:generate -DgroupId={} -DartifactId={} -Dversion={} -DarchetypeArtifactid=maven-archetype-quickstart -DinteractiveMode=false
创建web工程
mvn archetype:generate -DgroupId={} -DartifactId={} -Dversion={} -DarchetypeArtifactid=maven-archetype-webapp -DinteractiveMode=false
跳过测试
maven命令方式
mvn package -DskipTests
idea开发工具方式
pom文件配置插件方式
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<!--设置跳过所有测试-->
<!-- <skipTests>true</skipTests>-->
<includes>
<!--设置包含哪些测试, 支持通配符-->
<include>* * /App*.java</include>
</includes>
<excludes>
<!--设置排除哪些测试, 支持通配符-->
<exclude></exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>