pom文件做为MAVEN中重要的配置文件,对于它的配置是至关重要。文件中包含了开发者需遵循的规则、缺陷管理系统、组织、licenses、项目信息、项目依赖性等。下面将重点介绍一下该文件的基本组成与功能。java
<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/customsgit
artifactId:项目的通用名称github
version:项目的版本web
packaging:打包机制,如pom,jar,maven-plugin,ejb,war,ear,rar,parspring
name:用户描述项目的名称,可有可无的东西,非必要apache
url:开发团队官方地址 ,非必要api
classifer:分类maven
对于以上基本标签,groupId,artifactId,version,packaging做为项目惟一坐标ide
对于POM文件中的关系,主要有依赖,继承,合成等关系。spring-boot
<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:用来指定当前包的依赖范围
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}
这部分主要是对项目的构建过程进行配置,包括打包的方式、插件的安装等。配置以下所示:
<!-- 构建管理 --> <build> <!--构建工具插件管理--> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </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>
子标签介绍:
对于打包插件的相关配置在该模块配置。样例以下:
<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>
子标签说明:
<!--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的做用相似于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包含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