setting.xml主要用于配置maven的运行环境等一系列通用的属性,是全局级别的配置文件;而pom.xml主要描述了项目的maven坐标,依赖关系,开发者需要遵循的规则,缺陷管理系统,组织和licenses,以及其他所有的项目相关因素,是项目级别的配置文件。
基础配置
一个典型的pom.xml文件配置如下:
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
- <groupId>com.winner.tradegroupId>
- <artifactId>trade-coreartifactId>
- <version>1.0.0-SNAPSHOTversion>
- <packaging>jarpackaging>
- <classifier>...classifier>
- <dependencies>
- <dependency>
- <groupId>com.winner.tradegroupId>
- <artifactId>trade-testartifactId>
- <version>1.0.0-SNAPSHOTversion>
- <scope>testscope>
- <optional>falseoptional>
- <exclusions>
- <exclusion>
- <groupId>org.slf4jgroupId>
- <artifactId>slf4j-apiartifactId>
- exclusion>
- exclusions>
- dependency>
- dependencies>
- <properties>
- <file.encoding>UTF-8file.encoding>
- <java.source.version>1.5java.source.version>
- <java.target.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包:
- <dependency>
- <groupId>net.sf.json-libgroupId>
- <artifactId>json-libartifactId>
- <version>2.4version>
- <classifier>jdk15classifier>
- dependency>
构建配置
- <build>
- <finalName>myPorjectNamefinalName>
- <directory>${basedir}/targetdirectory>
- <defaultGoal>installdefaultGoal>
- <filters>
- <filter>../filter.propertiesfilter>
- filters>
- <resources>
- <resource>
- <targetPath>resourcestargetPath>
- <filtering>truefiltering>
- <directory>src/main/resourcesdirectory>
- <includes>
- <include>**/*.propertiesinclude>
- <include>**/*.xmlinclude>
- includes>
- <excludes>
- <exclude>jdbc.propertiesexclude>
- excludes>
- resource>
- resources>
- <testResources>
- <testResource>
- <targetPath />
- <filtering />
- <directory />
- <includes />
- <excludes />
- testResource>
- testResources>
- <sourceDirectory>${basedir}\src\main\javasourceDirectory>
- <scriptSourceDirectory>${basedir}\src\main\scripts
- scriptSourceDirectory>
- <testSourceDirectory>${basedir}\src\test\javatestSourceDirectory>
- <outputDirectory>${basedir}\target\classesoutputDirectory>
- <testOutputDirectory>${basedir}\target\test-classes
- testOutputDirectory>
- <extensions>
- <extension>
- <groupId>org.apache.maven.wagongroupId>
- <artifactId>wagon-sshartifactId>
- <version>2.8version>
- extension>
- extensions>
- <plugins>
- <plugin>
- <groupId>groupId>
- <artifactId>maven-assembly-pluginartifactId>
- <version>2.5.5version>
- <executions>
- <execution>
- <id>assemblyid>
- <phase>packagephase>
- <goals>
- <goal>singlegoal>
- goals>
- <inherited>falseinherited>
- execution>
- executions>
- <configuration>
- <finalName>${finalName}finalName>
- <appendAssemblyId>falseappendAssemblyId>
- <descriptor>assembly.xmldescriptor>
- configuration>
- <extensions>falseextensions>
- <dependencies>
- <dependency>...dependency>
- dependencies>
- <inherited>trueinherited>
- plugin>
- plugins>
- <pluginManagement>
- <plugins>...plugins>
- pluginManagement>
- build>
pom里面的仓库与setting.xml里的仓库功能是一样的。主要的区别在于,pom里的仓库是个性化的。比如一家大公司里的setting文件是公用的,所有项目都用一个setting文件,但各个子项目却会引用不同的第三方库,所以就需要在pom里设置自己需要的仓库地址。
分发配置
- <distributionManagement>
- <repository>
- <uniqueVersion>trueuniqueVersion>
- <id> repo-id id>
- <name> repo-namename>
- <url>file://${basedir}/target/deploy url>
- <layout />
- repository>
- <snapshotRepository>
- <uniqueVersion />
- <id />
- <name />
- <url />
- <layout />
- snapshotRepository>
- <site>
- <id> site-id id>
- <name> site-namename>
- <url>scp://svn.baidu.com/banseon:/var/www/localhost/banseon-web url>
- site>
- <downloadUrl />
- <relocation>
- <groupId />
- <artifactId />
- <version />
- <message />
- relocation>
- <status />
- distributionManagement>
仓库配置
- <repositories>
- <repository>
- <releases>
- <enabled />
- <updatePolicy />
- <checksumPolicy />
- releases>
- <snapshots>
- <enabled />
- <updatePolicy />
- <checksumPolicy />
- snapshots>
- <id> repo-id id>
- <name> repo-namename>
- <url>http://192.168.1.169:9999/repository/ url>
- <layout> defaultlayout>
- repository>
- repositories>
- <pluginRepositories>
- <pluginRepository />
- pluginRepositories>
profile配置
- <profiles>
- <profile>
- <activation>
- <activeByDefault>falseactiveByDefault>
- <jdk>1.7jdk>
- <os>
- <name>Windows XPname>
- <family>Windowsfamily>
- <arch>x86arch>
- <version>5.1.2600version>
- os>
- <property>
- <name>mavenVersionname>
- <value>2.0.3value>
- property>
- <file>
- <exists>/usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/exists>
- <missing>/usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/missing>
- file>
- activation>
- <id />
- <build />
- <modules />
- <repositories />
- <pluginRepositories />
- <dependencies />
- <reporting />
- <dependencyManagement />
- <distributionManagement />
- <properties />
- 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,如果第一个条件满足了,那么后面就不会在进行匹配。
报表配置
- <reporting>
- <excludeDefaults />
- <outputDirectory />
- <plugins>
- <plugin>
- <groupId />
- <artifactId />
- <version />
- <inherited />
- <configuration>
- <links>
- <link>http://java.sun.com/j2se/1.5.0/docs/api/link>
- links>
- configuration>
- <reportSets>
- <reportSet>
- <id>sunlinkid>
- <configuration />
- <inherited />
- <reports>
- <report>javadocreport>
- reports>
- reportSet>
- reportSets>
- plugin>
- plugins>
- reporting>
环境配置
- <issueManagement>
- <system> jira system>
- <url> http://jira.clf.com/url>
- issueManagement>
- <ciManagement>
- <system />
- <url />
- <notifiers>
- <notifier>
- <type />
- <sendOnError />
- <sendOnFailure />
- <sendOnSuccess />
- <sendOnWarning />
- <address />
- <configuration />
- notifier>
- notifiers>
- ciManagement>
项目信息配置
- <name>banseon-maven name>
- <url>http://www.clf.com/ url>
- <description>A maven project to study maven. description>
- <prerequisites>
- <maven />
- prerequisites>
- <inceptionYear />
- <mailingLists>
- <mailingList>
- <name> Demo name>
- <post> [email protected]post>
- <subscribe> [email protected]subscribe>
- <unsubscribe> [email protected]unsubscribe>
- <archive> http:/hi.clf.com/archive>
- mailingList>
- mailingLists>
- <developers>
- <developer>
- <id> HELLO WORLD id>
- <name> banseon name>
- <email> [email protected]email>
- <url />
- <roles>
- <role> Project Managerrole>
- <role>Architect role>
- roles>
- <organization> demoorganization>
- <organizationUrl>http://hi.clf.com/ organizationUrl>
- <properties>
- <dept> No dept>
- properties>
- <timezone> -5timezone>
- developer>
- developers>
- <contributors>
- <contributor>
- <name />
- <email />
- <url />
- <organization />
- <organizationUrl />
- <roles />
- <timezone />
- <properties />
- contributor>
- contributors>
- <licenses>
- <license>
- <name> Apache 2 name>
- <url>http://www.clf.com/LICENSE-2.0.txt url>
- <distribution> repodistribution>
- <comments> Abusiness-friendly OSS license comments>
- license>
- licenses>
- <scm>
- <connection>scm:svn:http://svn.baidu.com/banseon/maven/connection>
- <developerConnection>scm:svn:http://svn.baidu.com/banseon/maven/
- developerConnection>
- <tag />
- <url> http://svn.baidu.com/banseonurl>
- scm>
- <organization>
- <name> demo name>
- <url> http://www.clf.com/url>
- organization>