java pom.xml

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


基础配置

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

[html]   view plain  copy
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.   
  4.       
  5.     <modelVersion>4.0.0modelVersion>  
  6.   
  7.       
  8.     <groupId>com.winner.tradegroupId>  
  9.   
  10.       
  11.     <artifactId>trade-coreartifactId>  
  12.   
  13.       
  14.     <version>1.0.0-SNAPSHOTversion>  
  15.   
  16.       
  17.     <packaging>jarpackaging>  
  18.   
  19.       
  20.     <classifier>...classifier>  
  21.   
  22.       
  23.     <dependencies>  
  24.   
  25.           
  26.         <dependency>  
  27.   
  28.                
  29.               
  30.             <groupId>com.winner.tradegroupId>  
  31.             <artifactId>trade-testartifactId>  
  32.             <version>1.0.0-SNAPSHOTversion>  
  33.   
  34.               
  35.               
  36.             <scope>testscope>  
  37.   
  38.               
  39.             <optional>falseoptional>  
  40.   
  41.               
  42.             <exclusions>  
  43.                 <exclusion>  
  44.                     <groupId>org.slf4jgroupId>  
  45.                     <artifactId>slf4j-apiartifactId>  
  46.                 exclusion>  
  47.             exclusions>  
  48.   
  49.         dependency>  
  50.   
  51.     dependencies>  
  52.   
  53.       
  54.     <properties>  
  55.         <file.encoding>UTF-8file.encoding>  
  56.         <java.source.version>1.5java.source.version>  
  57.         <java.target.version>1.5java.target.version>  
  58.     properties>  
  59.   
  60.     ...  
  61. 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 plain  copy
  1. <dependency>  
  2.            <groupId>net.sf.json-libgroupId>  
  3.            <artifactId>json-libartifactId>  
  4.             <version>2.4version>  
  5.            <classifier>jdk15classifier>  
  6. dependency>  

 

构建配置

[html]   view plain  copy
  1. <build>  
  2.   
  3.       
  4.     <finalName>myPorjectNamefinalName>  
  5.   
  6.       
  7.     <directory>${basedir}/targetdirectory>  
  8.   
  9.       
  10.       
  11.     <defaultGoal>installdefaultGoal>  
  12.   
  13.       
  14.       
  15.     <filters>  
  16.         <filter>../filter.propertiesfilter>  
  17.     filters>  
  18.   
  19.       
  20.     <resources>  
  21.         <resource>  
  22.   
  23.               
  24.               
  25.               
  26.             <targetPath>resourcestargetPath>  
  27.   
  28.               
  29.             <filtering>truefiltering>  
  30.   
  31.               
  32.             <directory>src/main/resourcesdirectory>  
  33.   
  34.               
  35.             <includes>  
  36.                 <include>**/*.propertiesinclude>  
  37.                 <include>**/*.xmlinclude>  
  38.             includes>  
  39.   
  40.               
  41.             <excludes>  
  42.                 <exclude>jdbc.propertiesexclude>  
  43.             excludes>  
  44.   
  45.         resource>  
  46.     resources>  
  47.   
  48.       
  49.     <testResources>  
  50.         <testResource>  
  51.             <targetPath />  
  52.             <filtering />  
  53.             <directory />  
  54.             <includes />  
  55.             <excludes />  
  56.         testResource>  
  57.     testResources>  
  58.   
  59.       
  60.     <sourceDirectory>${basedir}\src\main\javasourceDirectory>  
  61.   
  62.       
  63.     <scriptSourceDirectory>${basedir}\src\main\scripts  
  64.     scriptSourceDirectory>  
  65.   
  66.       
  67.     <testSourceDirectory>${basedir}\src\test\javatestSourceDirectory>  
  68.   
  69.       
  70.     <outputDirectory>${basedir}\target\classesoutputDirectory>  
  71.   
  72.       
  73.     <testOutputDirectory>${basedir}\target\test-classes  
  74.     testOutputDirectory>  
  75.   
  76.       
  77.       
  78.       
  79.     <extensions>  
  80.   
  81.           
  82.           
  83.           
  84.           
  85.           
  86.         <extension>  
  87.             <groupId>org.apache.maven.wagongroupId>  
  88.             <artifactId>wagon-sshartifactId>  
  89.             <version>2.8version>  
  90.         extension>  
  91.   
  92.     extensions>  
  93.   
  94.       
  95.     <plugins>  
  96.         <plugin>  
  97.             <groupId>groupId>  
  98.             <artifactId>maven-assembly-pluginartifactId>  
  99.             <version>2.5.5version>  
  100.   
  101.               
  102.             <executions>  
  103.                 <execution>  
  104.   
  105.                       
  106.                     <id>assemblyid>  
  107.   
  108.                       
  109.                     <phase>packagephase>  
  110.   
  111.                       
  112.                     <goals>  
  113.                         <goal>singlegoal>  
  114.                     goals>  
  115.   
  116.                       
  117.                     <inherited>falseinherited>  
  118.   
  119.                 execution>  
  120.             executions>  
  121.   
  122.               
  123.             <configuration>  
  124.                 <finalName>${finalName}finalName>  
  125.                 <appendAssemblyId>falseappendAssemblyId>  
  126.                 <descriptor>assembly.xmldescriptor>  
  127.             configuration>  
  128.   
  129.               
  130.               
  131.             <extensions>falseextensions>  
  132.   
  133.               
  134.             <dependencies>  
  135.                 <dependency>...dependency>  
  136.             dependencies>  
  137.   
  138.               
  139.             <inherited>trueinherited>  
  140.   
  141.         plugin>  
  142.     plugins>  
  143.   
  144.       
  145.       
  146.       
  147.     <pluginManagement>  
  148.         <plugins>...plugins>  
  149.     pluginManagement>  
  150.   
  151. build>  

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


分发配置

[html]   view plain  copy
  1.   
  2.   
  3. <distributionManagement>  
  4.   
  5.       
  6.     <repository>  
  7.   
  8.           
  9.           
  10.         <uniqueVersion>trueuniqueVersion>  
  11.   
  12.         <id> repo-id id>  
  13.         <name> repo-namename>  
  14.         <url>file://${basedir}/target/deploy url>  
  15.         <layout />  
  16.   
  17.     repository>  
  18.   
  19.       
  20.     <snapshotRepository>  
  21.         <uniqueVersion />  
  22.         <id />  
  23.         <name />  
  24.         <url />  
  25.         <layout />  
  26.     snapshotRepository>  
  27.   
  28.       
  29.     <site>  
  30.   
  31.           
  32.         <id> site-id id>  
  33.   
  34.           
  35.         <name> site-namename>  
  36.   
  37.           
  38.         <url>scp://svn.baidu.com/banseon:/var/www/localhost/banseon-web url>  
  39.   
  40.     site>  
  41.   
  42.       
  43.       
  44.     <downloadUrl />  
  45.   
  46.       
  47.     <relocation>  
  48.   
  49.           
  50.         <groupId />  
  51.   
  52.           
  53.         <artifactId />  
  54.   
  55.           
  56.         <version />  
  57.   
  58.           
  59.         <message />  
  60.   
  61.     relocation>  
  62.   
  63.       
  64.       
  65.       
  66.     <status />  
  67.   
  68. distributionManagement>  

仓库配置

[html]   view plain  copy
  1.   
  2. <repositories>  
  3.   
  4.       
  5.     <repository>  
  6.   
  7.           
  8.         <releases>  
  9.   
  10.               
  11.             <enabled />  
  12.   
  13.               
  14.               
  15.               
  16.             <updatePolicy />  
  17.   
  18.               
  19.               
  20.             <checksumPolicy />  
  21.   
  22.         releases>  
  23.   
  24.           
  25.           
  26.           
  27.         <snapshots>  
  28.             <enabled />  
  29.             <updatePolicy />  
  30.             <checksumPolicy />  
  31.         snapshots>  
  32.   
  33.           
  34.         <id> repo-id id>  
  35.   
  36.           
  37.         <name> repo-namename>  
  38.   
  39.           
  40.         <url>http://192.168.1.169:9999/repository/ url>  
  41.   
  42.           
  43.           
  44.           
  45.           
  46.         <layout> defaultlayout>  
  47.   
  48.     repository>  
  49.   
  50. repositories>  
  51.   
  52.   
  53. <pluginRepositories>  
  54.   
  55.       
  56.     <pluginRepository />  
  57.   
  58. pluginRepositories>  

 

profile配置

[html]   view plain  copy
  1.   
  2. <profiles>  
  3.   
  4.       
  5.     <profile>  
  6.           
  7.         <activation>  
  8.   
  9.               
  10.             <activeByDefault>falseactiveByDefault>  
  11.   
  12.               
  13.             <jdk>1.7jdk>  
  14.   
  15.               
  16.             <os>  
  17.   
  18.                   
  19.                 <name>Windows XPname>  
  20.   
  21.                   
  22.                 <family>Windowsfamily>  
  23.   
  24.                   
  25.                 <arch>x86arch>  
  26.   
  27.                   
  28.                 <version>5.1.2600version>  
  29.   
  30.             os>  
  31.   
  32.               
  33.               
  34.             <property>  
  35.   
  36.                   
  37.                 <name>mavenVersionname>  
  38.   
  39.                   
  40.                 <value>2.0.3value>  
  41.   
  42.             property>  
  43.   
  44.               
  45.               
  46.             <file>  
  47.   
  48.                   
  49.                 <exists>/usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/exists>  
  50.   
  51.                   
  52.                 <missing>/usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/missing>  
  53.   
  54.             file>  
  55.   
  56.         activation>  
  57.         <id />  
  58.         <build />  
  59.         <modules />  
  60.         <repositories />  
  61.         <pluginRepositories />  
  62.         <dependencies />  
  63.         <reporting />  
  64.         <dependencyManagement />  
  65.         <distributionManagement />  
  66.         <properties />  
  67.     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 plain  copy
  1.   
  2.   
  3. <reporting>  
  4.   
  5.       
  6.     <excludeDefaults />  
  7.   
  8.       
  9.     <outputDirectory />  
  10.   
  11.       
  12.     <plugins>  
  13.   
  14.         <plugin>  
  15.             <groupId />  
  16.             <artifactId />  
  17.             <version />  
  18.             <inherited />  
  19.             <configuration>  
  20.                 <links>  
  21.                     <link>http://java.sun.com/j2se/1.5.0/docs/api/link>  
  22.                 links>  
  23.             configuration>  
  24.               
  25.               
  26.               
  27.             <reportSets>  
  28.   
  29.                   
  30.                 <reportSet>  
  31.   
  32.                       
  33.                     <id>sunlinkid>  
  34.   
  35.                       
  36.                     <configuration />  
  37.   
  38.                       
  39.                     <inherited />  
  40.   
  41.                       
  42.                     <reports>  
  43.                         <report>javadocreport>  
  44.                     reports>  
  45.   
  46.                 reportSet>  
  47.   
  48.             reportSets>  
  49.   
  50.         plugin>  
  51.   
  52.     plugins>  
  53.   
  54. reporting>     

 

环境配置

[html]   view plain  copy
  1.   
  2. <issueManagement>  
  3.   
  4.       
  5.     <system> jira system>  
  6.   
  7.       
  8.     <url> http://jira.clf.com/url>  
  9.   
  10. issueManagement>  
  11.   
  12.   
  13. <ciManagement>  
  14.   
  15.       
  16.     <system />  
  17.   
  18.       
  19.     <url />  
  20.   
  21.       
  22.     <notifiers>  
  23.   
  24.           
  25.         <notifier>  
  26.   
  27.               
  28.             <type />  
  29.   
  30.               
  31.             <sendOnError />  
  32.   
  33.               
  34.             <sendOnFailure />  
  35.   
  36.               
  37.             <sendOnSuccess />  
  38.   
  39.               
  40.             <sendOnWarning />  
  41.   
  42.               
  43.             <address />  
  44.   
  45.               
  46.             <configuration />  
  47.   
  48.         notifier>  
  49.   
  50.     notifiers>  
  51.   
  52. ciManagement>  

项目信息配置

[html]   view plain  copy
  1.   
  2. <name>banseon-maven name>  
  3.   
  4.   
  5. <url>http://www.clf.com/ url>  
  6.   
  7.   
  8.   
  9.   
  10. <description>A maven project to study maven. description>  
  11.   
  12.   
  13. <prerequisites>  
  14.   
  15.       
  16.     <maven />  
  17.   
  18. prerequisites>  
  19.   
  20.   
  21. <inceptionYear />  
  22.   
  23.   
  24. <mailingLists>  
  25.   
  26.       
  27.     <mailingList>  
  28.   
  29.           
  30.         <name> Demo name>  
  31.   
  32.           
  33.         <post> [email protected]post>  
  34.   
  35.           
  36.         <subscribe> [email protected]subscribe>  
  37.   
  38.           
  39.         <unsubscribe> [email protected]unsubscribe>  
  40.   
  41.           
  42.         <archive> http:/hi.clf.com/archive>  
  43.   
  44.     mailingList>  
  45.   
  46. mailingLists>  
  47.   
  48.   
  49. <developers>  
  50.   
  51.       
  52.     <developer>  
  53.   
  54.           
  55.         <id> HELLO WORLD id>  
  56.   
  57.           
  58.         <name> banseon name>  
  59.   
  60.           
  61.         <email> [email protected]email>  
  62.   
  63.           
  64.         <url />  
  65.   
  66.           
  67.         <roles>  
  68.             <role> Project Managerrole>  
  69.             <role>Architect role>  
  70.         roles>  
  71.   
  72.           
  73.         <organization> demoorganization>  
  74.   
  75.           
  76.         <organizationUrl>http://hi.clf.com/ organizationUrl>  
  77.   
  78.           
  79.         <properties>  
  80.             <dept> No dept>  
  81.         properties>  
  82.   
  83.           
  84.         <timezone> -5timezone>  
  85.   
  86.     developer>  
  87.   
  88. developers>  
  89.   
  90.   
  91. <contributors>  
  92.   
  93.       
  94.     <contributor>  
  95.         <name />  
  96.         <email />  
  97.         <url />  
  98.         <organization />  
  99.         <organizationUrl />  
  100.         <roles />  
  101.         <timezone />  
  102.         <properties />  
  103.     contributor>  
  104.   
  105. contributors>  
  106.   
  107.   
  108.   
  109. <licenses>  
  110.   
  111.       
  112.     <license>  
  113.   
  114.           
  115.         <name> Apache 2 name>  
  116.   
  117.           
  118.         <url>http://www.clf.com/LICENSE-2.0.txt url>  
  119.   
  120.           
  121.         <distribution> repodistribution>  
  122.   
  123.           
  124.         <comments> Abusiness-friendly OSS license comments>  
  125.   
  126.     license>  
  127.   
  128. licenses>  
  129.   
  130.   
  131. <scm>  
  132.   
  133.       
  134.     <connection>scm:svn:http://svn.baidu.com/banseon/maven/connection>  
  135.   
  136.       
  137.     <developerConnection>scm:svn:http://svn.baidu.com/banseon/maven/  
  138.     developerConnection>  
  139.   
  140.       
  141.     <tag />  
  142.   
  143.       
  144.     <url> http://svn.baidu.com/banseonurl>  
  145.   
  146. scm>  
  147.   
  148.   
  149. <organization>  
  150.   
  151.       
  152.     <name> demo name>  
  153.   
  154.       
  155.     <url> http://www.clf.com/url>  
  156.   
  157. organization>   

你可能感兴趣的:(maven)