maven的pom.xml文件配置(2)

一、什麼是POM
Project Object Model,項目對象模型。通過xml格式保存的pom.xml文件。作用類似ant的build.xml文件,功能更強大。該文件用於管理:源代碼、配置文件、開發者的信息和角色、問題追蹤系統、組織信息、項目授權、項目的url、項目的依賴關係等等。

一個完整的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.0  
                          http://maven.apache.org/maven-v4_0_0.xsd">  
      <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>  

二、基本設置

1、maven的協作相關屬性

<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.0  
                          http://maven.apache.org/maven-v4_0_0.xsd">  
      <modelVersion>4.0.0</modelVersion>  
      <groupId>org.codehaus.mojo</groupId>  
      <artifactId>my-project</artifactId>  
      <version>1.0</version>  
      <packaging>war</packaging>  
    </project>  

groupId:組織標識,例如:org.codehaus.mojo,在M2_REPO目錄下,將是: org/codehaus/mojo目錄。
artifactId:項目名稱,例如:my-project,在M2_REPO目錄下,將是:org/codehaus/mojo/my-project目錄。
version:版本號,例如:1.0,在M2_REPO目錄下,將是:org/codehaus/mojo/my-project/1.0目錄。
packaging:打包的格式,可以爲:pom , jar , maven-plugin , ejb , war , ear , rar , par

2、POM之間的關係
主要用於POM文件的複用。
a)依賴關係:依賴關係列表(dependency list)是POM的重要部分

<dependencies>  
  <dependency>  
    <groupId>junit</groupId>  
    <artifactId>junit</artifactId>  
    <version>4.0</version>  
    <scope>test</scope>  
  </dependency></dependencies>  

在這種機制下,maven還提供了一個類似java.lang.Object的頂級父pom.xml文件:


[html] view plain copy

    <project>  
      <modelVersion>4.0.0</modelVersion>  
      <name>Maven Default Project</name>  
      <repositories>  
        <repository>  
          <id>central</id>  
          <name>Maven Repository Switchboard</name>  
          <layout>default</layout>  
          <url>http://repo1.maven.org/maven2</url>  
          <snapshots>  
            <enabled>false</enabled>  
          </snapshots>  
        </repository>  
      </repositories>  
      <pluginRepositories>  
        <pluginRepository>  
          <id>central</id>  
          <name>Maven Plugin Repository</name>  
          <url>http://repo1.maven.org/maven2</url>  
          <layout>default</layout>  
          <snapshots>  
            <enabled>false</enabled>  
          </snapshots>  
          <releases>  
            <updatePolicy>never</updatePolicy>  
          </releases>  
        </pluginRepository>  
      </pluginRepositories>  
      <build>  
        <directory>target</directory>  
        <outputDirectory>target/classes</outputDirectory>  
        <finalName>${project.artifactId}-${project.version}</finalName>  
        <testOutputDirectory>target/test-classes</testOutputDirectory>  
        <sourceDirectory>src/main/java</sourceDirectory>  
        <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>  
        <testSourceDirectory>src/test/java</testSourceDirectory>  
        <resources>  
          <resource>  
            <directory>src/main/resources</directory>  
          </resource>  
        </resources>  
        <testResources>  
          <testResource>  
            <directory>src/test/resources</directory>  
          </testResource>  
        </testResources>  
        <pluginManagement>  
           <plugins>  
             <plugin>  
               <artifactId>maven-antrun-plugin</artifactId>  
               <version>1.1</version>  
             </plugin>        
             <plugin>  
               <artifactId>maven-assembly-plugin</artifactId>  
               <version>2.2-beta-2</version>  
             </plugin>  
             <plugin>  
               <artifactId>maven-clean-plugin</artifactId>  
               <version>2.2</version>  
             </plugin>  
             <plugin>  
               <artifactId>maven-compiler-plugin</artifactId>  
               <version>2.0.2</version>  
             </plugin>  
             <plugin>  
               <artifactId>maven-dependency-plugin</artifactId>  
               <version>2.0</version>  
             </plugin>  
             <plugin>  
               <artifactId>maven-deploy-plugin</artifactId>  
               <version>2.3</version>  
             </plugin>  
             <plugin>  
               <artifactId>maven-ear-plugin</artifactId>  
               <version>2.3.1</version>  
             </plugin>  
             <plugin>  
               <artifactId>maven-ejb-plugin</artifactId>  
               <version>2.1</version>  
             </plugin>  
             <plugin>  
               <artifactId>maven-install-plugin</artifactId>  
               <version>2.2</version>  
             </plugin>  
             <plugin>  
               <artifactId>maven-jar-plugin</artifactId>  
               <version>2.2</version>  
             </plugin>  
             <plugin>  
               <artifactId>maven-javadoc-plugin</artifactId>  
               <version>2.4</version>  
             </plugin>  
             <plugin>  
               <artifactId>maven-plugin-plugin</artifactId>  
               <version>2.4.1</version>  
             </plugin>  
             <plugin>  
               <artifactId>maven-rar-plugin</artifactId>  
               <version>2.2</version>  
             </plugin>  
             <plugin>                 
               <artifactId>maven-release-plugin</artifactId>  
               <version>2.0-beta-7</version>  
             </plugin>  
             <plugin>                 
               <artifactId>maven-resources-plugin</artifactId>  
               <version>2.2</version>  
             </plugin>  
             <plugin>  
               <artifactId>maven-site-plugin</artifactId>  
               <version>2.0-beta-6</version>  
             </plugin>  
             <plugin>  
               <artifactId>maven-source-plugin</artifactId>  
               <version>2.0.4</version>  
             </plugin>           
             <plugin>  
                <artifactId>maven-surefire-plugin</artifactId>  
                <version>2.4.2</version>  
             </plugin>  
             <plugin>  
               <artifactId>maven-war-plugin</artifactId>  
               <version>2.1-alpha-1</version>  
             </plugin>  
           </plugins>  
         </pluginManagement>  
      </build>  
      <reporting>  
        <outputDirectory>target/site</outputDirectory>  
      </reporting>  
      <profiles>  
        <profile>  
          <id>release-profile</id>  
          <activation>  
            <property>  
              <name>performRelease</name>  
              <value>true</value>  
            </property>  
          </activation>  
          <build>  
            <plugins>  
              <plugin>  
                <inherited>true</inherited>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-source-plugin</artifactId>  
                <executions>  
                  <execution>  
                    <id>attach-sources</id>  
                    <goals>  
                      <goal>jar</goal>  
                    </goals>  
                  </execution>  
                </executions>  
              </plugin>  
              <plugin>  
                <inherited>true</inherited>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-javadoc-plugin</artifactId>  
                <executions>  
                  <execution>  
                    <id>attach-javadocs</id>  
                    <goals>  
                      <goal>jar</goal>  
                    </goals>  
                  </execution>  
                </executions>  
              </plugin>  
              <plugin>  
                <inherited>true</inherited>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-deploy-plugin</artifactId>  
                <configuration>  
                  <updateReleaseInfo>true</updateReleaseInfo>  
                </configuration>  
              </plugin>  
            </plugins>  
          </build>  
        </profile>  
      </profiles>  
    </project>  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章