1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3 <!-- maven model version -->
4 <modelVersion>4.0.0</modelVersion>
5 <!-- project group id & artifact id -->
6 <groupId>com.freesoft.mvn-webapp</groupId>
7 <artifactId>mvnwebapp</artifactId>
8 <!-- packing type -->
9 <packaging>war</packaging>
10 <!-- version -->
11 <version>1.0-SNAPSHOT</version>
12 <name>mvnwebapp Maven Webapp</name>
13 <url>http://maven.apache.org</url>
14
15
16 <dependencies>
17
18 <!-- JUnit -->
19 <dependency>
20 <groupId>junit</groupId>
21 <artifactId>junit</artifactId>
22 <version>4.11</version>
23 <scope>test</scope>
24 </dependency>
25
26 </dependencies>
27
28
29 <build>
30 <finalName>mvnwebapp</finalName>
31 <pluginManagement>
32 <plugins>
33 <plugin>
34 <groupId>org.apache.tomcat.maven</groupId>
35 <artifactId>tomcat7-maven-plugin</artifactId>
36 <version>2.1</version>
37 <configuration>
38 <tomcat-url>http://localhost:8080/manager/html</tomcat-url>
39 <server>tomcat_localtest</server>
40 </configuration>
41 </plugin>
42 </plugins>
43 </pluginManagement>
44 </build>
45
46 <properties>
47 <struts.version>2.3.15</struts.version>
48 <mysql.version>5.1.29</mysql.version>
49 <hibernate.version>4.3.1.Final</hibernate.version>
50 </properties>
51 </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 添加检查插件

1 <reporting>
2 <plugins>
3 <plugin>
4 <groupId>org.apache.maven.plugins</groupId>
5 <artifactId>maven-project-info-reports-plugin</artifactId>
6 </plugin>
7 </plugins>
8 </reporting>

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

2.1.2 去除依赖项

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

 1 <!-- Struts2 -->
2 <dependency>
3 <groupId>org.apache.struts</groupId>
4 <artifactId>struts2-core</artifactId>
5 <version>${struts.version}</version>
6 <exclusions>
7 <exclusion>
8 <groupId>org.freemarker</groupId>
9 <artifactId>freemarker</artifactId>
10 </exclusion>
11 </exclusions>
12 </dependency>

3. 继承

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

1 <modelVersion>4.0.0</modelVersion>
2 <parent>
3 <groupId>com.thoughtworks.xstream</groupId>
4 <artifactId>xstream-parent</artifactId>
5 <version>1.4.3</version>
6 </parent>
7 <artifactId>xstream</artifactId>
8 <packaging>jar</packaging>
9 <name>XStream Core</name>

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

4. 聚合

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

1 <modules>
2 <module>my-app</module>
3 </modules>

5. 属性

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

1 <!-- mysql -->
2 <dependency>
3 <groupId>mysql</groupId>
4 <artifactId>mysql-connector-java</artifactId>
5 <version>${mysql.version}</version>
6 </dependency>

6. 构建

6.1 plugin

Plugin的配置如下:

 1 <pluginManagement>
2 <plugins>
3 <plugin>
4 <groupId>org.apache.tomcat.maven</groupId>
5 <artifactId>tomcat7-maven-plugin</artifactId>
6 <version>2.1</version>
7 <configuration>
8 <tomcat-url>http://localhost:8080/manager/html</tomcat-url>
9 <server>tomcat_localtest</server>
10 </configuration>
11 </plugin>
12 </plugins>
13 </pluginManagement>

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

6.2 resource

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

 1 <resources>
2 <resource>
3 <targetPath>WEB-INF/resource</targetPath>
4 <!-- 不对文件中的表达式进行处理 -->
5 <filtering>false</filtering>
6 <directory>${basedir}/src/test/resources</directory>
7 <includes>
8 <include>include.xml</include>
9 </includes>
10 <excludes>
11 <exclude>exclude.xml</exclude>
12 </excludes>
13 </resource>
14 </resources>

maven pom.xml配置文件详解的更多相关文章

  1. Maven pom.xml文件详解

    Maven pom.xml文件详解 一.简介 POM全称是Project Object Model,即项目对象模型. pom.xml是maven的项目描述文件,它类似与antx的project.xml ...

  2. Maven的pom.xml配置文件详解

    Maven简述 Maven项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具. Maven 除了以程序构建能力为特色之外,还提供高级项目管理工具.由于 Mav ...

  3. (转)Maven pom.xml 配置详解

    背景:maven一直感觉既熟悉又陌生,归根结底还是自己不太熟. 1 什么是pom? pom作为项目对象模型.通过xml表示maven项目,使用pom.xml来实现.主要描述了项目:包括配置文件:开发者 ...

  4. Maven(四-2) Maven pom.xml 配置详解

    转载于:http://niuzhenxin.iteye.com/blog/2042102 什么是pom?    pom作为项目对象模型.通过xml表示maven项目,使用pom.xml来实现.主要描述 ...

  5. Maven之(七)pom.xml配置文件详解

    setting.xml主要用于配置maven的运行环境等一系列通用的属性,是全局级别的配置文件:而pom.xml主要描述了项目的maven坐标,依赖关系,开发者需要遵循的规则,缺陷管理系统,组织和li ...

  6. Maven之pom.xml配置文件详解

    此文非原创,摘自:https://www.baidu.com/link?url=GlGgW21nijIiULDZj0RfPH8ofqGMqEnAzXiym7O3hfrZM5nFH2enukemBNTX ...

  7. [转]Maven之(七)pom.xml配置文件详解

    原文地址:https://blog.csdn.net/u012152619/article/details/51485297 setting.xml主要用于配置maven的运行环境等一系列通用的属性, ...

  8. Maven中的pom.xml配置文件详解

    原文:http://blog.csdn.net/u012152619/article/details/51485297 <project xmlns="http://maven.apa ...

  9. 7.Maven之(七)pom.xml配置文件详解

    转自:https://blog.csdn.net/qq_33363618/article/details/79438044 setting.xml主要用于配置maven的运行环境等一系列通用的属性,是 ...

随机推荐

  1. 剑指offer(21)栈的压入、弹出序列

    题目描述 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序 ...

  2. Server.Transfer VS Response.Redirect – Simplified

    https://www.codeproject.com/Articles/775221/Server-Transfer-VS-Response-Redirect-Simplified Introduc ...

  3. Vue:(一)概况

    Vue:https://cn.vuejs.org/ (一)Vue概况 Vue本身并不是一个框架 Vue结合周边生态构成一个灵活的.渐进式框架 声明式渲染 组件系统 客户端路由 状态管理 构建工具 (二 ...

  4. SpingBoot全局异常处理器被覆盖的解决办法

    @controllerAdvice()注解 @ControllerAdvice()注解可以定义一个统一的异常处理类,我们可以定义多个统一异常处理类, 但这里我们需要注意一点,默认的@Controlle ...

  5. 20175317 《Java程序设计》第五周学习总结

    20175317 <Java程序设计>第五周学习总结 教材学习内容总结 第五周我学习了教材第六章的内容,了解了接口的知识,学到了以下内容: 明白了什么是接口 学会了如何实现接口 了解了接口 ...

  6. [easyUI] 树形菜单 tree

    0.效果图 1. 一个id为mytree的无序列表 <h2>easy UI Tree</h2> <ul id="mytree"></ul& ...

  7. org.apache.hadoop.mapreduce.lib.input.InvalidInputException: Input path does not exist: file:/input

    原我是这样写的 //输入数据所在的文件目录 FileInputFormat.addInputPath(job, new Path("/input/")); //mapreduce执 ...

  8. jQuery中防止表单提交两次的方法

    遇到过表单提交两次的情况,做个记录: 解决场景:首先是表单验证,其次是防止多次提交表单: jQuery中插件:validate_submitHandler_plugin,具体的可以使用关键字搜索: 使 ...

  9. win2012R2安装net4.6.2失败提示“更新2919355包问题,或者win8.1、win10”的错误

    前言 在客户的服务器电脑安装net4.6,提示安装失败错误,最后顺利成功安装net4.6. 一.错误 1.win2012R2安装net4.6.2失败提示“更新2919355包问题,或者win8.1.w ...

  10. vue2.0 导出Excel表格数据

    1.安装三个依赖包 npm install -S file-saver npm install -S xlsx npm install -D script-loader 2.在项目中创建一个文件夹(比 ...