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 ...
随机推荐
- UVAlive 6697 Homework Evaluation
借鉴了别人的博客啊,自己写写给以后的自己看吧 给出两个字符串,用第二个字符串去匹配第一个字符串,可以对第二个字符串进行删除或插入操作,一位匹配成功得8分失败-5分,如果插入或删除,对于连续插入或删除m ...
- Maven项目配置logback
首先,在pom.xml中加入maven依赖 <!-- log start --> <dependency> <groupId>org.slf4j</group ...
- 【blog】SpringBoot普通类中如何获取其他bean例如Service、Dao
自己写工具类 工具类 import org.springframework.beans.BeansException; import org.springframework.context.Appli ...
- 【mmall】IDEA插件jrebel
破解方法 http://www.jianshu.com/p/87b11bad3d7f
- G - Galactic Collegiate Programming Contest Kattis - gcpc (set使用)
题目链接: G - Galactic Collegiate Programming Contest Kattis - gcpc 题目大意:当前有n个人,一共有m次提交记录,每一次的提交包括两个数,st ...
- Spring学习1:Spring基本特性
http://longliqiang88.github.io/2015/08/14/Spring%E5%AD%A6%E4%B9%A01%EF%BC%9ASpring%E5%9F%BA%E6%9C%AC ...
- TCP通信实现对接硬件发送与接收十六进制数据 & int与byte的转换原理 & java中正负数的表示
今天收到的一份需求任务是对接硬件,TCP通信,并给出通信端口与数据包格式,如下: 1.首先编写了一个简单的十六进制转byte[]数组与byte[]转换16进制字符串的两个方法,如下: /** * 将十 ...
- 如何解决错误0×80071AC3
前几天我在机房敲百例的时候,敲完了想把文件夹移动到我的U盘里去,结果出现了这种情况 上面说运行chkdsk并重试,我运行了一下,没什么反应,我就想既然不能移动文件夹 ...
- Pytorch Visdom
fb官方的一些demo 一. show something 1. vis.image:显示一张图片 viz.image( np.random.rand(3, 512, 256), opts=dic ...
- ESD总结
设计人员完全可以让系统在经过ESD事件后不发生故障并仍能继续运行.将这个目标谨记在心,下面让我们更好地理解ESD冲击时到底发生了什么,然后介绍如何设计正确的系统架构来应对ESD. 简单模型 将一个电容 ...