Mave------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这三个组合标示依赖的具体工程。如果在中央仓库中没有的依赖包,需要自行导入到本地或私有仓库中。
具体有三种方式,简单提一提,具体后续出专题文章。
通过本地maven 进行配置安装 使用maven install plugin。如:mvn install:intall-file -Dfile=non-maven-proj.jar -DgroupId=som.group -DartifactId=non-maven-proj -Dversion=1。
创建自己的repositories并且部署这个包,使用类似上面的deploy:deploy-file命令。
在代码中配置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>
子标签介绍:
- targetPath:指定build资源具体目录,默认是base directory。
- filtering:指定是否将filter文件的变量值在这个resource文件有效。即上面说的filters里定义的*.property文件。例如上面就指定那些变量值在configuration文件无效,设置为false。
- directory:指定属性文件的目录,build的过程需要找到它,并且将其放到targetPath下,默认的directory是${basedir}/src/main/resources。
- includes:指定包含文件的patterns,符合样式并且在directory目录下的文件将会包含进project的资源文件。
- excludes:指定不包含在内的patterns。
- 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>
子标签说明:
- extensions:true or false, 决定是否要load这个plugin的extensions,默认为true。
- inherited:是否让子pom继承,ture or false 默认为true。
- configuration:通常用于私有不开源的plugin,不能够详细了解plugin的内部工作原理,但使plugin满足的properties
- dependencies:与pom基础的dependencies的结构和功能都相同,只是plugin的dependencies用于plugin,而pom的denpendencies用于项目本身。
- dependencies:排除一些用不到的dependency或者修改dependency的版本等。
- executions:plugin也有很多个目标,每个目标具有不同的配置,executions就是设定plugin的目标。
<!--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>
文章转载至:https://www.cnblogs.com/hyhnet/p/7956197.html
Mave------pom.xml标签详解的更多相关文章
- POM.xml 标签详解
pom作为项目对象模型.通过xml表示maven项目,使用pom.xml来实现.主要描述了项目:包括配置文件:开发者需要遵循的规则,缺陷管理系统,组织和licenses,项目的url,项目的依赖性,以 ...
- 【转】maven POM.xml 标签详解
http://blog.csdn.net/sunzhenhua0608/article/details/32938533 pom作为项目对象模型.通过xml表示maven项目,使用pom.xml来实现 ...
- maven POM.xml 标签详解
pom作为项目对象模型.通过xml表示maven项目,使用pom.xml来实现.主要描述了项目:包括配置文件:开发者需要遵循的规则,缺陷管理系统,组织和licenses,项目的url,项目的依赖性,以 ...
- Maven的pom.xml标签详解
<!--父项目的坐标.如果项目中没有规定某个元素的值,那么父项目中的对应值即为项目的默认值. 坐标包括group ID,artifact ID和 version.--> <paren ...
- 【MAVEN】maven系列--pom.xml标签详解
pom文件作为MAVEN中重要的配置文件,对于它的配置是相当重要.文件中包含了开发者需遵循的规则.缺陷管理系统.组织.licenses.项目信息.项目依赖性等.下面将重点介绍一下该文件的基本组成与功能 ...
- 史上最全的maven的pom.xml文件详解(转载)
此文出处:史上最全的maven的pom.xml文件详解——阿豪聊干货 <project xmlns="http://maven.apache.org/POM/4.0.0" x ...
- 【maven学习】pom.xml文件详解
环境 apache-maven-3.6.1 jdk 1.8 eclipse 4.7 POM是项目对象模型(Project Object Model)的简称,它是Maven项目中的文件,使用XML表示, ...
- Maven pom.xml文件详解
Maven pom.xml文件详解 一.简介 POM全称是Project Object Model,即项目对象模型. pom.xml是maven的项目描述文件,它类似与antx的project.xml ...
- [转]Maven的pom.xml文件详解
Maven的pom.xml文件详解------Build Settings 2013年10月30日 13:04:01 阅读数:44678 根据POM 4.0.0 XSD,build元素概念性的划分为两 ...
- 史上最全的maven的pom.xml文件详解
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
随机推荐
- 微信小程序开发(2) 计算器
在这篇微信小程序开发教程中,我们将介绍如何使用微信小程序开发计算器功能. 本文主要分为两个部分,小程序主体部分及计算器业务页面部分 一.小程序主体部分 一个小程序主体部分由三个文件组成,必须放在项目的 ...
- ****** 二十八 ******、软设笔记【数据库】-分布式数据库、特点、数据存储、DBMS组成
分布式数据库 一.分布式数据库 分布式数据库由一组数据组成,这些数据物理上分布在计算机网络的不同结点(场地)上,逻辑上是属于同一个系统.每个结点可以执行局部应用,也能通过网络通信子 ...
- 一张图教你弄清楚linux虚拟机的静态IP设置原理 VMnet8
- [机器学习笔记]奇异值分解SVD简介及其在推荐系统中的简单应用
本文先从几何意义上对奇异值分解SVD进行简单介绍,然后分析了特征值分解与奇异值分解的区别与联系,最后用python实现将SVD应用于推荐系统. 1.SVD详解 SVD(singular value d ...
- 腾讯的h5制作工具教程
http://www.alloyteam.com/2015/06/h5-jiao-hu-ye-bian-ji-qi-aeditor-jie-shao/
- Java基础_0311: 数据表与简单Java类映射
数据表与简单Java类映射 现在假设有如下的关系表,现在要求实现如下的数据关联操作: 一个部门有多个雇员: 一个雇员有一个或零个领导 代码实现 class Dept { private int dep ...
- window.name
name 设置或返回存放窗口的名称的字符串.该名称是在 open()方法创建窗口时指定的或者使用一个<frame>标记的name属性指定的. 窗口的名称可用作一个<a>或者&l ...
- ubuntu16.04配置anaconda环境
0 - 下载安装包 推荐到清华镜像下载.我选择的是Anaconda3-5.1.0-Linux-x86_64.sh. 1 - 安装Anaconda 然后切换到安装包目录,执行下面命令,期间一直按回车或者 ...
- js中创建数组,并往数组里添加元素
数组的创建 var arrayObj = new Array(); //创建一个数组 var arrayObj = new Array([size]); //创建一个数组并指定长度,注意不是上限,是长 ...
- SpringBoot文件上传大小设置(yml中配置)
#文件大小 MB必须大写 # maxFileSize 是单个文件大小 # maxRequestSize是设置总上传的数据大小 spring: servlet: multipart: enabled: ...