基本信息
Maven项目一般包含以下信息:
groupId:组织/公司域名反写
artifactId:项目名称
version:项目的版本信息
name:项目简称
description:项目的简要描述
例如:
<?xml version="1.0" encoding="UTF-8"?>
<project ...>
<!-- 固定 4.0.0, 指定了当前 POM 模型的版本 -->
<modelVersion>4.0.0</modelVersion>
<groupId>com.gty.test</groupId>
<artifactId>maven-demo</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>maven demo</version>
<description>This is maven demo.</description>
</project>
Maven依赖引入方式
通过<dependencies>标签引入依赖,格式如下:
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
<scope>runtime</scope>
</dependency>
<dependency>
……
</dependency>
</dependencies>
其中scope可选值包括:
compile:编译时需要用到该jar
runtime:编译时不需要但运行时需要
provided:编译时需要,但运行时由jdk或某个服务提供
test:测试时需要用到该jar
处理间接依赖和顺序问题
Maven引入一个依赖包后,会将其依赖的子包一起引入进来,但存在多个相同的间接依赖时,根据以下步骤判断:
层级一致,优先导入父类定义在前面的间接依赖,例如:依赖A和依赖B均直接依赖C,且依赖A配置在前,这时无论版本是否一致,均按依赖A引入的依赖C为准
层级不一致,优先导入层级最高的依赖,无论版本,例如:依赖A通过依赖D引入依赖C,依赖B直接依赖C,且依赖A配置在前,是无论版本是否一致,依赖B对C的依赖层级更高,因此按依赖B引入的依赖C为准
依赖排除
在引用多个模块发生依赖冲突的时候,可以通过<excludes>
标签排除依赖,如下:
<dependencies>
<dependency>
<groupId>xyz.ibudai</groupId>
<artifactId>demo-a</artifactId>
<version>1.0.0</version>
<excludes>
<exclude>
<groupId>xyz.ibudai</groupId>
<artifactId>dependency-b</artifactId>
<version>1.0.0</version>
</exclude>
</excludes>
</dependency>
</dependencies>
或通过<optional>
标签禁用依赖传递,例如:
<dependencies>
<dependency>
<groupId>xyz.ibudai</groupId>
<artifactId>demo-a</artifactId>
<version>1.0.0</version>
<optional>true</optional>
</dependency>
</dependencies>
以上两种都可以在引入依赖a的时候避免引入依赖b
变量配置
使用<properties>标签可以自定义变量,通过${}引入变量,例如:
<properties>
<mysql.version>8.0.30</mysql.version>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
模块管理
当项目有多个子模块,可以通过<module>
标签进行模块管理,例如:
<!-- parent.pom -->
<modules>
<module>module-1</module>
<module>module-2</module>
</modules>
为module-1和module-2设置同一个父pom,在父pom中使用模块管理两个子模块pom。
在子pom中使用<parent>
标签标识继承的父pom
<!-- module-1.pom -->
<parent>
<groupId>xyz.ibudai</groupId>
<artifactId>maven-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>module-1</artifactId>
通过在父pom中添加<dependencyManagement>
标签可以进行模块统一管理,在<dependencyManagement>
中定义的模块既不会在当前模块引入依赖,也不会给子模块引入依赖,它的功能就是当子模块添加依赖的时候,自动引入在其中管理的版本
这种场景可以应用于多个模块中部分模块依赖,部分不依赖的情况,可以实现统一管理
除此之外,<dependencyManagement>
还可以实现非继承关系的pom之间的依赖统一管理,可以将scope设置为import,并将type设置为pom。例如:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>xyz.ibudai</groupId>
<artifactId>maven-demo</artifactId>
<version>1.0.0-SNAPSHOT</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
插件管理
maven-jar-plugin
这个插件可以添加额外信息至打包后的JAR包里面的/META-INF/MANIFEST.MF
文件中,例如:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<archive>
<manifest>
<mainClass>org.example.MyTest</mainClass>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
<!-- 配置额外属性信息 -->
<manifestEntries>
<Plugin-Id>demo-plugin</Plugin-Id>
<Plugin-Version>1.0.0</Plugin-Version>
</manifestEntries>
</archive>
</configuration>
</plugin>
Assembly
正常Maven打包仅编译工程中新建的java文件并存储其.class,使用Assembly插件可以让依赖一起打仅JAR包里面
Shade
Shade的功能也是打包依赖,同时可以通过配置策略指定需要打包的依赖。此外Shade还可以对依赖进行二次包装从而绕开Maven引用依赖版本唯一化的限制。
核心标签包括:
<includes>:指定需要一起打包的依赖
<relocations>:实现模块重命名
<filters>:对非必要文件的排除,可以进行模糊通配
构建配置
版本指定
在<plugin>
标签内可以指定工程打包编译使用的JDK版本
<plugins>
<plugin>
<!-- 编译时使用 JDK 版本 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
文件排除
默认项目打包后/resources
目录下文件都将统一打包进编译后的 JAR 文件,但为了方便配置修改通常将配置文件排除打包,使用时只需将文件放置于 JAR 同级即可。
如下示例中将application.yml 文件排除打包,后续若需要修改配置无需重新打包只需重启项目即可。
<resources>
<resource>
<!-- 设置编译去除 yml 配置文件 -->
<directory>src/main/resources</directory>
<excludes>
<exclude>application.yml</exclude>
</excludes>
</resource>
</resources>
主类配置
在打包时可能出现无法识别工程主类的问题,导致编译后的文件无法正常运行,此时则可以在 pom 文件中手动设置工程的主类。
其中 <mainClass> 中配置的为项目主类的完成限定名。
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>xyz.ibudai.TestWebApplication</mainClass>
<layout>JAR</layout>
</configuration>
</plugin>
</plugins>
maven仓库配置文件
maven-settings.xml是用来管理镜像仓库的,主要标签有:
<profiles>
:<profiles>
下面可以配置<repositories>
,其中可以有多个<repository>
,用不同的id进行区分。当不设置activeProfiles时,配置了多个profile时,默认是都有效,会依次进行尝试下载。如果都下载不到的话,还会去一个默认的central仓库找。<mirrors>
:仓库备份拦截,通过<mirrorOf>
配置的名称拦截对应id的<repository>
,例如不配置central仓,就会以<mirrorOf>
为central的<mirror>
地址来拦截central<server>
:管理仓库认证的,<server>
的id与<repository>
的id一致,可以为镜像仓库提供认证能力
评论区