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.0 modelVersion> <groupId>com.winner.trade groupId> <artifactId>trade-core artifactId> <version>1.0.0-SNAPSHOT version> <packaging>jar packaging> <classifier>... classifier> <dependencies> <dependency> <groupId>com.winner.trade groupId> <artifactId>trade-test artifactId> <version>1.0.0-SNAPSHOT version> <scope>test scope> <optional>false optional> <exclusions> <exclusion> <groupId>org.slf4j groupId> <artifactId>slf4j-api artifactId> exclusion> exclusions> dependency> dependencies> <properties> <file.encoding>UTF-8 file.encoding> <java.source.version>1.5 java.source.version> <java.target.version>1.5 java.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-lib groupId> <artifactId>json-lib artifactId> <version>2.4 version> <classifier>jdk15 classifier> dependency>
构建配置
<build> <finalName>myPorjectName finalName> <directory>${basedir}/target directory> <defaultGoal>install defaultGoal> <filters> <filter>../filter.properties filter> filters> <resources> <resource> <targetPath>resources targetPath> <filtering>true filtering> <directory>src/main/resources directory> <includes> <include>**/*.properties include> <include>**/*.xml include> includes> <excludes> <exclude>jdbc.properties exclude> excludes> resource> resources> <testResources> <testResource> <targetPath /> <filtering /> <directory /> <includes /> <excludes /> testResource> testResources> <sourceDirectory>${basedir}\src\main\java sourceDirectory> <scriptSourceDirectory>${basedir}\src\main\scripts scriptSourceDirectory> <testSourceDirectory>${basedir}\src\test\java testSourceDirectory> <outputDirectory>${basedir}\target\classes outputDirectory> <testOutputDirectory>${basedir}\target\test-classes testOutputDirectory> <extensions> <extension> <groupId>org.apache.maven.wagon groupId> <artifactId>wagon-ssh artifactId> <version>2.8 version> extension> extensions> <plugins> <plugin> <groupId> groupId> <artifactId>maven-assembly-plugin artifactId> <version>2.5.5 version> <executions> <execution> <id>assembly id> <phase>package phase> <goals> <goal>single goal> goals> <inherited>false inherited> execution> executions> <configuration> <finalName>${finalName} finalName> <appendAssemblyId>false appendAssemblyId> <descriptor>assembly.xml descriptor> configuration> <extensions>false extensions> <dependencies> <dependency>... dependency> dependencies> <inherited>true inherited> plugin> plugins> <pluginManagement> <plugins>... plugins> pluginManagement> build>pom里面的仓库与setting.xml里的仓库功能是一样的。主要的区别在于,pom里的仓库是个性化的。比如一家大公司里的setting文件是公用的,所有项目都用一个setting文件,但各个子项目却会引用不同的第三方库,所以就需要在pom里设置自己需要的仓库地址。
分发配置
<distributionManagement> <repository> <uniqueVersion>true uniqueVersion> <id> repo-id id> <name> repo-name name> <url>file://${basedir}/target/deploy url> <layout /> repository> <snapshotRepository> <uniqueVersion /> <id /> <name /> <url /> <layout /> snapshotRepository> <site> <id> site-id id> <name> site-name name> <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-name name> <url>http://192.168.1.169:9999/repository/ url> <layout> default layout> repository> repositories> <pluginRepositories> <pluginRepository /> pluginRepositories>
profile配置
<profiles> <profile> <activation> <activeByDefault>false activeByDefault> <jdk>1.7 jdk> <os> <name>Windows XP name> <family>Windows family> <arch>x86 arch> <version>5.1.2600 version> os> <property> <name>mavenVersion name> <value>2.0.3 value> 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>sunlink id> <configuration /> <inherited /> <reports> <report>javadoc report> 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> <archive> http:/hi.clf.com/ archive> mailingList> mailingLists> <developers> <developer> <id> HELLO WORLD id> <name> banseon name> <url /> <roles> <role> Project Manager role> <role>Architect role> roles> <organization> demo organization> <organizationUrl>http://hi.clf.com/ organizationUrl> <properties> <dept> No dept> properties> <timezone> -5 timezone> 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> repo distribution> <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/banseon url> scm> <organization> <name> demo name> <url> http://www.clf.com/ url> organization>