转自https://blog.csdn.net/jariwsz/article/details/19554137

我们先看一个简单的例子:

 <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">
<!-- maven model version -->
<modelVersion>4.0.0</modelVersion>
<!-- project group id & artifact id -->
<groupId>com.freesoft.mvn-webapp</groupId>
<artifactId>mvnwebapp</artifactId>
<!-- packing type -->
<packaging>war</packaging>
<!-- version -->
<version>1.0-SNAPSHOT</version>
<name>mvnwebapp Maven Webapp</name>
<url>http://maven.apache.org</url> <dependencies> <!-- JUnit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency> </dependencies> <build>
<finalName>mvnwebapp</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<tomcat-url>http://localhost:8080/manager/html</tomcat-url>
<server>tomcat_localtest</server>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build> <properties>
<struts.version>2.3.15</struts.version>
<mysql.version>5.1.29</mysql.version>
<hibernate.version>4.3.1.Final</hibernate.version>
</properties>
</project>

下面分段讲解。

1. 基本信息

modelVersion Maven模块版本,目前我们一般都取值4.0.0
groupId 整个系统的名称。
artifactId 子模块名称。
packaging 打包类型,可取值:jar,war等等,这个配置用于package的phase,具体可以参见package运行的时候启动的plugin,后面有机会我们会讲述如何配置打包的插件。

2. dependencies

依赖关系。实际上pom之间存在好三种关系:继承、依赖、聚合。我们先讲依赖,这也是最重要的关系。

groupId 依赖项的groupId
artifactId 依赖项的artifactId
version 依赖项的版本
scope 依赖项的适用范围:

  • compile,缺省值,适用于所有阶段,会随着项目一起发布。
  • provided,类似compile,期望JDK、容器或使用者会提供这个依赖。如servlet.jar。
  • runtime,只在运行时使用,如JDBC驱动,适用运行和测试阶段。
  • test,只在测试时使用,用于编译和运行测试代码。不会随项目发布。
  • system,类似provided,需要显式提供包含依赖的jar,Maven不会在Repository中查找它。

之前例子里的junit就只用在了test中。

exclusions 排除项目中的依赖冲突时使用。

2.1 关于排除依赖冲突

我们可能经常会遇到这样一个问题:我们的项目有两个依赖项:A & B,而且A和B同时依赖了C,但不是同一个版本。那么我们怎么办呢?

2.1.1 添加检查插件

 <reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
</plugin>
</plugins>
</reporting>

然后运行:mvn project-info-reports:dependencies,来查看依赖项报告。

2.1.2 去除依赖项

如果我们需要在某个dependency中去除某个依赖项,直接这样即可:

 <!-- Struts2 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts.version}</version>
<exclusions>
<exclusion>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</exclusion>
</exclusions>
</dependency>

3. 继承

我的repository下面有个例子就直接拿来用了:

 <modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream-parent</artifactId>
<version>1.4.3</version>
</parent>
<artifactId>xstream</artifactId>
<packaging>jar</packaging>
<name>XStream Core</name>

其中的parent表示父pom是com.thoughtworks.xstream的xstream-parent的1.4.3版本。继承关系比较简单,这里不做过多介绍。

4. 聚合

我们可以通过一个大的项目来整合各个小的模块:

 <modules>
<module>my-app</module>
</modules>

5. 属性

属性表述类似于EL表达式,ANT中也同样有,所以我们的properties字段可以这样使用:

         <!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>

6. 构建

6.1 plugin

Plugin的配置如下:

 <pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<tomcat-url>http://localhost:8080/manager/html</tomcat-url>
<server>tomcat_localtest</server>
</configuration>
</plugin>
</plugins>
</pluginManagement>

我们可以看到同样要哟偶groupId、artifactId、version还有一些配置参数。

6.2 resource

指定你在Build时需要的资源文件:

 <resources>
<resource>
<targetPath>WEB-INF/resource</targetPath>
<!-- 不对文件中的表达式进行处理 -->
<filtering>false</filtering>
<directory>${basedir}/src/test/resources</directory>
<includes>
<include>include.xml</include>
</includes>
<excludes>
<exclude>exclude.xml</exclude>
</excludes>
</resource>
</resources>

Maven中POM.XML详解的更多相关文章

  1. 【maven】 pom.xml详解

    pom.xml详解 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...

  2. Maven学习总结(15)——Maven 项目中pom.xml详解

    <project xmlns="http://maven.apache.org/POM/4.0.0"  xmlns:xsi="http://www.w3.org/2 ...

  3. JavaEE学习之Maven配置文件pom.xml详解(转)

    一.引言 (本文转载自:http://blog.csdn.net/longeremmy/article/details/9670619) 使用maven有一些时间了,一直没有好好将pom配置文件每个节 ...

  4. Maven配置文件Pom.xml详解

    <project xmlns="http://maven.apache.org/POM/4.0.0 "      xmlns:xsi="http://www.w3. ...

  5. Maven项目POM.xml详解

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  6. maven的pom.xml详解

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  7. 【转】maven核心,pom.xml详解

    感谢如下博主: http://www.cnblogs.com/qq78292959/p/3711501.html maven核心,pom.xml详解 什么是pom?    pom作为项目对象模型.通过 ...

  8. Maven配置文件setting.xml详解

    注:本文来源于:大话JAVA的那些事 <Maven配置文件setting.xml详解> <?xml version="1.0" encoding="UT ...

  9. Maven项目中的pom.xml详解【转】

    什么是pom? pom作为项目对象模型.通过xml表示maven项目,使用pom.xml来实现.主要描述了项目:包括配置文件:开发者需要遵循的规则,缺陷管理系统,组织和licenses,项目的url, ...

随机推荐

  1. [转]RPA流程自动化-Blueprism认证考试介绍

    本文转自:https://www.cnblogs.com/digod/p/9190186.html RPA流程自动化-Blueprism认证考试介绍 接触RPA有一段时间了,几种RPA相关工具也都试用 ...

  2. 人工智能第三课:数据科学中的Python

    我用了两天左右的时间完成了这一门课<Introduction to Python for Data Science>的学习,之前对Python有一些基础,所以在语言层面还是比较顺利的,这门 ...

  3. Tomcat 8005/8009/8080/8443端口的作用

    --关闭tomcat进程所用.当执行shutdown.sh关闭tomcat时就是连接8005端口执行“SHUTDOWN”命令--由此,我们直接telnet8005端口执行“SHUTDOWN”(要大写, ...

  4. [PHP] 算法-数值的整数次方的PHP实现

    给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 思路: 1.指数的二进制表达10^6次方 可以表示10^110(二进制) 10^100 ...

  5. php 函数小技巧(一)

    密码加密与验证 password_hash — 创建密码的哈希(hash) string password_hash ( string $password , integer $algo [, arr ...

  6. go里面的指针用法

    什么是指针 指针是存储一个变量的内存地址的变量. 在上图中,变量 b 的值是 156,存储在地址为 0x1040a124 的内存中.变量 a 存储了变量 b 的地址.现在可以说 a 指向 b. 指针的 ...

  7. 去除bootstrap默认的input和选中时的样式

    input默认样式除border外, 还有一个阴影效果box-shadow:选中时同样有阴影效果. input,input:focus{ border: none !important; box-sh ...

  8. Testlink1.9.17使用方法(第一章 前言)

    第一章 前言 QQ交流群:585499566 一.Testlink主要功能: 测试项目管理 测试需求管理 测试用例管理 测试计划的制定 测试用例对测试需求的覆盖管理 测试用例的执行 大量测试数据的度量 ...

  9. video 标签在微信浏览器的问题解决方法

    最近做的些web页面,内嵌许多小视频,在ios和安卓手机上播放时,遇到不少问题: 在微信浏览器内播放时,视频会自动全屏 如果每个视频都有一张自定义的图片作为封面,在显示视频的同时,如果没有给这个视频设 ...

  10. selenium的基本用法

    selenium需要配合一个driver  我使用的是chrome的driver 注意一定要下载对应浏览器版本的driver 否则会报错的 http://chromedriver.storage.go ...