什么是 POM?

POM (Project Object Model) 项目对象模型。它是一个XML文件,其中包含有关Maven用于构建项目的项目和配置细节的信息。它包含大多数项目的默认值。例如,构建项目的目录:target;java源码文件目录: src/main/java;测试java源码文件目录: src/test/java;等等。当执行任务或目标时,Maven将在当前目录中查找POM,它读取POM。获取所需要的配置信息,然后执行目标。

POM中可以指定项目的依赖,可以执行的插件或目标,构建配置文件等。其他信息,如项目版本,说明,开发人员,邮件列表等也可以指定。

POM 结构

这是直接在POM的项目元素下列出的元素。注意modelVersion为4.0.0。这是目前唯一支持Maven 2和3的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
http://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>

Maven 坐标

groupId:artifactId:version三个字段是Maven必不可少的字段,这三个字段的作用非常类似与一个地址和时间戳。这标志着存储库中特定位置,就像Maven项目的坐标系统一样。

  • groupId: 这是组织或则项目独一无二的ID值。
  • artifactId: 是项目的名称,把groupId进行却分开来。
  • version: 指项目的当前版本。
  • packaging: 如果没有指定默认是jar,我们可以设置为pom, jar, maven-plugin, ejb, war, ear, rar, par等,如果设置为war则会将项目最终打包成war包。
  • classifier: 你有时候会在项目中看到这个熟悉,这样项目显示为groupId:artifactId:packaging:classifier:version。

Maven 依赖

Maven的基石就是它的依赖列表,大多数项目都是依赖其它构建才能正常运行。如下是一个简单依赖。

 <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">
...
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<type>jar</type>
<scope>test</scope>
<optional>true</optional>
</dependency>
...
</dependencies>
...
</project>
  • groupId、artifactId、version: 如上一节所述,这里可以计算出项目的坐标,并将其引入到项目依赖中。
  • classifier: 这是一个可选参数。
  • type: 依赖项目的包装类型,默认为jar。
  • scope: 该元素用来引入到当前项目的类路径(编译、测试或运行等),以及限制依赖的传递性。总共有如下五个范围:
    • compile:默认值,编译期。依赖项可以在所有类路径中使用,且会传递到子项目。
    • provided:只可以在编译和测试中使用,切不会传递给子项目。
    • runtiome:在项目测试和运行类路径中使用,不会在编译中使用。
    • test:只可以在测试中使用,不会传递子项目。
    • system:这个和provided非常类似,工件始终可以使用,并且不会存储库中查找。
  • systemPath:只有当scope为system时,需要指定JAR的绝对路径。
  • optional:可选元素,如果optional为true时,如果A依赖B,B依赖D,如果A没有显示的引入D,则A不会依赖D。

exclusions可以显示的排出依赖项中传递的依赖项目。例如maven-embedder需要依赖maven-core,但我们不希望引入maven-core依赖项目,那么我们可以显示的排除这个项目。如下:

 <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">
...
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-embedder</artifactId>
<version>2.0</version>
<exclusions>
<exclusion>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
</exclusion>
</exclusions>
</dependency>
...
</dependencies>
...
</project>

这样你的项目中不会有maven-core依赖项。

Super POM

Super POM 是Maven默认的POM,除非明确设置,否则所有的POM都会扩展Super POM,这意味着您为项目创建的POM继承Super POM。下面的代码是Maven 2.0.x 的 Super POM。

 <project>
<modelVersion>4.0.0</modelVersion>
<name>Maven Default Project</name> <repositories>
<repository>
<id>central</id>
<name>Maven Repository Switchboard</name>
<layout>default</layout>
<url>http://repo1.maven.org/maven2</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories> <pluginRepositories>
<pluginRepository>
<id>central</id>
<name>Maven Plugin Repository</name>
<url>http://repo1.maven.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
</pluginRepository>
</pluginRepositories> <build>
<directory>target</directory>
<outputDirectory>target/classes</outputDirectory>
<finalName>${artifactId}-${version}</finalName>
<testOutputDirectory>target/test-classes</testOutputDirectory>
<sourceDirectory>src/main/java</sourceDirectory>
<scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
</build> <reporting>
<outputDirectory>target/site</outputDirectory>
</reporting> <profiles>
<profile>
<id>release-profile</id> <activation>
<property>
<name>performRelease</name>
</property>
</activation> <build>
<plugins>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId> <executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId> <executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId> <configuration>
<updateReleaseInfo>true</updateReleaseInfo>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles> </project>

下面是Maven 2.1.x 的Super POM

 <project>
<modelVersion>4.0.0</modelVersion>
<name>Maven Default Project</name> <repositories>
<repository>
<id>central</id>
<name>Maven Repository Switchboard</name>
<layout>default</layout>
<url>http://repo1.maven.org/maven2</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories> <pluginRepositories>
<pluginRepository>
<id>central</id>
<name>Maven Plugin Repository</name>
<url>http://repo1.maven.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
</pluginRepository>
</pluginRepositories> <build>
<directory>${project.basedir}/target</directory>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<finalName>${project.artifactId}-${project.version}</finalName>
<testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<!-- TODO: MNG-3731 maven-plugin-tools-api < 2.4.4 expect this to be relative... -->
<scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>${project.basedir}/src/test/resources</directory>
</testResource>
</testResources>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-2</version>
</plugin>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.0</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.4</version>
</plugin>
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<version>2.3.1</version>
</plugin>
<plugin>
<artifactId>maven-ejb-plugin</artifactId>
<version>2.1</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.5</version>
</plugin>
<plugin>
<artifactId>maven-plugin-plugin</artifactId>
<version>2.4.3</version>
</plugin>
<plugin>
<artifactId>maven-rar-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.0-beta-8</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.3</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>2.0-beta-7</version>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>2.0.4</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.3</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1-alpha-2</version>
</plugin>
</plugins>
</pluginManagement>
</build> <reporting>
<outputDirectory>${project.build.directory}/site</outputDirectory>
</reporting>
<profiles>
<profile>
<id>release-profile</id> <activation>
<property>
<name>performRelease</name>
<value>true</value>
</property>
</activation> <build>
<plugins>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<updateReleaseInfo>true</updateReleaseInfo>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles> </project>

最小的 POM

POM最低要求如下:

  • project 根节点
  • modelVersion 需要设置为4.0.0
  • groupId 项目组的标识(建议使用网站的倒序)
  • artifactId 项目名称
  • version 项目的版本信息,这里提一下(SNAPSHOT 和 RELEASE),在项目中常常看到这两个单词。SNAPSHOT 代表不稳定版,常常属于开发中的项目,而RELEASE稳定版。

下面是已个最小POM的简单例子:

 <project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.cnblogs.hello</groupId>
<artifactId>hello</artifactId>
<version>1.0.SNAPSHOT</version>
</project>

POM要求配置groupId,artifactId 和 version,这三个值构成了项目完全限定的项目名称,就是<groupId>:<artifactId>:<version>,至于上面的例子,其完全限定的项目名称是"com.cnblogs.hello:hello:1.0.SNAPSHOP",其本地仓库对应的路径为:com/cnblogs/hello/hello/1.0.SNAPSHOT/hello-1.0.SNAPSHOT.jar

另外,如果配置文件未指定详细信息,Maven将使用默认值。每个Maven项目都有一个packaging类型。如果没有在POM中指定,那么就会使用默认值“jar”。

此外,正如你看到的,在最小POM中,repositories存储库也没有被指定,如果使用最小POM构建项目,它将继承Super POM中的repositories配置的存储库。

继承

项目继承会合并下列元素:

  • dependencies : 依赖
  • developers 和 contributors :  开发者和贡献者
  • plugin lists (including reports) : 插件列表(包括报表)
  • plugin executions with matching ids : 匹配ids的执行插件
  • plugin configuration : 插件配置
  • resources : 资源

Super POM是项目继承的一个例子,但是您也可以通过在POM中指定父元素来引入您自己的父POM,如以下示例所示。

 <project>
<parent>
<groupId>com.cnblogs.hello</groupId>
<artifactId>hello</artifactId>
<version>1.0.SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.cnbligs.hello</groupId>
<artifactId>app</artifactId>
<version>1.0.SNAPSHOT</version>
</project>

上面示例是将最小POM指定为app的父POM。指定父POM我需要使用完全限定名指定(即需要使用groupId,artifactID 和 version),通过这个设置,我们的模块现在可以继承父POM的一些属性。

或者,如果我们希望groupId 和 version 和父POM的保持一致,则可以将在POM删除groupId和version,如下所示:

 <project>
<parent>
<groupId>com.cnblogs.hello</groupId>
<artifactId>hello</artifactId>
<version>1.0.SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>app</artifactId>
</project>

这里注意下parent中的relativePath,如果父POM不是在该POM的上一级目录,就必须配置这个属性并定位到父POM中。relativePath的默认值为../pom.xml。

聚合

项目聚合跟项目继承有些类似,但不是从POM中指定父POM,而是从父POM中指定POM。通过这样做,父项目现在知道它所有的子项目,并且对父项目执行Maven命令,那么Maven命令也会执行到所有的子项目中。要做一个项目聚合,必须执行以下操作:

  • 改变父POM的packaging属性为pom。
  • 在父POM中指定子模块(子项目)。

将如上两个POM做如下修改。

com.cnblogs.hello:app:1.0.SNAPSHOT

 <project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.cnbligs.hello</groupId>
<artifactId>app</artifactId>
<version>1.0.SNAPSHOT</version>
</project>

com.cnblogs.hello:hello:1.0.SNAPSHOT

 <project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.cnblogs.hello</groupId>
<artifactId>hello</artifactId>
<version>1.0.SNAPSHOT</version>
<!-- 指定为父POM,project 聚合的第一步 -->
<packaging>pom</packaging> <!-- 添加子模块 根据POM的相对路径进行指定。 -->
<modules>
<module>../app</module>
</modules>
</project>

现在,每当Maven命令处理hello项目时,通过的Maven命令也会针对app项目运行。

继承 和 聚合

如果你有多个Maven项目,并且他们都具有相似的配置,则可以通过抽出相似的配置并制作父项目来构建。因此,您所要做的就是让你的Maven项目继承该父项目,然后将这些配置应用与所有的这些项目。

如果你有一组构建或一起处理的项目。你可以创建一个父项目并让该父项目将这些子项目声明进来。通过这样做,你只需要构建父项目,其余的也随之而来。

当然,你也可以同时拥有继承和聚合。意思是说,你可以让你的模块指定一个父项目,同时让这个父项目指定这些Maven作为它的子模块。你只需要符合以下三条规则:

  • 在每一个子POM中指定他们的父POM是谁。
  • 在父POM中将packaging的值改为pom。
  • 在父POM中指定其所有的子项目模块(子POM)。

参考一下代码示例:

com.cnblogs.hello:app:1.0.SNAPSHOT

 <project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.cnblogs.hello</groupId>
<artifactId>hello</artifactId>
<version>1.0.SNAPSHOT</version>
<packaging>pom</packaging> <modules>
<module>../app</module>
</modules>
</project>

com.cnblogs.hello:app:1.0.SNAPSHOT

 <project>
<parent>
<groupId>com.cnblogs.hello</groupId>
<artifactId>hello</artifactId>
<version>1.0.SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.cnbligs.hello</groupId>
<artifactId>app</artifactId>
<version>1.0.SNAPSHOT</version>
</project>

变量

在某些情况下,您需要在多个不同位置使用相同的值。为了帮助确保值仅指定一次,Maven允许您在POM中使用自己的和预定义的变量。例如如下示例:

<!-- 首先在properties标签中自定义 project.version 属性 值为 1.0.SANPSHOT  在后续的代码中可以直接使用${project.version}获取值 -->
<properties>
<project.version>1.0.SNAPSHOT</project.version>
</properties> <!-- version可以这样写 -->
<version>${project.version}</version>

其实properties标签不仅仅是只能声明版本号,凡是需要统一声明后再引用的场合都可以使用。

Apache Maven(三):POM的更多相关文章

  1. Failure to transfer org.apache.maven:maven-archiver:pom:2.5 from http://repo.maven.apache.org/ maven2 was cached in the local repository, resolution will not be reattempted until the update interv

    Failure to transfer org.apache.maven:maven-archiver:pom:2.5 from http://repo.maven.apache.org/  mave ...

  2. Failure to transfer org.apache.maven:maven-archiver:pom:2.5 from https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval o

    pom.xml报错: Failure to transfer org.apache.maven:maven-archiver:pom:2.5 from https://repo.maven.apach ...

  3. 【原创】大叔经验分享(10)Could not transfer artifact org.apache.maven:maven. from/to central. Received fatal alert: protocol_version

    maven编译工程报错 [ERROR] Failed to execute goal net.alchim31.maven:scala-maven-plugin:3.2.2:add-source (s ...

  4. maven项目建立pom.xml报无法解析org.apache.maven.plugins:maven-resources-plugin:2.4.3

    一.发现问题 建立maven项目后,pom.xml在显示红叉.鼠标放上去,显示Executiondefault-testResources of goalorg.apache.maven.plugin ...

  5. Maven 问题 Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:3.0.0-M1 的处理

    一.问题描述 Maven项目报错,该项目是导入的项目,然后再通过开发工具打开项目时,pom.xml文件报错. 并且新建Maven Project 也会报错. 二.报错详细Failure to tran ...

  6. eclipse加载maven工程提示pom.xml无法解析org.apache.maven.plugins:maven-resources-plugin:2.4.3解决方案

    pom文件提示信息: Failure to transfer org.apache.maven.plugins:maven-resources-plugin:pom:2.4.3 from http:/ ...

  7. pom文件报错org.apache.maven.archiver.mavenarchiver.getmanifest

    eclipse导入新的maven项目时,pom.xml第一行报错: org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.mav ...

  8. 解决Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.17 问题

    详细报错如下 Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.17 from http://maven ...

  9. Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.12.4

    Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.12.4 from https://repo.mave ...

随机推荐

  1. angular怎么样注销事件

    angular怎么样注销事件 $scope.$on("$destroy", function() { //清除配置,不然scroll会重复请求 }) 在Controller中监听$ ...

  2. python 序列化,常用模块

    生成器与迭代器 生成器 在 Python 中,使用了 yield 的函数被称为生成器(generator). 跟普通函数不同的是,生成器是一个返回迭代器的函数,只能用于迭代操作,更简单点理解生成器就是 ...

  3. 自整定模糊PID算法的理论

    模糊控制系统的构成与与常规的反馈控制系统的主要区别在于控制器主要是由模糊化,模糊推理机和精确化三个功能模块和知识库(包括数据库和规则库)构成的.具体实现过程如下所示: (1)预处理: 输入数据往往是通 ...

  4. MyISAM引擎表出现“Error 'Incorrect key file for table”

    mysql主从复制中的从库突然出现了警报,sql_thread停止了,show slave status\G;查看 mysql> show slave status\G ; . row **** ...

  5. 出租WiFi到底靠不靠谱?

    创业是一种心态,也是不断的探索,他融入我们的生活,从日常中积累,从小微处启航. 一.背景交代 最近在换工作,本周搬到新租的单身公寓,空间不大,倒是干净整洁.委托租房中介帮忙开通宽带,告知是电信网最低开 ...

  6. 设计模式——简单工厂模式(SimpleFactory Pattern)

    最近做项目总是遇到这样或者那样的问题,代码不够简洁,代码可扩展性不够好,耦合度又太高了,导致经常有种想去重构又无从下手的感觉. 无意间翻出了之前买的一本书<大话设计模式>读了一遍,受益匪浅 ...

  7. Android(java)学习笔记19:Java中InetAddress类概述和使用

    1. 要想让网络中的计算机能够互相通信,必须为每台计算机指定一个标识号,通过这个标识号来指定要接受数据的计算机和识别发送的计算机. 在TCP/IP协议中,这个标识号就是IP地址. 那么,我们如果获取和 ...

  8. 关于SessionFactory的不同实现类分别通过getCurrentSession()方法 和 openSession() 方法获取的Session对象在保存对象时的一些区别

    一.单向多对一关联关系 一).使用LocalSessionFactoryBean类,即在applicationContext中配置的 <!-- 配置SessionFactory 使用LocalS ...

  9. SQLServer用存储过程实现插入更新数据

    实现 1)有同样的数据,直接返回(返回值:0): 2)有主键同样,可是数据不同的数据,进行更新处理(返回值:2): 3)没有数据,进行插入数据处理(返回值:1). [创建存储过程] Create pr ...

  10. POJ 3308 最少点集覆盖

    题意:和Uva 11419 类似. 首先最少点集覆盖 = 最大匹配. 我们可以在 S 和行 的边 不是1,有了权值,但是题意要求的是乘积最小,那么可以用 log(a*b) = loga + logb ...