备忘录2.Maven之pom.xml配置文件详解

setting.xml主要用于配置maven的运行环境等一系列通用的属性,是全局级别的配置文件;而pom.xml主要描述了项目的maven坐标,依赖关系,开发者需要遵循的规则,缺陷管理系统,组织和licenses,以及其他所有的项目相关因素,是项目级别的配置文件。

基础配置

一个典型的pom.xml文件配置如下:

[html]view plaincopy

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0modelVersion>

com.winner.tradegroupId>

trade-coreartifactId>

1.0.0-SNAPSHOTversion>

jarpackaging>

...classifier>

com.winner.tradegroupId>

trade-testartifactId>

1.0.0-SNAPSHOTversion>

testscope>

falseoptional>

org.slf4jgroupId>

slf4j-apiartifactId>

exclusion>

exclusions>

dependency>

dependencies>

UTF-8file.encoding>

1.5java.source.version>

1.5java.target.version>

properties>

    ...  

project>

一般来说,上面的几个配置项对任何项目都是必不可少的,定义了项目的基本属性。

这里有必要对一个不太常用的属性classifier做一下解释,因为有时候引用某个jar包,classifier不写的话会报错。

classifier元素用来帮助定义构件输出的一些附属构件。附属构件与主构件对应,比如主构件是 kimi-app-2.0.0.jar,该项目可能还会通过使用一些插件生成 如kimi-app-2.0.0-javadoc.jar (Java文档)、 kimi-app-2.0.0-sources.jar(Java源代码) 这样两个附属构件。这时候,javadoc、sources就是这两个附属构件的classifier,这样附属构件也就拥有了自己唯一的坐标。

classifier的用途在于:

1. maven download  javadoc / sources jar包的时候,需要借助classifier指明要下载那个附属构件

2. 引入依赖的时候,有时候仅凭groupId、artifactId、version无法唯一的确定某个构件,需要借助classifier来进一步明确目标。比如JSON-lib,有时候会同一个版本会提供多个jar包,在JDK1.5环境下是一套,在JDK1.3环境下是一套:

引用它的时候就要注明JDK版本,否则maven不知道你到底需要哪一套jar包:

[html]view plaincopy

net.sf.json-libgroupId>

json-libartifactId>

2.4version>

jdk15classifier>

dependency>

构建配置

[html]view plaincopy

myPorjectNamefinalName>

${basedir}/targetdirectory>

installdefaultGoal>

../filter.propertiesfilter>

filters>

resourcestargetPath>

truefiltering>

src/main/resourcesdirectory>

**/*.propertiesinclude>

**/*.xmlinclude>

includes>

jdbc.propertiesexclude>

excludes>

resource>

resources>

testResource>

testResources>

${basedir}\src\main\javasourceDirectory>

${basedir}\src\main\scripts  

scriptSourceDirectory>

${basedir}\src\test\javatestSourceDirectory>

${basedir}\target\classesoutputDirectory>

${basedir}\target\test-classes  

testOutputDirectory>

org.apache.maven.wagongroupId>

wagon-sshartifactId>

2.8version>

extension>

extensions>

groupId>

maven-assembly-pluginartifactId>

2.5.5version>

assemblyid>

packagephase>

singlegoal>

goals>

falseinherited>

execution>

executions>

${finalName}finalName>

falseappendAssemblyId>

assembly.xmldescriptor>

configuration>

falseextensions>

...dependency>

dependencies>

trueinherited>

plugin>

plugins>

...plugins>

pluginManagement>

build>

pom里面的仓库与setting.xml里的仓库功能是一样的。主要的区别在于,pom里的仓库是个性化的。比如一家大公司里的setting文件是公用的,所有项目都用一个setting文件,但各个子项目却会引用不同的第三方库,所以就需要在pom里设置自己需要的仓库地址。

分发配置

[html]view plaincopy

trueuniqueVersion>

 repo-id id>

 repo-namename>

file://${basedir}/target/deploy url>

repository>

snapshotRepository>

 site-id id>

 site-namename>

scp://svn.baidu.com/banseon:/var/www/localhost/banseon-web url>

site>

relocation>

distributionManagement>

仓库配置

[html]view plaincopy

releases>

snapshots>

 repo-id id>

 repo-namename>

http://192.168.1.169:9999/repository/ url>

 defaultlayout>

repository>

repositories>

pluginRepositories>

profile配置

[html]view plaincopy

falseactiveByDefault>

1.7jdk>

Windows XPname>

Windowsfamily>

x86arch>

5.1.2600version>

os>

mavenVersionname>

2.0.3value>

property>

/usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/exists>

/usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/missing>

file>

activation>

profile>

profile配置项在setting.xml中也有,是pom.xml中profile元素的裁剪版本,包含了id,activation, repositories, pluginRepositories和 properties元素。这里的profile元素只包含这五个子元素是因为setting.xml只关心构建系统这个整体(这正是settings.xml文件的角色定位),而非单独的项目对象模型设置。如果一个settings中的profile被激活,它的值会覆盖任何其它定义在POM中或者profile.xml中的带有相同id的profile。

pom.xml中的profile可以看做pom.xml的副本,拥有与pom.xml相同的子元素与配置方法。它包含可选的activation(profile的触发器)和一系列的changes。例如test过程可能会指向不同的数据库(相对最终的deployment)或者不同的dependencies或者不同的repositories,并且是根据不同的JDK来改变的。只需要其中一个成立就可以激活profile,如果第一个条件满足了,那么后面就不会在进行匹配。

报表配置

[html]view plaincopy

http://java.sun.com/j2se/1.5.0/docs/api/link>

links>

configuration>

sunlinkid>

javadocreport>

reports>

reportSet>

reportSets>

plugin>

plugins>

reporting>

环境配置

[html]view plaincopy

 jira system>

 http://jira.clf.com/url>

issueManagement>

notifier>

notifiers>

ciManagement>

项目信息配置

[html]view plaincopy

banseon-maven name>

http://www.clf.com/ url>

A maven project to study maven. description>

prerequisites>

 Demo name>

 [email protected]>

 [email protected]>

 [email protected]>

 http:/hi.clf.com/archive>

mailingList>

mailingLists>

 HELLO WORLD id>

 banseon name>

 [email protected]>

 Project Managerrole>

Architect role>

roles>

 demoorganization>

http://hi.clf.com/ organizationUrl>

 No dept>

properties>

 -5timezone>

developer>

developers>

contributor>

contributors>

 Apache 2 name>

http://www.clf.com/LICENSE-2.0.txt url>

 repodistribution>

 Abusiness-friendly OSS license comments>

license>

licenses>

scm:svn:http://svn.baidu.com/banseon/maven/connection>

scm:svn:http://svn.baidu.com/banseon/maven/  

developerConnection>

 http://svn.baidu.com/banseonurl>

scm>

 demo name>

 http://www.clf.com/url>

organization>

你可能感兴趣的:(备忘录2.Maven之pom.xml配置文件详解)