概述
- Maven 是一个项目管理和整合工具
- Maven 为开发者提供了一套完整的构建生命周期框架
- Maven 简化了工程的构建过程,并对其标准化,它无缝衔接了编译、发布、文档生成、团队合作和其他任务
Maven安装
设置 Maven 环境变量(不清楚的自行查找资料)
下载安装文件,选择最新稳定版本:http://maven.apache.org/download.html
- Windows
配置M2_HOME环境变量:
M2_HOME=D:\Software\apache-maven-3.5.0;
将bin目录追加到PATH路径中:%M2_HOME%\bin;
功能
- 项目管理工具
- 依赖管理工具
- 构建工具
优点
- 对第三方依赖库进行了统一的版本管理
- 统一了构建过程
- 统一了项目的目录结构
POM文件详解
- POM 代表工程对象模型,它是使用Maven工作时的基本组件,是一个xml文件,它被放在工程根目录下,文件命名为pom.xml;
- POM 包含了关于工程和各种配置细节的信息,Maven使用这些信息构建工程;
- POM 也包含了目标和插件,当执行一个任务或者目标时,Maven会查找当前目录下的POM,从其中读取所需要的配置信息,然后执行目标;
- 在创建POM之前,我们首先确定工程组(groupId),及其名称(artifactId)和版本,在仓库中这些属性是工程的唯一标识;
- 所有的POM文件需要project元素和三个必须的字段:groupId, artifactId,version;
- 在仓库中的工程标识为groupId:artifactId:version;
- POM.xml的根元素是 project,它有三个主要的子节点
- groupId:这是工程组的标识,它在一个组织或者项目中通常是唯一的;
- artifactId:这是工程的标识,它通常是工程的名称,groupId 和 artifactId 一起定义了 artifact 在仓库中的位置;
- version:这是工程的版本号,在 artifact 的仓库中,它用来区分不同的版本;
POM内容详解https://blog.csdn.net/oDeviloo/article/details/52050277
概述
<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>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<packaging>...</packaging>
<!-- 依赖配置 -->
<dependencies>...</dependencies>
<parent>...</parent>
<dependencyManagement>...</dependencyManagement>
<modules>...</modules>
<properties>...</properties>
<!-- 构建配置 -->
<build>...</build>
<reporting>...</reporting>
<!-- 项目信息 -->
<name>...</name>
<description>...</description>
<url>...</url>
<inceptionYear>...</inceptionYear>
<licenses>...</licenses>
<organization>...</organization>
<developers>...</developers>
<contributors>...</contributors>
<!-- 环境设置 -->
<issueManagement>...</issueManagement>
<ciManagement>...</ciManagement>
<mailingLists>...</mailingLists>
<scm>...</scm>
<prerequisites>...</prerequisites>
<repositories>...</repositories>
<pluginRepositories>...</pluginRepositories>
<distributionManagement>...</distributionManagement>
<profiles>...</profiles>
</project>
基本配置
- modelVersion : pom模型版本,maven2和3只能为4.0.0
- groupId : 组ID,maven用于定位
- artifactId : 在组中的唯一ID用于定位
- version : 项目版本
- packaging : 项目打包方式,有以下值:pom, jar, maven-plugin, ejb, war, ear, rar, par,默认为jar
依赖配置
- parent:用于确定父项目的坐标
<parent>
<groupId>com.hyn</groupId>
<artifactId>SIP-parent</artifactId>
<relativePath></relativePath>
<version>0.0.1-SNAPSHOT</version>
</parent>
groupId:父项目的构件标识符
artifactId:父项目的唯一标识符
relativePath:Maven首先在当前项目的找父项目的pom,然后在文件系统的这个位置(relativePath),然后在本地仓库,再在远程仓库找
version:父项目的版本
- modules : 有些maven项目会做成多模块的,这个标签用于指定当前项目所包含的所有模块。之后对这个项目进行的maven操作,会让所有子模块也进行相同操作
<modules>
<module>com-a</module>
<module>com-b</module>
<module>com-c</module>
</modules>
- properties : 用于定义pom常量
<properties>
<java.version>1.7</java.version>
</properties>
这个常量可以在pom文件的任意地方通过${java.version}来引用
- dependencies : 项目相关依赖配置,如果在父项目写的依赖,会被子项目引用,一般父项目会将子项目公用的依赖引入
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
- dependencyManagement : 在父模块中定义后,子模块不会直接使用对应依赖,但是在使用相同依赖的时候可以不加版本号,这样的好处是,父项目统一了版本,而且子项目可以在需要的时候才引用对应的依赖
父项目:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
子项目:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
构建配置
- build : 用于配置项目构建相关信息
<build>
<!--该元素设置了项目源码目录,当构建项目的时候,构建系统会编译目录里的源码。该路径是相对于pom.xml的相对路径。-->
<sourceDirectory/>
<!--该元素设置了项目脚本源码目录,该目录和源码目录不同:绝大多数情况下,该目录下的内容会被拷贝到输出目录(因为脚本是被解释的,而不是被编译的)。-->
<scriptSourceDirectory/>
<!--该元素设置了项目单元测试使用的源码目录,当测试项目的时候,构建系统会编译目录里的源码。该路径是相对于pom.xml的相对路径。-->
<testSourceDirectory/>
<!--被编译过的应用程序class文件存放的目录。-->
<outputDirectory/>
<!--被编译过的测试class文件存放的目录。-->
<testOutputDirectory/>
<!--使用来自该项目的一系列构建扩展-->
<extensions>
<!--描述使用到的构建扩展。-->
<extension>
<!--构建扩展的groupId-->
<groupId/>
<!--构建扩展的artifactId-->
<artifactId/>
<!--构建扩展的版本-->
<version/>
</extension>
</extensions>
<!--当项目没有规定目标(Maven2 叫做阶段)时的默认值-->
<defaultGoal/>
<!--这个元素描述了项目相关的所有资源路径列表,例如和项目相关的属性文件,这些资源被包含在最终的打包文件里。-->
<resources>
<!--这个元素描述了项目相关或测试相关的所有资源路径-->
<resource>
<!-- 描述了资源的目标路径。该路径相对target/classes目录(例如${project.build.outputDirectory})。举个例子,如果你想资源在特定的包里(org.apache.maven.messages),你就必须该元素设置为org/apache/maven/messages。然而,如果你只是想把资源放到源码目录结构里,就不需要该配置。-->
<targetPath/>
<!--是否使用参数值代替参数名。参数值取自properties元素或者文件里配置的属性,文件在filters元素里列出。-->
<filtering/>
<!--描述存放资源的目录,该路径相对POM路径-->
<directory/>
<!--包含的模式列表,例如**/*.xml.-->
<includes/>
<!--排除的模式列表,例如**/*.xml-->
<excludes/>
</resource>
</resources>
<!--这个元素描述了单元测试相关的所有资源路径,例如和单元测试相关的属性文件。-->
<testResources>
<!--这个元素描述了测试相关的所有资源路径,参见build/resources/resource元素的说明-->
<testResource>
<targetPath/>
<filtering/>
<directory/>
<includes/>
<excludes/>
</testResource>
</testResources>
<!--构建产生的所有文件存放的目录-->
<directory/>
<!--产生的构件的文件名,默认值是${artifactId}-${version}。-->
<finalName/>
<!--当filtering开关打开时,使用到的过滤器属性文件列表-->
<filters/>
<!--子项目可以引用的默认插件信息。该插件配置项直到被引用时才会被解析或绑定到生命周期。给定插件的任何本地配置都会覆盖这里的配置-->
<pluginManagement>
<!--使用的插件列表 。-->
<plugins>
<!--plugin元素包含描述插件所需要的信息。-->
<plugin>
<!--插件在仓库里的group ID-->
<groupId/>
<!--插件在仓库里的artifact ID-->
<artifactId/>
<!--被使用的插件的版本(或版本范围)-->
<version/>
<!--是否从该插件下载Maven扩展(例如打包和类型处理器),由于性能原因,只有在真需要下载时,该元素才被设置成enabled。-->
<extensions/>
<!--在构建生命周期中执行一组目标的配置。每个目标可能有不同的配置。-->
<executions>
<!--execution元素包含了插件执行需要的信息-->
<execution>
<!--执行目标的标识符,用于标识构建过程中的目标,或者匹配继承过程中需要合并的执行目标-->
<id/>
<!--绑定了目标的构建生命周期阶段,如果省略,目标会被绑定到源数据里配置的默认阶段-->
<phase/>
<!--配置的执行目标-->
<goals/>
<!--配置是否被传播到子POM-->
<inherited/>
<!--作为DOM对象的配置-->
<configuration/>
</execution>
</executions>
<!--项目引入插件所需要的额外依赖-->
<dependencies>
<!--参见dependencies/dependency元素-->
<dependency>
......
</dependency>
</dependencies>
<!--任何配置是否被传播到子项目-->
<inherited/>
<!--作为DOM对象的配置-->
<configuration/>
</plugin>
</plugins>
</pluginManagement>
<!--使用的插件列表-->
<plugins>
<!--参见build/pluginManagement/plugins/plugin元素-->
<plugin>
<groupId/>
<artifactId/>
<version/>
<extensions/>
<executions>
<execution>
<id/>
<phase/>
<goals/>
<inherited/>
<configuration/>
</execution>
</executions>
<dependencies>
<!--参见dependencies/dependency元素-->
<dependency>
......
</dependency>
</dependencies>
<goals/>
<inherited/>
<configuration/>
</plugin>
</plugins>
</build>