【MAVEN】maven系列--pom.xml标签详解

pom文件作为MAVEN中重要的配置文件,对于它的配置是相当重要。文件中包含了开发者需遵循的规则、缺陷管理系统、组织、licenses、项目信息、项目依赖性等。下面将重点介绍一下该文件的基本组成与功能。

标签预览

<project>
	<modelVersion>4.0.0</modelVersion>
	<!-- 基础设置 -->
	<groupId>...</groupId>
	<artifactId>...</artifactId>
	<version>...</version>
	<packaging>...</packaging>
	<name>...</name>
	<url>...</url>
	<dependencies>...</dependencies>
	<parent>...</parent>
	<dependencyManagement>...</dependencyManagement>
	<modules>...</modules>
	<properties>...</properties>
	<!--构建设置 -->
	<build>...</build>
	<reporting>...</reporting>
	<!-- 更多项目信息 -->
	<name>...</name>
	<description>...</description>
	<url>...</url>
	<inceptionYear>...</inceptionYear>
	<licenses>...</licenses>
	<organization>...</organization>
	<developers>...</developers>
	<contributors>...</contributors>
	<!-- 环境设置-->
	<issueManagement>...</issueManagement>
	<ciManagement>...</ciManagement>
	<mailingLists>...</mailingLists> 
	<scm>...</scm>
	<prerequisites>...</prerequisites>
	<repositories>...</repositories>
	<pluginRepositories>...</pluginRepositories>
	<distributionManagement>...</distributionManagement>
	<profiles>...</profiles>
</project>

基本内容设置

groupId:项目或者组织的唯一标志 ,如cn.gov.customs生成的相对路径为:/cn/gov/customs

artifactId:项目的通用名称

version:项目的版本

packaging:打包机制,如pom,jar,maven-plugin,ejb,war,ear,rar,par

name:用户描述项目的名称,无关紧要的东西,非必要

url:开发团队官方地址 ,非必要

classifer:分类

对于以上基本标签,groupId,artifactId,version,packaging作为项目唯一坐标

POM依赖关系设置

对于POM文件中的关系,主要有依赖,继承,合成等关系。

依赖关系

<dependencies>
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>4.0</version>
		<type>jar</type>
		<scope>test</scope>
		<optional>true</optional>
	</dependency>
	
	<dependency>
	<groupId>com.alibaba.china.shared</groupId>
	<artifactId>alibaba.apollo.webx</artifactId>
	<version>2.5.0</version>
	<exclusions>
		<exclusion>
			<artifactId>org.slf4j.slf4j-api</artifactId>
			<groupId>com.alibaba.external</groupId>
		</exclusion>
		....
	</exclusions>
	......
</dependencies>

以上代码说明:groupId, artifactId, version这三个组合标示依赖的具体工程。如果在中央仓库中没有的依赖包,需要自行导入到本地或私有仓库中。

具体有三种方式,简单提一提,具体后续出专题文章。

  1. 通过本地maven 进行配置安装 使用maven install plugin。如:mvn install:intall-file -Dfile=non-maven-proj.jar -DgroupId=som.group -DartifactId=non-maven-proj -Dversion=1。

  2. 创建自己的repositories并且部署这个包,使用类似上面的deploy:deploy-file命令。

  3. 在代码中配置scope为system,并且指定系统路径。

dependency介绍

dependency下面包含众多字标签。

type:默认为jar类型,常用的类型有:jar,ejb-client,test-jar...,可设置plugins中的extensions值为true后在增加新类型。

scope:用来指定当前包的依赖范围

  • compile(编译范围),是默认的范围,编译范围依赖在所有的classpath中可用,同时它们也会被打包。
  • provided(已提供范围),只有在当JDK或者一个容器已提供该依赖之后才使用。
  • runtime(运行时范围),在运行和测试系统的时候需要。
  • test(测试范围),在一般的 编译和运行时都不需要。
  • system(系统范围),与provided类似

optional:设置指依赖是否可选,默认为false,即子项目默认都继承,为true,则子项目必需显示的引入,与dependencyManagement里定义的依赖类似 。

exclusions:如果X需要A,A包含B依赖,那么X可以声明不要B依赖,只要在exclusions中声明exclusion。

exclusion:将B从依赖树中删除,如上配置,alibaba.apollo.webx不想使用com.alibaba.external ,但是alibaba.apollo.webx是集成了com.alibaba.external,r所以就需要排除掉。

parent:如果一个工程作为父类工程,那就必须添加pom,子系统要继承父类,也必须使用parent标签。对于子系统使用如下所示:

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

说明:

relativePath:为可选项,maven会首先搜索该地址,然后再搜索远程仓库。

dependencyManagement:用于帮助管理chidren的dependencies,优点就是可以集中管理版本。

modules:多模块项目的标签,顺序不重要,MAVEN会自动拓展排序。使用如下所示:

<!--子模块-->
<modules>
    <module>ygb-service-config</module>
    <module>ygb-service-bus</module>
    <module>ygb-service-policy-center</module>
    <module>ygb-service-letter-of-indemnity</module>
    <module>ygb-service-authentication-center</module>
    <module>ygb-service-eureka-center</module>
    <module>ygb-service-api-gateway</module>
    <module>ygb-service-demo</module>
    <module>ygb-service-cache-ehcache</module>
    <module>ygb-service-maven</module>
</modules>

properties:POM文件常量定义区,在文件中可以直接引用,如版本、编码等。如下所示:

<properties>
  <file.encoding>UTF-8</file_encoding>
  <java.source.version>1.8</java_source_version>
  <java.target.version>1.8</java_target_version>
</properties>	

使用方式:${file.encoding}

MAVEN构建设置

这部分主要是对项目的构建过程进行配置,包括打包的方式、插件的安装等。配置如下所示:

<!-- 构建管理 -->
<build>
    <!--构建工具插件管理-->
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

build模块设置

defaultGoal :默认的目标,必须跟命令行上的参数相同,如:jar:jar,或者与时期parse相同,例如install。

directory:指定build target目标的目录,默认为$(basedir}/target,即项目根目录下的target。

finalName:指定去掉后缀的工程名字,例如:默认为${artifactId}-${version}。

filters:定义指定filter属性的位置,例如filter元素赋值filters/filter1.properties,那么这个文件里面就可以定义name=value对,这个name=value对的值就可以在工程pom中通过${name}引用,默认的filter目录是${basedir}/src/main/fiters/。

resources:描述工程中各种文件资源的位置 。

<resource> 
	<targetPath>META-INF/plexus</targetPath> 
	<filtering>false</filtering> 
	<directory>${basedir}/src/main/plexus</directory> 
	<includes> 
		<include>configuration.xml</include> 
	</includes> 
	<excludes> 
		<exclude>**/*.properties</exclude> 
	</excludes> 
</resource>

子标签介绍:

  1. targetPath:指定build资源具体目录,默认是base directory。
  2. filtering:指定是否将filter文件的变量值在这个resource文件有效。即上面说的filters里定义的*.property文件。例如上面就指定那些变量值在configuration文件无效,设置为false。
  3. directory:指定属性文件的目录,build的过程需要找到它,并且将其放到targetPath下,默认的directory是${basedir}/src/main/resources
  4. includes:指定包含文件的patterns,符合样式并且在directory目录下的文件将会包含进project的资源文件。
  5. excludes:指定不包含在内的patterns。
  6. testResources:包含测试资源元素。默认的测试资源路径是${basedir}/src/test/resources,测试资源是不部署的。

plugins配置

对于打包插件的相关配置在该模块配置。样例如下:

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-jar-plugin</artifactId> 
    <version>2.0</version> 
    <extensions>false</extensions> 
    <inherited>true</inherited> 
    <configuration> 
      <classifier>test</classifier> 
    </configuration> 
    <dependencies>...</dependencies> 
    <executions>...</executions> 
</plugin>

子标签说明:

  1. extensions:true or false, 决定是否要load这个plugin的extensions,默认为true。
  2. inherited:是否让子pom继承,ture or false 默认为true。
  3. configuration:通常用于私有不开源的plugin,不能够详细了解plugin的内部工作原理,但使plugin满足的properties
  4. dependencies:与pom基础的dependencies的结构和功能都相同,只是plugin的dependencies用于plugin,而pom的denpendencies用于项目本身。
  5. dependencies:排除一些用不到的dependency或者修改dependency的版本等。
  6. executions:plugin也有很多个目标,每个目标具有不同的配置,executions就是设定plugin的目标。
  7.  <!--executions 内部标签示意-->
    
     <execution> 
         <id>echodir</id> 
         <goals> 
           <goal>run</goal> 
         </goals> 
         <phase>verify</phase> 
         <inherited>false</inherited> 
         <configuration> 
           <tasks> 
             <echo>Build Dir: ${project.build.directory}</echo> 
           </tasks> 
         </configuration> 
     </execution> 
    

pluginManagement配置

pluginManagement的作用类似于denpendencyManagement,只是denpendencyManagement是用于管理项目jar包依赖,pluginManagement是用于管理plugin。样例如下:

<pluginManagement> 
  <plugins> 
    <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-jar-plugin</artifactId> 
      <version>2.2</version> 
      <executions> 
        <execution> 
          <id>pre-process-classes</id> 
          <phase>compile</phase> 
          <goals> 
            <goal>jar</goal> 
          </goals> 
          <configuration> 
            <classifier>pre-process</classifier> 
          </configuration> 
        </execution> 
      </executions> 
    </plugin> 
  </plugins> 
</pluginManagement> 

与pom build里的plugins区别是,这里的plugin是列出来,然后让子pom来决定是否引用。

子pom引用方法: 在pom的build里的plugins引用:

<plugins> 
  <plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-jar-plugin</artifactId> 
  </plugin> 
</plugins>

reporting设置

reporting包含site生成阶段的一些元素,某些maven plugin可以生成reports并且在reporting下配置。reporting里面的reportSets和build里面的executions的作用都是控制pom的不同粒度去控制build的过程,我们不单要配置plugins,还要配置那些plugins单独的goals。样例如下:

<reporting> 
    <plugins> 
      <plugin> 
        ... 
        <reportSets> 
          <reportSet> 
            <id>sunlink</id> 
            <reports> 
              <report>javadoc</report> 
            </reports> 
            <inherited>true</inherited> 
            <configuration> 
              <links> 
                <link>http://java.sun.com/j2se/1.5.0/docs/api/</link> 
              </links> 
            </configuration> 
          </reportSet> 
        </reportSets> 
      </plugin> 
    </plugins> 
  </reporting> 

更多项目信息

这块是一些非必要的设置信息,但是作为项目来讲、版权来讲,也会很重要的信息。

name:项目除了artifactId外,可以定义多个名称。

description:项目描述。

url:项目url。

inceptionYear:创始年份。

Licenses样例如下

<licenses>
  <license>
    <name>Apache 2</name>
    <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
    <distribution>repo</distribution>
    <comments>A business-friendly OSS license</comments>
  </license>
</licenses>

organization:组织信息。

developers:开发者信息。样例如下:

<developers>
    <developer>
      <id>hanyahong</id>
      <name>hanyahong</name>
      <email>ceo@hanyahong.com</email>
      <url>http://www.hanyahong.com</url>
      <organization>hanyahong</organization>
      <organizationUrl>http://www.hanyahong.com</organizationUrl>
      <roles>
        <role>architect</role>
        <role>developer</role>
      </roles>
      <timezone>-6</timezone>
      <properties>
        <picUrl>http://www.hanyahong.com/test</picUrl>
      </properties>
    </developer>
  </developers>

issueManagement:环境配置信息,样例如下:

<issueManagement> 
	<system>Bugzilla</system> 
	<url>http://hanyahong.com/</url> 
</issueManagement> 

repositories:仓库配置信息,pom里面的仓库与setting.xml里的仓库功能是一样,主要的区别在于,pom里的仓库是个性化的。比如一家大公司里的setting文件是公用 的,所有项目都用一个setting文件,但各个子项目却会引用不同的第三方库,所以就需要在pom里设置自己需要的仓库地址。
repositories:要成为maven2的repository artifact,必须具有pom文件在$BASE_REPO/groupId/artifactId/version/artifactId-version.pom
BASE_REPO可以是本地,也可以是远程的。repository元素就是声明那些去查找的repositories
默认的central Maven repository在http://repo1.maven.org/maven2/。样例如下:

<repositories> 
    <repository> 
      <releases> 
        <enabled>false</enabled> 
        <updatePolicy>always</updatePolicy> 
        <checksumPolicy>warn</checksumPolicy> 
      </releases> 
      <snapshots> 
        <enabled>true</enabled> 
        <updatePolicy>never</updatePolicy> 
        <checksumPolicy>fail</checksumPolicy> 
      </snapshots> 
      <id>codehausSnapshots</id> 
      <name>Codehaus Snapshots</name> 
      <url>http://snapshots.maven.codehaus.org/maven2</url> 
      <layout>default</layout> 
    </repository> 
  </repositories> 

(本文完)
注:本文参考互联网相关文章整理所得。

更多信息后续分专题讲解

http://www.hanyahong.com

https://github.com/hanyahong

posted @ 2017-12-02 23:06  神秘人2332  阅读(7493)  评论(0编辑  收藏  举报