一、Maven的概念
1-1 Maven是什么
- Maven是一款自动化构建工具,专注于服务java平台的项目构架和依赖管理
- 依赖管理:jar之间的依赖关系,jar包管理问题统称为依赖管理
- 项目构建:项目构建不同于项目创建,项目构建是过程,项目创建是书简完成的
- 项目构建由7个步骤组成
- 清理 mvn clean
- 编译 mvn compile
- 测试 mvn test
- 报告
- 打包 mvn package
- 安装 mvn install
- 部署
- 项目构建由7个步骤组成
补充:maven底层是用java语言编写的。
1-2 Maven的用途
- 获取jar包
- 使用Maven可以统一在一个地址下载资源jar包
- 添加jar包
- 使用Maven后可以将jar包同意存储在Maven本地仓库,使用坐标的方式将jar包引入仓库
- 处理jar包冲突及依赖问题
- Maven可以自动解决jar包的冲突和依赖问题
二、Maven的使用
2-1 Maven的准备工作
- 下载地址:http://maven.apache.org/
- Maven底层使用java语言编写的,所以需要配置JAVA_HOME环境变量及Path
- 输入【cmd】,进入命令行窗口,输入**【mvn -v】** ,检查Maven环境是否搭建成功
- Maven的路径应该为全英文,不能含有中文
2-2 Maven的基本配置
- Maven配置文件位置:maven根目录/conf/settings.xml
- 设置本地仓库【默认:C:/用户家目录/.m2/repository】
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<localRepository>E:\SG_220106\LocalRepository</localRepository>
- 设置阿里云镜像服务器
<mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
</mirrors>
设置使用JDK版本 profile,我用的jdk17
<profiles>
<!-- profile
| Specifies a set of introductions to the build process, to be activated using one or more of the
| mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
| or the command line, profiles have to have an ID that is unique.
|
| An encouraged best practice for profile identification is to use a consistent naming convention
| for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
| This will make it more intuitive to understand what the set of introduced profiles is attempting
| to accomplish, particularly when you only have a list of profile id's for debug.
|
| This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
<profile>
<id>jdk-1.4</id>
<activation>
<jdk>1.4</jdk>
</activation>
<repositories>
<repository>
<id>jdk14</id>
<name>Repository for JDK 1.4 builds</name>
<url>http://www.myhost.com/maven/jdk14</url>
<layout>default</layout>
<snapshotPolicy>always</snapshotPolicy>
</repository>
</repositories>
</profile>
-->
<!--
| Here is another profile, activated by the system property 'target-env' with a value of 'dev',
| which provides a specific path to the Tomcat instance. To use this, your plugin configuration
| might hypothetically look like:
|
| ...
| <plugin>
| <groupId>org.myco.myplugins</groupId>
| <artifactId>myplugin</artifactId>
|
| <configuration>
| <tomcatLocation>${tomcatPath}</tomcatLocation>
| </configuration>
| </plugin>
| ...
|
| NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
| anything, you could just leave off the <value/> inside the activation-property.
|
<profile>
<id>env-dev</id>
<activation>
<property>
<name>target-env</name>
<value>dev</value>
</property>
</activation>
<properties>
<tomcatPath>/path/to/tomcat/instance</tomcatPath>
</properties>
</profile>
-->
<profile>
<id>jdk-17</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>17</jdk>
</activation>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.compilerVersion>17</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
2-3 将Maven整合到Idea中
步骤:
- File---->Serttings---->Build,Execution,Deployment---->Maven
- Maven_home_directory(maven根目录)---->E:/Java environment/apache-maven-3.6.3
- 点击User setting file的Override,将内容改为setting.xml的本地位置
- 将Local repository的内容更改为本地仓库的位置(即setting.xml设置的)
补充:一般来说,填写User setting file后idea会自动识别Local repository。
注意:每次登入的时候根据idea的不同,可能需要更改Maven
- 我们可以在File——close project——Customize——AllSetting打开后所做的设置即为全局设置,如Maven中的仓库路径的指定
- 勾选Local repository的Override
注意:有时候会因为某些原因maven出现一些问题,可以直接删库重新下载。或者哪个插件有问题,删除那个插件。路径如下:本地仓库+\org\apache\maven\plugins
2-4 在IDEA中新建Maven新建Maven工程
注意:以下除去文件夹名不同其他结构均相同,用的现成的文件偷个懒。
① 测试版
目录包含 main包和test包
目录结构
GroupId:公司或组织名称的倒序
ArtifactID:当前项目名称(与name相同)
version(版本号),web版中有。
注意:Maven 默认约定了项目中 src/test/java 目录用于存放测试代码,并且按照包结构进行组织。如果你将测试类直接放在 test 目录下而不是 test 目录下的包中,可能会导致编译或运行错误。这是因为 Maven 默认会按照包结构进行编译和测试,并期望测试类按照正确的包结构进行组织。但可以选择点击右方的MAVEN—>Lifecycle---->test用来运行
② web版
目录结构
三、Maven的核心概念
3-1 Maven的POM
- POM:Project Object Model【项目对象模型】,将项目封装为对象模型,便于Maven管理构建项目。
- pom.xml的常用标签
<?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>maven_demo</artifactId>
<groupId>com.atguigu</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>maven_helloworld</artifactId>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
3-2 Maven的约定目录结构
- 项目名
- src【书写java源代码】
- main【书写java主程序代码】
- java【书写java代码】
- resources【书写配置文件代码】
- test【书写测试代码】
- java【书写测试java代码】
- main【书写java主程序代码】
- pom.xml【书写配置文件代码】
- target【编译后目录结构】
- src【书写java源代码】
3-3 Maven的生命周期
- Maven的声明周期分为三部分,在周期内按顺序执行各个命令
- Clean LifeCycle:在进行真正的构建之前进行一些清理工作。一般clean只会把当前的target清理掉。
- Default LifeCycle:构建的核心部分,编译,测试,打包,安装,部署等等。
- Site LifeCycle:生成项目报告,站点,发布站点。(site)
注意:
① Maven的三套生命周期相互独立,我们可以仅仅调用clean来清理工作目录,仅调用site来生成站点,也可以直接运行mvn clean install site运行所有生命周期。
② 按照顺序执行各个命令:当我们执行test(测试)命令的时候,会按照生命周期依次运行:编译—>测试—>打包—>安装—>部署。
生命周期名称 | 作用 | 各个环节 |
---|---|---|
Clean | 清理操作相关 | pre-clean clean post-clean |
Site | 生成站点相关 | pre-site site post-site deploy-site |
Default | 主要构建过程 | validate generate-sources process-sources generate-resources process-resources 复制并处理资源文件,至目标目录,准备打包。 compile 编译项目 main 目录下的源代码。 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将最终的包复制到远程的仓库,以让其它开发人员共享;或者部署到服务器上运行(需借助插件,例如:cargo)。 |
3-4 插件和目标
插件:Lifecycle的就是插件,插件由jar包和一些配置文件组成。
目标:插件都在Plusgins中,每一个插件都能实现多个命令,每一个命令就是一个插件目标。例如clean有三个命令,即clean插件有3个目标。
插件目录:本地仓库+\org\apache\maven\plugins
3-5 Maven的仓库
- 仓库分类
- 本地仓库:为当前计算机提供maven服务
- 远程仓库:也可以为其他计算机提供maven服务
- 私服:架设在当前局域网环境下,为当前局域网范围内所有的Maven工程提供服务
- 中央仓库:架设在Internet下,为全世界所有Maven工程提供服务
- 中央仓库的镜像:假设在各大洲,为中央仓库分担流量。减轻中央仓库的压力,同时更快的响应用户请求
- 仓库文件的类型
- Maven的插件
- 第三方框架或工具的jar包
- 自己研发的项目或模块
3-6 Maven的坐标
- 作用:使用坐标引入jar包
- 坐标由g-a-v组成
- groupId:公司或这字的域名倒序+当前项目名称
- artifactId:当前项目的模块名称
- version:当前模块的版本
补充:
- g-a-v:本地仓库的jar包位置
- a-v:jar包全名
坐标应用
- 坐标参考网址:http://mvnrepository.com 、http://search.maven.org
- 应用:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.17</version>
</dependency>
</dependencies>
四、Maven的依赖管理
4-1 依赖范围
- 依赖语法:
- complie(默认值):在main、test、Tomcat【服务器】下均有效
- test:只能在test目录下有效
- maven工程中代码一般发在test包下,所以默认为test。
- provided:在main、test下均有效,Tomcat【服务器】无效
- javax.servlet-api是因为服务器下已经有jar包了,tmocat的lib下包含servlet包,maven导入了一个jar包,jar包之间会产生冲突
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
4-2 依赖传递性
- 路径最短者优先(就近原则)
- 先声明者优先在先写的优先
- Maven可以自动解决jar包之间的依赖问题
注意:父工程也要install。
4-3 统一管理版本号
为了防止不同版本的jar包之间由冲突,我们需要同意管理版本号
步骤:
- 在properties中建立版本号名称和版本号 5.3.17
- 在原来的位置上用${版本号名称}的方式进行引用
<properties>
<spring-version>5.3.17</spring-version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring-version}</version>
</dependency>
</dependencies>
五、Maven的继承
5-1 Maven为什么需要继承
- 当子工程中大部分需要共同使用一个jar包时,可以提取父工程的jar包。类似于java类的继承,子类可以使用父类的方法。
- 父类的打包方式必须是pom方式:pom
- 其他打包方式:
- jar:java---->jar
- war:web---->war
5-2 Maven的继承方式一
- 在父工程中的pom.xml中导入jar包,在子工程中统一使用。【所有子工程强制引入父工程jar包】
<packaging>pom</packaging>
<modules>
<module>FirstMaven</module>
<module>SecondMaven</module>
<module>ThirdMaven</module>
<module>untitled</module>
</modules>
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
注意:子类父类同时存在一个需要导入的jar包,则导入子类的jar包(就近原则)
5-3 Maven继承方式二(主要)
- 父工程在 外在添加一个标签
- 子工程在中添加 相对路径
- 子工程在中添加标签,不用填写版本号。
父工程:
<packaging>pom</packaging>
<modules>
<module>FirstMaven</module>
<module>SecondMaven</module>
<module>ThirdMaven</module>
<module>untitled</module>
</modules>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
子工程:
<parent>
<groupId>com.atguigu</groupId>
<artifactId>TestMaven</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
六、Maven的聚合
- 优势:只要将子工程聚合到父工程中,就可以实现效果;例如安装和清楚父工程时,子工程会进行同步操作。即我们使用clean插件进行清理工作的时候,子工程同样会进行清理操作(父工程的target文件清理的同时,子工程的target文件同时被清理)
- Maven会按照依赖顺序自动安装子工程
<modules>
<module>FirstMaven</module>
<module>SecondMaven</module>
<module>ThirdMaven</module>
<module>untitled</module>
</modules>
七、Maven版本的web项目
步骤:
- 新建module模块
- 为当前项目添加web模块
- 修改web模块的web.xml位置及上下文路径
- 为当前的web应用设置artifacts