maven 配置篇之pom.xml

什么是pom?
   
pom 为项 象模型。通 xml 表示maven 目,使用pom.xml来 实现 。主要描述了 目:包括配置文件; 开发 者需要遵循的 规则 ,缺陷管理系 组织 和licenses, 目的url, 目的依 性,以及其他所有的 目相 因素。
快速察看:
<project>
  <modelVersion>4.0.0</modelVersion>

  <!-- The Basics -->
  <groupId>...</groupId>
  <artifactId>...</artifactId>
  <version>...</version>
  <packaging>...</packaging>
  <dependencies>...</dependencies>
  <parent>...</parent>
  <dependencyManagement>...</dependencyManagement>
  <modules>...</modules>
  <properties>...</properties>

  <!-- Build Settings -->
  <build>...</build>
  <reporting>...</reporting>

  <!-- More Project Information -->
  <name>...</name>
  <description>...</description>
  <url>...</url>
  <inceptionYear>...</inceptionYear>
  <licenses>...</licenses>
  <organization>...</organization>
  <developers>...</developers>
  <contributors>...</contributors>

  <!-- Environment Settings -->
  <issueManagement>...</issueManagement>
  <ciManagement>...</ciManagement>
  <mailingLists>...</mailingLists>
  <scm>...</scm>
  <prerequisites>...</prerequisites>
  <repositories>...</repositories>
  <pluginRepositories>...</pluginRepositories>
  <distributionManagement>...</distributionManagement>
  <profiles>...</profiles>
</project>

基本内容:
    POM
包括了所有的 目信息。
maven

pom
了最小的 maven2 元素,允 groupId,artifactId,version 。所有需要的元素
  • groupId:目或者组织的唯一志,并且配置生成的路径也是由此生成,如org.codehaus.mojo生成的相路径/org/codehaus/mojo
  • artifactId: 目的通用名称
  • version:目的版本
  • packaging: 打包的机制,如pom, jar, maven-plugin, ejb, war, ear, rar, par
  • classifier:
POM 系:
主要 承,合成
 
赖关 系:
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.0</version>
      <type>jar</type>
      <scope>test</scope>
      <optional>true</optional>
    </dependency>
    ...
  </dependencies>
  • groupId, artifactId, version:描述了依目唯一
可以通 以下方式 行安装:
·   使用以下的命令安装:
·   mvn install:install-file –Dfile=non-maven-proj.jar –DgroupId=some.group –DartifactId=non-maven-proj –Dversion=1
·   建自己的 , 并配置,使用 deploy:deploy-file
·   置此依 围为 system ,定 一个系 路径。 不提倡。
  • type:的依赖产品包形式,如jarwar
  • scope:用于限制相的依,包括以下的几种变量:
·   compile :默 ,用于 编译
·   provided 似于 编译 ,但支持你期待 jdk 或者容器提供, 似于 classpath
·   runtime: ,需要使用
·   test: 用于 test 务时 使用
·   system: 需要外在提供相 得元素。通 systemPath 来取得
  • systemPath: 用于范围为system。提供相的路径
  • optional: 注可,当目自身也是依赖时。用于连续赖时使用
   独占性    
  
外在告 maven 你只包括指定的 目,不包括相 的依 。此因素主要用于解决版本冲突 问题
  <dependencies>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-embedder</artifactId>
      <version>2.0</version>
      <exclusions>
        <exclusion>
          <groupId>org.apache.maven</groupId>
          <artifactId>maven-core</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
表示 目maven-embedder需要 目maven-core,但我 不想引用maven-core


   
另一个 大的 化,maven 来的是 承。主要的 置:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>my-parent</artifactId>
  <version>2.0</version>
  <packaging>pom</packaging>
</project>
    packaging
型,需要pom用于parent和合成多个 目。我 需要增加相 值给 父pom,用于子 承。 主要的元素如下:
  • 开发者和合作者
  • 插件列表
  • 表列表
  • 插件行使用相的匹配ids
  • 插件配置
  • 目配置
<project>
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>my-parent</artifactId>
    <version>2.0</version>
    <relativePath>../my-parent</relativePath>
  </parent>
  <artifactId>my-project</artifactId>
</project>
relativePath
可以不需要,但是用于指明 parent 的目 ,用于快速 查询

dependencyManagement

用于父 目配置共同的依 赖关 系,主要配置依 包相同因素,如版本, scope

合成(或者多个模
   
一个 目有多个模 ,也叫做多重模 ,或者合成 目。
如下的定
<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>my-parent</artifactId>
  <version>2.0</version>
  <modules>
    <module>my-project1<module>
    <module>my-project2<module>
  </modules>
</project>

build

   
主要用于 编译设 置,包括两个主要的元素, build report
  build
   
主要分 两部分,基本元素和 展元素集合
注意:包括 build profile build
<project>
  <!-- "Project Build" contains more elements than just the BaseBuild set -->
  <build>...</build>
  <profiles>
    <profile>
      <!-- "Profile Build" contains a subset of "Project Build"s elements -->
      <build>...</build>
    </profile>
  </profiles>
</project>

基本元素
<build>
  <defaultGoal>install</defaultGoal>
  <directory>${basedir}/target</directory>
  <finalName>${artifactId}-${version}</finalName>
  <filters>
    <filter>filters/filter1.properties</filter>
  </filters>
  ...
</build>
  • defaultGoal: 的目或者段。如install
  • directory: 编译输出的目
  • finalName: 生成最后的文件的
  • filter: 义过滤,用于替的属性文件,使用maven定的属性。置所有placehold

(resources)
   
目中需要指定的 源。如 spring 配置文件 ,log4j.properties
<project>
  <build>
    ...
    <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>
    </resources>
    <testResources>
      ...
    </testResources>
    ...
  </build>
</project>
  • resources: resource的列表,用于包括所有的
  • targetPath: 指定目路径,用于放置源,用于build
  • filtering: 是否替换资源中的属性placehold
  • directory: 源所在的位置
  • includes: 式,包括那些
  • excludes: 排除的
  • testResources: 测试资源列表
插件
 
build 行的插件,比 有用的部分,如使用 jdk 5.0 编译 等等
<project>
  <build>
    ...
    <plugins>
      <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>
    </plugins>
  </build>
</project>
  • extensions: true or false,是否装插件展。默false
  • inherited: true or false,是否此插件配置将会用于poms,那些承于此的
  • configuration: 指定插件配置
  • dependencies: 插件需要依的包
  • executions: 用于配置execution,一个插件可以有多个目
如下:
    <plugin>
        <artifactId>maven-antrun-plugin</artifactId>

        <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>
        </executions>
      </plugin>
 
明:
  • id:execution 的唯一
  • goals: 表示目
  • phase: 表示段,目将会在什么阶
  • inherited: 和上面的元素一false maven将会拒绝执子插件
  • configuration: 表示此行的配置属性

插件管理
    pluginManagement
:插件管理以同 的方式包括插件元素,用于在特定的 目中配置。所有 承于此 目的子 目都能使用。主要定 插件的共同元素

展元素集合
主要包括以下的元素:
Directories
用于 置各 录结 构,如下:
  <build>
    <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>
    ...
  </build>

Extensions

表示需要 展的插件,必 包括 build 路径。

<project>
  <build>
    ...
    <extensions>
      <extension>
        <groupId>org.apache.maven.wagon</groupId>
        <artifactId>wagon-ftp</artifactId>
        <version>1.0-alpha-3</version>
      </extension>
    </extensions>
    ...
  </build>
</project>

Reporting
   
用于在 site 表。特定的 maven 插件能 出相 的定制和配置 表。
  <reporting>
    <plugins>
      <plugin>
        <outputDirectory>${basedir}/target/site</outputDirectory>
        <artifactId>maven-project-info-reports-plugin</artifactId>
        <reportSets>
          <reportSet></reportSet>
        </reportSets>
      </plugin>
    </plugins>
  </reporting>

Report Sets
   
用于配置不同的目 用于不同的
<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
配置 组织 信息
  <organization>
    <name>Codehaus Mojo</name>
    <url>http://mojo.codehaus.org</url>
  </organization>

Developers
配置 开发 者信息
<developers>
    <developer>
      <id>eric</id>
      <name>Eric</name>
      <email>[email protected]</email>
      <url>http://eric.propellors.net</url>
      <organization>Codehaus</organization>
      <organizationUrl>http://mojo.codehaus.org</organizationUrl>
      <roles>
        <role>architect</role>
        <role>developer</role>
      </roles>
      <timezone>-6</timezone>
      <properties>
        <picUrl>http://tinyurl.com/prv4t</picUrl>
      </properties>
    </developer>
  </developers>

Contributors
  <contributors>
    <contributor>
      <name>Noelle</name>
      <email>[email protected]</email>
      <url>http://noellemarie.com</url>
      <organization>Noelle Marie</organization>
      <organizationUrl>http://noellemarie.com</organizationUrl>
      <roles>
        <role>tester</role>
      </roles>
      <timezone>-5</timezone>
      <properties>
        <gtalk>[email protected]</gtalk>
      </properties>
    </contributor>
  </contributors>



Issue Management
   
bug 跟踪系 ,如 bugzilla,testtrack,clearQuest
  <issueManagement>
    <system>Bugzilla</system>
    <url>http://127.0.0.1/bugzilla</url>
  </issueManagement>
Continuous Integration Management
连续 整合管理,基于 triggers 或者 timings
  <ciManagement>
    <system>continuum</system>
    <url>http://127.0.0.1:8080/continuum</url>
    <notifiers>
      <notifier>
        <type>mail</type>
        <sendOnError>true</sendOnError>
        <sendOnFailure>true</sendOnFailure>
        <sendOnSuccess>false</sendOnSuccess>
        <sendOnWarning>false</sendOnWarning>
        <configuration><address>[email protected]</address></configuration>
      </notifier>
    </notifiers>
  </ciManagement>

Mailing Lists
  <mailingLists>
    <mailingList>
      <name>User List</name>
      <subscribe>[email protected]</subscribe>
      <unsubscribe>[email protected]</unsubscribe>
      <post>[email protected]</post>
      <archive>http://127.0.0.1/user/</archive>
      <otherArchives>
        <otherArchive>http://base.google.com/base/1/127.0.0.1</otherArchive>
      </otherArchives>
    </mailingList>
  </mailingLists>

SCM
 
件配置管理,如 cvs svn
  <scm>
    <connection>scm:svn:http://127.0.0.1/svn/my-project</connection>
    <developerConnection>scm:svn:https://127.0.0.1/svn/my-project</developerConnection>
    <tag>HEAD</tag>
    <url>http://127.0.0.1/websvn/my-project</url>
  </scm>

Repositories

配置同 setting.xml 中的 开发库

Plugin Repositories
配置同 repositories

Distribution Management
用于配置分 管理,配置相 布信息 , 主要用于 布,在 mvn deploy 后表示要 布的位置
1
配置到文件系
<distributionManagement>
<repository>
<id>proficio-repository</id>
<name>Proficio Repository</name>
<url>file://${basedir}/target/deploy</url>
</repository>
</distributionManagement>
2
使用 ssh2 配置
<distributionManagement>
<repository>
<id>proficio-repository</id>
<name>Proficio Repository</name>
<url>scp://sshserver.yourcompany.com/deploy</url>
</repository>
</distributionManagement>
3
使用 sftp 配置
<distributionManagement>
<repository>
<id>proficio-repository</id>
<name>Proficio Repository</name>
<url>sftp://ftpserver.yourcompany.com/deploy</url>
</repository>
</distributionManagement>
4
使用外在的 ssh 配置
   
编译扩 展用于指定使用 wagon 外在 ssh 提供,用于提供你的文件到相 程服 器。
<distributionManagement>
<repository>
<id>proficio-repository</id>
<name>Proficio Repository</name>
<url>scpexe://sshserver.yourcompany.com/deploy</url>
</repository>
</distributionManagement>
<build>
<extensions>
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ssh-external</artifactId>
<version>1.0-alpha-6</version>
</extension>
</extensions>
</build>

5
使用 ftp 配置
<distributionManagement>
<repository>
<id>proficio-repository

猜你喜欢

转载自callan.iteye.com/blog/166165