写在前面:
- 本文转载了https://blog.youkuaiyun.com/cpf2016/article/details/45674377的很多内容,多谢!
- 刚开始写,刚开始学,不好之处还请批评指正!
一、Maven的POM:
maven 通过pom.xml来配置项目的基本信息、依赖信息、构建信息等,和 Ant 的build.xml 一个道理
(一)基本信息
在标签内配置,主要有:
-
<groupId>
: 组织标识,例如:org.codehaus.mojo,在M2_REPO目录下,将是: org/codehaus/mojo目录。 -
<artifactId>
: 项目名称,例如:my-project,在M2_REPO目录下,将是:org/codehaus/mojo/my-project目录。 -
<version>
: 版本号,例如:1.0,在M2_REPO目录下,将是:org/codehaus/mojo/my-project/1.0目录。 -
<packaging>
: 打包的格式,可以为:pom , jar , maven-plugin , ejb , war , ear , rar , par。如果是继承自别的pom则需要写成pom。
(二)继承信息
如果不在子pom.xml
中删除,会报一下提示:
在<project></project>
标签内配置,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>
<groupId>org.codehaus.mojo</groupId>
<artifactId>my-parent</artifactId>
<version>2.0</version>
<!-- 父类 packaging 必须设置为 pom -->
<packaging>pom</packaging>
<!--统一配置junit的依赖-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifact>
<version>4.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
</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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.codehaus.mojo</groupId>
<artifactId>my-parent</artifactId>
<version>2.0</version>
<!-- relativePath 不是必需的,用于指定在搜索本地和远程存储库之前,最先搜索父项的路径。 -->
<!--以本pom.xml为基准,定位父pom.xml位置-->
<relativePath>../my-parent/pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifact>
<!--子工程无需再配置version,如果不想和父工程一致,可以写,一致的话就不用写了
<version>4.0.0</version>
-->
<scope>test</scope>
</dependency>
</dependencies>
<artifactId>my-project</artifactId>
</project>
(三)属性信息
pom.xml 中的属性是值的占位符,类似EL(Expression Language,),类似ant的属性,比如${X},可用于pom文件任何赋值的位置。
可以使用属性信息对同一系列的jar进行版本的统一管理。
有以下分类:
- env.X:操作系统环境变量,比如${env.PATH}
- project.x:pom文件中的属性,比如:1.0,引用方式:${project.version}
- settings.x:settings.xml文件中的属性,比如:false,引用方式:${settings.offline}
- Java System Properties:java.lang.System.getProperties()中的属性,比如java.home,引用方式:${java.home}
- 自定义:在pom文件中可以:c:/apps/cargo-installs,引用方式:${installDir}
示例:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdk.version>1.7</jdk.version>
<spring.version>4.1.1.RELEASE</spring.version>
<jstl.version>1.2</jstl.version>
<junit.version>4.11</junit.version>
<logback.version>1.0.13</logback.version>
<jcl-over-slf4j.version>1.7.5</jcl-over-slf4j.version>
</properties>
(四)依赖信息
在<dependencies><dependency></dependency></dependencies>
标签内配置。
<!-- 依赖列表 -->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<!-- type默认为jar -->
<type>jar</type>
<!-- scope(依赖范围)类型包含:compile、provided、runtime、test、system,指出该项依赖参与到自动构建的那个过程 -->
<!-- compile:默认范围,在所有类路径中都可用,会被打包,该依赖关系会传播到依赖项目。 -->
<!-- provided:类似于compile,仅在编译和测试类路径中可用,不会被打包。该依赖关系不具有传递性。 -->
<!-- runtime:表示依赖不是编译所必需的,而是用于执行。它在运行时和测试类路径中,但不在编译类路径中。会被打包。 -->
<!-- test:表示依赖关系对于正常使用应用程序不是必需的,并且仅适用于测试编译和执行阶段。不会被打包,它不具有传递性。 -->
<!-- system: 类似provided,不过依赖不会从maven远程中央仓库下载,而是从本地maven仓库中获取。-->
<scope>test</scope>
<!-- systemPath 仅仅当依赖范围是 system 时使用 -->
<!--
<systemPath>绝对路径</systemPath>
-->
<!-- optional 默认为 false。true表示当其他项目依赖此项目时不会引入该依赖。-->
<optional>true</optional>
<!-- 显示排除依赖项 -->
<!--
<exclusions>
<exclusion>
<groupId></groupId>
<artifactId></artifactId>
</exclusion>
</exclusions>
-->
</dependency>
<!-- <dependency></dependency> -->
</dependencies>
TIPS:
- 依赖具有传递性,如:项目A依赖项目B,项目B依赖spring-core.jar,那么在项目A会自动的依赖spring-core.jar。
- compiler范围的依赖才能传递。test和provided范围的依赖不能传递,如果项目A和B都有需要,只能重复添加。
- 依赖的原则:
- 就近原则
【1】就近原则
MakeFriends 离HelloFriend近,所以传递过来的是1.2.14版本。
【2】如果距离相同,先声明者优先原则
MakeFriends和HelloFriends、OurFriends距离相同。所以需要根据MakeFriends的pom.xml中,是先依赖的HelloFriends还是OurFirends。
(五)构建信息
在<build></build>
标签中配置,<build>
标签中主要有两类,如下:
- 全局配置(project build):在
<project>
标签内,针对整个项目的所有情况都有效可以包含,包含两个特殊的元素,即各种<directory>
和<extensions>
. - profile 配置(profile build)在
<profile>
内,针对不同的profile配置,只包括基本的build元素。
<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/maven-v4_0_0.xsd">
…
<!– "Project Build" contains more elements than just the BaseBuild set –>
<build>…</build>
<profiles>
<profile>
<!– "Profile Build" contains a subset of "Project Build"s elements –>
<build>…</build>
</profile>
</profiles>
</project>
5.1 基本元素
两种<build>
标签都可以包括
<build>
...
<defaultGoal>install</defaultGoal>
<directory>${basedir}/target</directory>
<finalName>${artifactId}-${version}</finalName>
<filters>
<filter>filters/filter1.properties</filter>
</filters>
...
</build>
- defaultGoal,执行构建时默认的goal或phase,如jar:jar或者package等
- directory,构建的结果所在的路径,默认为${basedir}/target目录
- finalName,构建的最终结果的名字,该名字可能在其他plugin中被改
- filter,定义*.properties文件,包含一个properties列表,该列表会应用到支持filter的resources中。也就是说,定义在filter的文件中的name=value键值对,会在build时代替
${name}
值应用到<resources>
中。maven的默认filter文件夹为${basedir}/src/main/filters
5.2 resources元素
- Resources:用于排除或包含某些资源文件
<build>
....
<!-- 资源元素列表,每一个都描述与项目关联的文件是什么和在哪里 -->
<resources>
<resource>
<!-- 用于指定放置构建资源集的目录。目标路径默认为基础目录。 -->
<targetPath>src/main/resources</targetPath>
<!-- true或false,表示是否为此资源启用过滤。即是否启用上面基本元素的 <filters> 过滤。 -->
<filtering>false</filtering>
<!-- 定义资源的位置。构建的默认目录是${basedir}/src/main/resources。 -->
<directory>${basedir}/src/main/resources</directory>
<!-- 指定包含的资源文件,用 * 号做通配符。 -->
<includes>
<include>configuration.xml</include>
<!-- <include></include> -->
</includes>
<!-- 要排除哪些文件。在包含和排除之间冲突时,排除胜利。 -->
<excludes>
<exclude>**/*.properties</exclude>
<!-- <exclude></exclude> -->
</excludes>
</resource>
<!-- <resource></resource> -->
</resources>
<!-- 定义和resource类似,只不过在test时使用,测试资源元素列表 -->
<testResources>
<testResource>
<!-- 配置与资源元素类似,在测试阶段使用。测试资源目录默认是${basedir}/src/test/resources。测试资源不会被部署。 -->
</testResource>
<!-- <testResource></testResource> -->
</testResources>
</build>
5.3 plugins元素
- Plugins:设置构建的插件
<!-- 插件 -->
<build>
....
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<!-- true或者false,默认是false。是否加载此插件的扩展。-->
<extensions>false</extensions>
<!-- true或false,默认值为true。这个plugin的配置是否应该应用于该pom孩子的pom中。 -->
<inherited>true</inherited>
<!-- 配置该插件希望得到的参数 -->
<configuration></configuration>
<!-- 改变插件的依赖 -->
<dependencies></dependencies>
</plugin>
<!-- 关于executions的配置 -->
<!--plugin可以有多个目标,每一个目标都可以有一个分开的配置,可以将一个plugin绑定到不同的阶段-->
<!--假如绑定antrun:run目标到verify阶段-->
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<!--标识,用于和其他execution区分。-->
<!--当这个阶段执行时,它将以这个形式展示[plugin:goal execution: id]。-->
<!--在这里为: [antrun:run execution: echodir]-->
<id>echodir</id>
<!--目标列表-->
<goals>
<goal>run</goal>
</goals>
<!--目标执行的阶段-->
<phase>verify</phase>
<!--子类pom是否继承-->
<inherited>false</inherited>
<!--在指定目标下的配置-->
<configuration>
<tasks>
<echo>Build Dir: ${project.build.directory}</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
5.5 pluginManagement 元素
pluginManagement的配置和plugins的配置是一样的,只是用于继承,使得可以在孩子pom中使用。
- 父pom:
<build>
...
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>pre-process-classes</id>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>pre-process</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
...
</build>
- 子pom
<build>
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
</plugins>
...
</build>
这样大大简化了孩子pom的配置
5.4 profiles 元素
对于profile没有深入了解,暂时copy一份,占个位置。
<build>
....
<!-- 项目能够根据构建环境更改设置。 -->
<profiles>
<!-- 一个 profile 元素包含一个激活条件和激活后将对POM的修改。 -->
<profile>
<id>test</id>
<activation></activation>
<!-- "Profile Build" 是 "Project Build" 的一个子集 -->
<build></build>
</profile>
<profile>
<id>product</id>
<activation></activation>
<!-- "Profile Build" 是 "Project Build" 的一个子集 -->
<build></build>
</profile>
</profiles>
</build>
除了上面写的,pom.xml 还有很多其他的配置:
- 分发配置
- 仓库配置
- 报表配置
- 环境配置
- 项目信息配置
详见这位作者写的:https://blog.youkuaiyun.com/u012152619/article/details/51485297
一篇很详细的pom.xml说明博客:
http://outofmemory.cn/maven/dive-into-pom.xml-file
两个查找jar坐标的网站:
http://www.mvnrepository.com/
https://search.maven.org/