Maven里头的pom.xml配置详解
正常的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/maven-v4_0_0.xsd">
<!-- maven model version -->
<modelVersion>4.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.</struts.version>
<mysql.version>5.1.</mysql.version>
<hibernate.version>4.3..Final</hibernate.version>
</properties>
</project>
1. 基本信息
modelVersion | Maven模块版本,目前我们一般都取值4.0.0 |
groupId | 整个系统的名称。 |
artifactId | 子模块名称。 |
packaging | 打包类型,可取值:jar,war等等,这个配置用于package的phase,具体可以参见package运行的时候启动的plugin,后面有机会我们会讲述如何配置打包的插件。 |
2.依赖
依赖关系。实际上pom之间存在好三种关系:继承、依赖、聚合。我们先讲依赖,这也是最重要的关系。
groupId | 依赖项的groupId |
artifactId | 依赖项的artifactId |
version | 依赖项的版本 |
scope | 依赖项的适用范围:
之前例子里的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.</modelVersion>
<parent>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream-parent</artifactId>
<version>1.4.</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配置详解的更多相关文章
- Maven系列一pom.xml 配置详解
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- 【maven学习】pom.xml文件详解
环境 apache-maven-3.6.1 jdk 1.8 eclipse 4.7 POM是项目对象模型(Project Object Model)的简称,它是Maven项目中的文件,使用XML表示, ...
- Maven使用笔记(四)pom.xml配置详解
pom.xml文件配置详解 --声明规范 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=" ...
- 【转】Maven pom.xml 配置详解
原文链接:https://yq.aliyun.com/articles/38271 pom.xml文件配置详解 --声明规范 <project xmlns="http://maven. ...
- Maven项目pom.xml配置详解
maven项目pom.xml文件配置详解,需要时可以用作参考: <project xmlns="http://maven.apache.org/POM/4.0.0" xmln ...
- 学习笔记——Maven pom.xml配置详解
POM的全称是“ProjectObjectModel(项目对象模型)”. pom.xml详解 声明规范 <projectxmlns="http://maven.apache.org/P ...
- (转)Maven pom.xml 配置详解
背景:maven一直感觉既熟悉又陌生,归根结底还是自己不太熟. 1 什么是pom? pom作为项目对象模型.通过xml表示maven项目,使用pom.xml来实现.主要描述了项目:包括配置文件:开发者 ...
- Maven(四-2) Maven pom.xml 配置详解
转载于:http://niuzhenxin.iteye.com/blog/2042102 什么是pom? pom作为项目对象模型.通过xml表示maven项目,使用pom.xml来实现.主要描述 ...
- Maven的pom.xml 配置详解
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
随机推荐
- 《Effective MySQL之SQL语句最优化》读书笔记——乱七八糟系列(给自己看)
该书区别于诸如<MySQL技术内幕——InnoDB存储引擎>等书的一大特色是该书主要讲的是MySQL数据库中的索引技术,并分别讲了InnoDB.MyISAM.Memory三个存储引擎.其中 ...
- 大数据架构之:Kafka
Kafka 是一个高吞吐.分布式.基于发布订阅的消息系统,利用Kafka技术可在廉价PC Server上搭建起大规模消息系统.Kafka具有消息持久化.高吞吐.分布式.多客户端支持.实时等特性,适用于 ...
- 【TECH】CAS php客户端配置
搞完java又搞php,我整个人都不好了=.= 跟大师在linux上折腾了一下午,没调出来,早上在windows上跑通了,中午终于在linux上搞定了,嘿嘿. server端配置参见这里 在windo ...
- Python 字典Dict概念和操作
# 字典概念:无序的, 可变的键值对集合 # 定义 # 方式1 # {key: value, key: value...} # 例如 # {"name": "xin&qu ...
- CCNA 课程 四
Vlan基础: Vlan的作用:把物理上分割的用户,让他们逻辑上在一起. Vlan 范围: 0- 4095 0 4095 是保留的 不可以使用 1 cisco 本证vlan 标准vlan 1 -10 ...
- shell中嵌套执行expect命令实例(利用expect实现自动登录)
expect是 #!/bin/bashpasswd='123456'/usr/bin/expect <<EOFset time 30spawn ssh root@192.168.76.10 ...
- javax.servlet.jsp.JspException cannot be resolved to a type 和 javax.servlet.jsp.PageContext cannot be resolved to a type 解决办法
今天我从码云上拉一个项目下来,是个maven项目,闲来无事自己研究研究,发现刚拉下来,项目就有报错,我一看是httpServletRequest cannot be resolved to a typ ...
- 【codevs1907】方格取数3(最大流最小割定理)
网址:http://codevs.cn/problem/1907/ 题意:在一个矩阵里选不相邻的若干个数,使这些数的和最大. 我们可以把它看成一个最小割,答案就是矩阵中的所有数-最小割.先把矩阵按国际 ...
- intellij idea build时出现Artifact contains illegal characters的解决
此处无法创建是因为Artifact的命名为大小写混合,将大写改为小写即可正常创建
- js装饰者模式
装饰者模式是为已有的功能动态地添加更多功能的一种方式.当系统需要新功能的时候,是向旧的类中添加新的代码.这些新加的代码通常装饰了原有类的核心职责或主要行为,在主类中加入了新的字段,新的方法和新的逻辑, ...