转自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. lua语言初探

    写在最前面 <cocos2d-x lua核心编程>是我首次购买电子书,坑的就不谈了,书里的代码部分基本上不是少空格就是多换行,让阅读变得十分困难. 所以又购买了实体书,加上看一些大佬视频和 ...

  2. 第一册:lesson forty five。

    原文: The boss's letter. A:Can you come here a minute please,Bob? B:Yes,sir. A:Where is C? B:She is ne ...

  3. [android] 隐式意图激活另外一个activity

    随着api的升级,系统的很多应用包名和类名都改掉了,所以很多时候,打开系统应用的时候会报错,隐式意图就是解决组件之间松耦合,描述动作行为 获取Intent对象,通过new出来 调用Intent对象的s ...

  4. [android] 内容观察者

    拦截短信,比如当发短信的时候,就把短信读取出来,当系统的短信发生变化的时候,大叫一声,把数据发送到公共的消息邮箱里面,我们的应用通过内容观察者观察公共的消息邮箱 获取ContentResolver对象 ...

  5. mybatis_ The content of element type association must match (constructor,id,result,ass ociation,collection,discriminator)

    一般遇到这种问题肯定要看一看association中元素编写顺序, <resultMap id="orderRslMap" type="orders"&g ...

  6. js动画 Css提供的运动 js提供的运动

    1.     动画 (1)      Css样式提供了运动 过渡的属性transition  从一种情况到另一种情况叫过渡 Transition:attr  time  linear  delay: ...

  7. Java岗 面试考点精讲(基础篇01期)

    即将到来金三银四人才招聘的高峰期,渴望跳槽的朋友肯定跟我一样四处找以往的面试题,但又感觉找的又不完整,在这里我将把我所见到的题目做一总结,并尽力将答案术语化.标准化.预祝大家面试顺利. 术语会让你的面 ...

  8. 设计模式—模板方法的C++实现

    这是Bwar在2009年写的设计模式C++实现,代码均可编译可运行,一直存在自己的电脑里,曾经在团队技术分享中分享过,现搬到线上来. 1. 模板方法简述 1.1 目的 定义一个操作中的算法骨架,而将一 ...

  9. 如何处理Express异常?

    译者按:根据墨菲定律:“有可能出错的事情,就会出错”.那么,既然代码必然会出错,我们就应该处理好异常. 原文: How to handle errors in Express 译者:Fundebug ...

  10. mapper代理查询

    对于查询来说,要根据具体的业务,来指定mapper接口中方法的返回值类型1:如果只返回一条记录,mapper接口中方法的返回值类型应指定为pojo类型或其他简单类型,这样mybatis内部就会使用se ...