Maven的常用基本概念
本文主要记录一些关于Maven的常用基本概念,如需了解Maven的安装及配置可以参看笔者的另一篇博文
https://blog.csdn.net/weixin_43801935/article/details/84553806
一、什么是构件
任何可以被Maven的坐标系统唯一标识的对象都是一个Maven构件。大多数情况下构件是构建Maven项目所生成的文件,如JAR。但是只包含其他POM使用的定义的POM文件也是Maven的构件。
Maven构件的类型由其POM文件的元素指定。最常用的值是pom、jar、ear、war以及maven-plugin。
二、使用POM文件的方式
使用pom文件的方式一般分为以下三种:
1.默认的:
这种方式直接用于构建一个构件
2.父POM:
这种方式提供一个由子项目继承的单个配置信息源,在子项目中声明这个POM文件作为它们的元素的值。
3.聚合器:
这种方式用于构建一组声明为的项目,这些子项目位于其当前聚合器项目的文件夹中,每个都包含有它自己的POM文件。
小白说明:
作为父POM或者聚合器的POM文件的元素的值将是pom。注意一个pom文件可能同时提供两项功能。
三、GAV坐标
POM定义了5种称为坐标的元素,用于标识Maven构件。GAV是、以及的首字母缩写,这三个坐标是必须始终指定的。
一个完整的坐标表达式具有如下格式:
artifactId:groupId:packaging:version:classifier
1.groupId:
项目或者项目组的全局的唯一标识符。这通常是Java源代码中使用的全限定的Java包名。
2.artifactId:
用于标识和某个相关的不同的构件。
3.type:
是指和项目相关的主要构件的类型,对应于侯建的POM文件中的的值。它的默认值为jar。
4.version:
标识了构件的版本。
5.classifier:
用于区分属于相同的POM但是却被以不同的方式构建的构件。例如jdk16、jdk17。
POM文件必须声明它所管理的构件的坐标。
<groupId>com.test<groupId>
<artifactId>testContent<artifactId>
<version>1.0.0<version>
<packaging>jar</packaging>
如例子中拥有如下坐标的项目将会产生一个具有以下格式的名称的构件:
artifactId-version.packaging,如testContent-1.0.0.jar
四、依赖
项目的依赖是指编译和执行它所需要的外部构件。依赖可以传递,一个复杂的项目可能会有一个深层级的依赖树。
Maven的声明在POM的元素中:
<dependencies>
<dependency>
<groupId/>
<artifactId/>
<version/>
<type/>
<scope/>
<systemPath/>
</dependency>
...
</dependencies>
在dependency声明中,GAV坐标是必不可少的。type和scope元素对于那些值不分别是默认值jar和compile的依赖来说也是必需的。
元素可以具有以下值:
compile --编译和执行需要的(默认)
runtime --只有执行需要
optional --不被引用了这个项目所产生的构件的其他项目,视为传递依赖。
provided --不会被包含在由这个POM产生的WAR文件的WEB_INF/lib目录中
test --只有编译和测试的执行需要
systemPath --元素用来指定文件系统中的绝对位置
五、依赖管理
POM的dependencyManagement元素包含可以被其他项目使用的dependency声明。这样的POM的子项目将会自动继承这些声明。其他项目可以通过使用scope的import值来导入它们。
引用了dependencyManagement元素的项目可以使用它所声明的依赖,而不需要指定它们的version坐标。如果dependencyManagement中version在稍后有所改变,则他将被所有引用它的POM拾起。
例如下面示例中版本在properties部分定义,在dependencyManagement引用。
<properties>
<test.version>1.0.0</test.version>
...
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.test</groupId>
<artifactId>testContent</artifactId>
<version>${test.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
对于这种情况,依赖的scope元素有一个特殊的import值,它将外部POM(没有被声明为parent)的dependencyManagement元素的内容导入到当前POM的dependencyManagement元素中。
六、构建的生命周期
Maven构建的生命周期是一个明确定义的用于构建和分发构件的过程。有3个内置的构建生命周期:clean、default、和site。
一个构建的生命周期由一系列的阶段所组成。下面列举了默认的构建生命周期的各个阶段的部分清单。
validate --检查项目是都正确,所有必须的信息是否已经就绪。
process-sources --处理源代码,如过滤任何值。
compile --编译项目的源代码。
process-test-resources --复制并处理资源到目标目录中。
test --使用合适的单元测试框架测试编译的源代码。
package --将编译的代码打包为它的可分发格式,如JAR。
integration-test --处理并将软件包部署到一个可以运行集成的测试环境中。
verify --运行任何的检查以验证软件包是否有效,并且符合质量标准。
install --将软件包安装到本地存储库中,在那里其他本地构建项目可以将它引用为依赖。
deploy --将最终的构件上传到远程存储库,以与其他开发人员和项目共享。
执行这些阶段中的一个阶段将会调用所有前面的阶段。
七、插件和插件管理
虽然Maven协调了所有构建生命周期阶段的执行,但是它并没有直接实现它们。相反,它将它们委托给了插件,这些插件是maven-plugin类型的构件(打包为JAR文件)。Apache Maven项目为标准构建生命周期所定义的所有任务都提供了插件,更多的是由第三方生产的,用于处理各种自定义的任务。插件可能拥有多个内部步骤,或者目标,其也可以被单独调用。
插件的配置如
<plugin>
<groupId>com.test<groupId>
<artifactId>test-plugin</artifactId>
<version>1.0.0</version>
</plugin>
如同dependencyManagement,pluginManagement声明了其他POM可以使用的信息,如下面的例子,但这只适用于子POM,因为对于插件来说没有导入声明,version坐标是继承的。
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>testContent</artifactId>
<version>1.0.0</version>
<configuration>
<source>1.8</source>
<target>1.8<target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
下面的例子演示子POM如何继承父POM的pluginManagement配置,注意子POM可以只引用其构建所需的插件,同时子POM还可以重写它需要自定义的任何插件配置。
<build>
<plugins>
<plugin>
<artifactId>testContent</artifactId>
</plugin>
</plugins>
</build>
小白说明
在声明Maven项目生成的插件时可以省略groupId
八、完整的POM示例展示
下面列出三个文件块为大家展示父POM、子POM和聚合器的具体使用样例
父POM
<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>com.test</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<junit.version>4.12</junit.version>
<mysql.version>8.0.13</mysql.version>
<slf4j.version>1.7.25</slf4j.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
聚合器
<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>
<parent>
<groupId>com.test</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.test</groupId>
<artifactId>aggregator</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
<modules>
<module>module</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<port>8080</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
子POM
<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>
<parent>
<groupId>com.test</groupId>
<artifactId>aggregator</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>module</artifactId>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
</dependencies>
</project>
到此maven的常用基本概念就介绍完了,关于maven的知识非常多,此处并没有全部讲述完,后面不定期更新,如果你希望自己学习更多也可以在 http://maven.apache.org 上查看官方文档。
写文章不易喜欢就点个赞吧~