Maven pom.xml简单归结

承接前文Maven安装Maven settings.xml解读

pom文件解析

笔者此处只对一些重要的配置作下讲解,更多的配置详情参考官网Maven-POM Reference


先附上简单的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>
	<groupId>com.jing</groupId>
	<artifactId>testMaven</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>testMaven</name>
	<description>This is a test war maven</description>

	<dependencies>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.12</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>testMaven</finalName>
		<plugins>
			<plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-war-plugin</artifactId>  
                <version>2.6</version>   
            </plugin> 
		</plugins>
	</build>
</project>

parent

父POM依赖配置

  <parent>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>my-parent</artifactId>
    <version>2.0</version>
    <relativePath>../my-parent</relativePath>
  </parent>

注意点:

  1. 子项目可继承父项目,有个前提是父项目的打包类型packing必须为pom,且单个子项目只可继承一个父项目

  2. relativePath表示寻找父项目pom文件的相对路径,默认值为../pom.xml。查找规则为:maven首先从当前构建项目开始查找父项目的pom文件,然后从本地仓库,最后从远程仓库。

  3. 子项目继承父项目(包含祖先项目)的属性主要有dependenciespluginsproperties等等;
    modules是不继承的;
    如果子项目不指明version则默认使用父项目对应的version值,上例中便是2.0

packing

工程项目的打包类型。默认为jar类

  • jar 打包后会生成.jar格式的依赖包
  • war 打包后会生成.war格式的web包
  • pom 表明只是一个模板类型,多用于父项目

properties

全局属性,只对当前工程有效,不对父/子工程生效

  <properties>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>

其中的属性可通过${${key}}引用,上例中便可通过${maven.compiler.source}得到1.7

dependencyManagement

依赖配置管理

<dependencyManagement>
	<dependencies>
		<dependency>...</dependency>
	</dependencies>
</dependencyManagement>

注意点:

  1. 代表不应用parent参数中的version属性,即采用自定义的version属性。
  2. 需要注意的是它并不添加项目的依赖,只是对项目的依赖进行管理,只有POM中定义了指定的dependency属性才会被添加到项目中

dependency

笔者此处对denpendcy参数作下详细的描述

classifier

classifier主要是在version的基础上再添加指定相应的类型,比如betarealease
举个例子:

<!--加载的依赖名为json-20140107-beta.jar-->
<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20140107</version>
    <classifier>beta</classifier>
</dependency>

scope

依赖作用范围,默认为compile。具体参数配置如下

  • compile 适用于所有阶段,包括发布
  • provided 作用在编译和测试阶段,并不具有传递性,针对Servlet api等使用,同时可用来剔除jar包
  • runtime 只作用于运行、测试阶段,比如JDBC驱动
  • test 只作用于测试阶段,且不会随发布包发布
  • system 以外部包形式加载第三方依赖包,与systemPath属性搭配使用
  • import 作用于<dependencyManagement>节点下,表示引入相应POM中的<dependencyManagement>预定义依赖,解决单继承而无法分类管理依赖库的问题
<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <!--版本号保持一致-->
                <version>${spring.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

optional

设置依赖是否可选,默认为false,表示该项目如果被继承,此依赖会随着被传递;若配置为true,则和dependencyManagement功能一致,只有子项目显式指定才会被加载

exclusions

剔除相应的依赖,避免jar包冲突,即不要在一个项目引入多种同类型但不同版本的jar包

modules

为当前项目添加子项目,而相应的子项目,其pom文件必须有相应的parent参数指向当前项目

小结

POM文件也可配置repositorydistributeManagement参数用于下载构建和上传构建,具体可见前文分析。

posted @ 2017-03-14 20:39  南柯问天  阅读(960)  评论(5编辑  收藏  举报