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用于清理项目

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

1.2、default

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

PhaseDescription
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主要用于生成项目的站点

PhaseDescription
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版本,并且总是必需的。

<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、项目基本元素

    <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)可以标识唯一的包或插件
例:

        <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

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

	<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有限生效
    <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、依赖管理相关

   <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、引用附加的工件

	  <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但构建方式不同的两个工件

	  <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就是可选的,不会参与传递。
    例如:父pom的某个依赖包dependencyA内部引入了dependencyB,但是dependencyB使用了<optional>true</optional>,那么子pom里引入该依赖包dependencyA时不会一起引入dependencyB

3.5、构建管理相关

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

    <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

可用于动态环境激活项目

spring:
  profiles:
    active: @spring.profiles.active@
    <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、找到标签添加以下代码

<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>
  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值