Maven知识

文章目录

一、概念

1、官方文档

Maven官方文档

2、什么是Maven?

Maven, a Yiddish word meaning accumulator of knowledge, began as an attempt to simplify the build processes in the Jakarta Turbine project. There were several projects, each with their own Ant build files, that were all slightly different. JARs were checked into CVS. We wanted a standard way to build the projects, a clear definition of what the project consisted of, an easy way to publish project information, and a way to share JARs across several projects.

The result is a tool that can now be used for building and managing any Java-based project. We hope that we have created something that will make the day-to-day work of Java developers easier and generally help with the comprehension of any Java-based project.

大致可以理解为:构建和管理任何基于java的项目的工具

二、相关知识

1、Maven生命周期

Maven生命周期官方解释

Maven有三套生命周期,并且这三套生命周期之间是独立的,互不相关的。每个生命周期都有不同的阶段(Phases),这些不同的阶段之间是有序的,每个阶段必须在前面的阶段执行完后才可执行。

Maven的三套生命周期分别是:clean,default,site

1.1、clean

clean用于清理项目

Phase Description
pre-clean 在实际的项目清理之前执行所需的过程
clean 删除以前构建生成的所有文件
post-clean 执行完成项目清理所需的流程

1.2、default

default是开发中经常用到的生命周期,项目的校验、编译、打包、部署等都在这里

Phase Description
validate 验证项目是否正确,所有必要的信息是否可用
initialize 初始化构建状态,例如设置属性或创建目录
generate-sources 生成要包含在编译中的任何源代码
process-sources 处理源代码,例如过滤任何值
generate-resources 生成要包含在包中的资源
process-resources 将资源复制并处理到目标目录中,准备打包
compile 编译项目的源代码
process-classes 对编译生成的文件进行后处理,例如对Java类进行字节码增强
generate-test-sources 生成任何测试源代码以包含在编译中
process-test-sources 处理测试源代码,例如过滤任何值
generate-test-resources 为测试创建资源
process-test-resources 将资源复制并处理到测试目标目录中
test-compile 将测试源代码编译到测试目标目录中
process-test-classes 对测试编译生成的文件进行后处理,例如对Java类进行字节码增强。
test 使用合适的单元测试框架运行测试。这些测试不应该要求对代码进行打包或部署
prepare-package 在实际包装前,完成包装前的准备工作。这通常会导致软件包的未打包、处理过的版本
package 将编译后的代码打包成可分发的格式,比如JAR
pre-integration-test 在执行集成测试之前执行所需的操作。这可能涉及诸如设置所需环境之类的事情
integration-test 如有必要,将包处理并部署到可以运行集成测试的环境中
post-integration-test 执行集成测试后所需的操作。这可能包括清理环境
verify 运行任何检查来验证包是有效的并且符合质量标准
install 将包安装到本地存储库中,以便在本地其他项目中作为依赖项使用
deploy 在集成或发布环境中完成,将最终包复制到远程存储库,以便与其他开发人员和项目共享

1.3、site

site主要用于生成项目的站点

Phase Description
pre-site 在实际的项目站点生成之前执行所需的过程
site 生成项目的站点文档
post-site 执行完成站点生成所需的流程,并为站点部署做准备
site-deploy 将生成的站点文档部署到指定的web服务器

2、Pom文件

Pom文件官方解释

POM代表"项目对象模型"。它是Maven项目的XML表示形式,保存在名为pom.xml的文件中。pom.xml文件中可以定义项目所需的版本、依赖、插件、构建等等

pom.xml:

这是直接在POM的项目元素下的元素列表。注意,modelVersion包含4.0.0。这是目前唯一支持的POM版本,并且总是必需的。

xml 复制代码
<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>
 
  <!-- The Basics -->
  <groupId>...</groupId>
  <artifactId>...</artifactId>
  <version>...</version>
  <packaging>...</packaging>
  <dependencies>...</dependencies>
  <parent>...</parent>
  <dependencyManagement>...</dependencyManagement>
  <modules>...</modules>
  <properties>...</properties>
 
  <!-- Build Settings -->
  <build>...</build>
  <reporting>...</reporting>
 
  <!-- More Project Information -->
  <name>...</name>
  <description>...</description>
  <url>...</url>
  <inceptionYear>...</inceptionYear>
  <licenses>...</licenses>
  <organization>...</organization>
  <developers>...</developers>
  <contributors>...</contributors>
 
  <!-- Environment Settings -->
  <issueManagement>...</issueManagement>
  <ciManagement>...</ciManagement>
  <mailingLists>...</mailingLists>
  <scm>...</scm>
  <prerequisites>...</prerequisites>
  <repositories>...</repositories>
  <pluginRepositories>...</pluginRepositories>
  <distributionManagement>...</distributionManagement>
  <profiles>...</profiles>
</project>

3、Pom常用元素

3.1、项目基本元素

xml 复制代码
    <groupId>xxx</groupId>
    <artifactId>xxx</artifactId>
    <version>xxx</version>
    <packaging>xxx</packaging>
	<name>xxx</name>
  	<description>xxx</description>
  • <groupId>xxx</groupId>:通常使用完全限定的包名来将其与具有类似名称的其他项目区分开来(例如:org.springframework.boot)。
  • <artifactId>xxx</artifactId>:该工件的标识符,在由groupId给出的组中是唯一的。工件是由项目产生或使用的东西。(例如:spring-boot-starter-web)
  • <version>xxx</version>:这个项目产生的工件的当前版本。

一个gav(groupId,artifactId,version)可以标识唯一的包或插件

例:

xml 复制代码
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>2021.0.8</version>
  • <packaging></packaging>生成该项目的类型,有jar、war、ear、pom四种类型,默认为jar
  • <name>xxx</name>:项目名称
  • <description>xxx</description>:项目表述文本

3.2、<properties></properties>

可以自定义一些公共的值在这个标签下统一管理,引用方式${xxx}

xml 复制代码
	<properties>
        <revision>1.0.0-SNAPSHOT</revision>

        <!-- dependent version -->
        <spring-boot.version>2.7.15</spring-boot.version>
        <spring-cloud-alibaba.version>2021.0.5.0</spring-cloud-alibaba.version>
        <spring-cloud.version>2021.0.8</spring-cloud.version>
        <lombok.version>1.18.30</lombok.version>
    </properties>
    
	<dependencies>
		<dependency>
        	<groupId>org.projectlombok</groupId>
        	<artifactId>lombok</artifactId>
        	<version>${lombok.version}</version>
        	<optional>true</optional>
    	</dependency>
    </dependencies>

3.3、pom继承相关

pom支持继承的方式复用父pom中的内容,以下是继承规则:

  • 子Pom继承父Pom的所有资源包括依赖、插件、构建方式
  • <dependencyManagement></dependencyManagement>或者<pluginManagement></pluginManagement>中的依赖(dependencies )或插件(plugins )不会被继承到子类,但是子类如果需要使用可以自己引入这里面的依赖(dependencies )或插件(plugins ),并且不需要声明<version></version>(会自动继承父类的version)
  • 子pom和父pom声明同一个依赖(dependencies )或插件(plugins ),如果子pom声明了<version></version>,子pom声明的version有限生效
xml 复制代码
    <parent>
        <artifactId>xxx</artifactId>
        <groupId>xxx</groupId>
        <version>xxx</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

	<modules>
       	<module>xxx</module>
        <module>xxx</module>
    </modules>
  • <parent>...</parent>:子Pom中标识父Pom相关坐标
  • <relativePath>.../pom.xml</relativePath>:指定父Pom的相对路径,默认为.../pom.xml
  • <modules/module></modules/module>:子模块信息

3.4、依赖管理相关

xml 复制代码
   <dependencies>
      <dependency>
        <groupId></groupId>
        <artifactId></artifactId>
        <version></version/>
        <type></type>
        <classifier></classifier>
        <scope></scope>
        <systemPath></systemPath>
        <exclusions>
          <exclusion>
            <groupId/></groupId>
            <artifactId></artifactId>
          </exclusion>
        </exclusions>
        <optional/>
      </dependency>
    </dependencies>
  • <dependencies/dependency>...</dependencies/dependency>:依赖信息
  • <type>xxx</type>:依赖类型包括jar、war、pom等等,默认是jar。
  • <classifier>xxx</classifier>:引用附加的工件或区分属于同一POM但构建方式不同的两个工件

例子:
a、引用附加的工件

xml 复制代码
	  <dependency>
        <groupId>com.test</groupId>
        <artifactId>mytest</artifactId>
        <version>1.0</version/>
        <classifier>javadoc</classifier>
      </dependency>

这样就会引入两个依赖mytest-1.0.jar和mytest-1.0-javadoc.jar

b、区分属于同一POM但构建方式不同的两个工件

xml 复制代码
	  <dependency>
        <groupId>com.test</groupId>
        <artifactId>mytest</artifactId>
        <version>1.0</version/>
        <classifier>jdk15</classifier>
      </dependency>

我假设现在项目是jdk8,在mytest包中用的jdk15就可以这样声明

  • <systemPath>xxx</systemPath>:指定引用jar包的路径,例如:D:\a\b
  • <scope>xxx</scope>:指定依赖的作用域,包括compile、runtime、test、system、provided、import,默认是compile
  • compile:这是默认作用域。全程参与当前项目的编译、测试、运行时、打包。这个作用域是可传递的
  • runtime:该依赖项目无需参与项目的编译,不过后期的测试和运行周期需要其参与。
  • test:仅在测试编译和执行阶段可用。这个作用域不是可传递的
  • system:不会从maven库中取依赖,而是从本地文件拿。一般要搭配<systemPath></systemPath>使用
  • provided: 参与编译,测试,运行等周期但不参与打包,希望该依赖是容器或者jdk等提供。这个作用域不是可传递的
  • import:一般只用于<dependencyManagement></dependencyManagement>依赖管理中,把当前依赖下的所有依赖都引入进来。
  • <exclusions/exclusion>...</exclusions/exclusion>:排除相关依赖
  • <optional>xxx</optional>:表示该依赖是否是可选的。值只有true或者false,默认为false。true就是可选的,不会参与传递。

3.5、构建管理相关

包括资源的加载、插件的管理等

xml 复制代码
    <build>
        <finalName>xxx</finalName>
        <resources>
            <resource>
                <directory>xxx</directory>
                <filtering>false</filtering>
                <includes>
                    <include>**/*</include>
                </includes>
            </resource>
        </resources>
        
        <plugins>
            <plugin>
                <groupId>xxx</groupId>
                <artifactId>xxx</artifactId>
                <version>1.0</version>
                <executions>
                    <execution>
                        <id>xxx</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
  • <build>...</build>:包含构建项目所需的信息
  • <finalName>xxx</finalName>:构建项目生成的文件名
  • <resources/resource>...</resources/resource>:类路径资源配置
  • <directory>xxx</directory>:描述资源存储的目录。路径是相对于POM的。
  • <filtering>xxx</filtering>:是否过滤资源以用参数化值替换。值为true或者false,默认为false
  • <includes/include>xxx</includes/include>:要包含的文件路径
  • <excludes/exclude>xxx</excludes/exclude>:要排除的文件路径
  • <plugins/plugin>xxx</plugins/plugin>:插件信息
  • <pluginManagement></pluginManagement>:插件管理,一般用于pom继承时插件的管理,类似<dependencyManagement>
  • <executions/execution>xxx</executions/execution>:在构建生命周期中执行的一组目标的一个或多个规范,每个规范(可能)都有不同的配置。
  • <inherited>xxx</inherited>:插件是否可传递(继承到子类)
  • <id>xxx</id>:用于在构建期间标记目标,默认值:default
  • <phase>xxx</phase>:将此执行中的目标绑定到Maven的default生命周期中的阶段(参见Maven生命周期default)
  • <goals/goal>xxx</goals/goal>:绑定插件的声明周期中的阶段
  • <configuration>xxx</configuration>:可用于配置插件内部所需的配置信息

3.6、<profiles></profiles>

可用于动态环境激活项目

yml 复制代码
spring:
  profiles:
    active: @spring.profiles.active@
xml 复制代码
    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <spring.profiles.active>dev</spring.profiles.active>
            </properties>
            <!-- 默认环境 -->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <spring.profiles.active>test</spring.profiles.active>
            </properties>
        </profile>
    </profiles>

可在开发(dev)、测试(test)、生产(prod)环境等切换相应配置

三、拓展

1、Maven配置阿里云镜像加速

1、找到maven的setting.xml配置,一般在C:\Users\xxx.m2下

2、找到标签添加以下代码

xml 复制代码
<mirrors>
  <mirror>
    <id>aliyun</id>
    <mirrorOf>*</mirrorOf>
    <name>aliyun maven</name>
    <url>https://maven.aliyun.com/repository/public</url>
  </mirror>
  <mirror>
    <id>aliyun-nexus</id>
    <mirrorOf>*</mirrorOf>
    <name>aliyun nexus</name>
    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
  </mirror>
</mirrors>
相关推荐
进阶的架构师1 分钟前
互联网Java工程师面试题及答案整理(2024年最新版)
java·开发语言
黄俊懿2 分钟前
【深入理解SpringCloud微服务】手写实现各种限流算法——固定时间窗、滑动时间窗、令牌桶算法、漏桶算法
java·后端·算法·spring cloud·微服务·架构
木子020410 分钟前
java高并发场景RabbitMQ的使用
java·开发语言
夜雨翦春韭21 分钟前
【代码随想录Day29】贪心算法Part03
java·数据结构·算法·leetcode·贪心算法
大霞上仙44 分钟前
jmeter学习(1)线程组与发送请求
java·学习·jmeter
笃励1 小时前
Java面试题二
java·开发语言·python
易雪寒1 小时前
IDEA在git提交时添加忽略文件
java·git·intellij-idea
打码人的日常分享2 小时前
企业人力资源管理,人事档案管理,绩效考核,五险一金,招聘培训,薪酬管理一体化管理系统(源码)
java·数据库·python·需求分析·规格说明书
27669582922 小时前
京东e卡滑块 分析
java·javascript·python·node.js·go·滑块·京东
爱写代码的刚子2 小时前
C++知识总结
java·开发语言·c++