Maven POM
POM 代表項目對象模型。它是工作在Maven的基本單位。這是一個XML文件。它始終駐留在該項目 pom.xml 基本目錄。
POM 包含的項目使用Maven來構建該項目和各種配置的詳細信息。
POM 也包含了目標和插件。在執行任務或目標時,Maven 會在當前目錄中的POM。它讀取POM,得到所需要的配置信息,然後執行的目標。部分的配置可以在POM使用如下:
-
project dependencies
-
plugins
-
goals
-
build profiles
-
project version
-
developers
-
mailing list
創建一個POM之前,我們應該先決定項目組(groupId),它的名字(artifactId)和它的版本,因為這些屬性在倉庫中的唯一標識項目的幫助。
例子POM
<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.companyname.project-group</groupId> <artifactId>project</artifactId> <version>1.0</version> <project>
應當指出,應該有一個單一的POM文件為每個項目。
-
所有的 POM 文件要求的項目元素和三個必填字段: groupId,artifactId,version。
-
在庫中的項目符號是:groupId:artifactId:version.
-
根元素 pom.xml是項目,它有三個主要的子節點:
節點 | 描述 |
---|---|
groupId | This is an Id of project's group. This is generally unique amongst an organization or a project. For example, a banking group com.company.bank has all bank related projects. |
artifactId | This is an Id of the project.This is generally name of the project. For example, consumer-banking. Along with the groupId, the artifactId defines the artifact's location within the repository. |
version |
This is the version of the project.Along with the groupId, It is used within an artifact's repository to separate versions from each other. For example:
com.company.bank:consumer-banking:1.0 com.company.bank:consumer-banking:1.1. |
超級POM
所有的POM繼承自父類(儘管明確界定)。這個基礎POM被稱為超級POM,並包含繼承默認情況下的值。 Maven使用有效的POM(超級POM加項目配置的配置)執行有關的目標。它可以幫助開發人員指定的最低配置的詳細信息在pom.xml。雖然配置可以很容易被覆蓋。 一個簡單的方法來看看超級POM的默認配置是通過運行下麵的命令:mvn help:effective-pom 創建一個pom.xml上的任何目錄computer.Use的內容上麵提到的例子POM。 在下麵的例子中,我們已經創建了一個pom.xml在C:\MVN\項目文件夾中。 現在,打開命令控製台,進入該文件夾包含pom.xml 和執行以下mvn 命令。
C:\MVN\project>mvn help:effective-pom
Maven將開始處理,並顯示有效的effective-pom。
[INFO] Scanning for projects... [INFO] Searching repository for plugin with prefix: 'help'. [INFO] ------------------------------------------------------------------------ [INFO] Building Unnamed - com.companyname.project-group:project-name:jar:1.0 [INFO] task-segment: [help:effective-pom] (aggregator-style) [INFO] ------------------------------------------------------------------------ [INFO] [help:effective-pom {execution: default-cli}] [INFO] ..... [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: < 1 second [INFO] Finished at: Thu Jul 05 11:41:51 IST 2012 [INFO] Final Memory: 6M/15M [INFO] ------------------------------------------------------------------------
結果顯示在控製台的有效POM,繼承,插值之後,應用配置文件。
<?xml version="1.0" encoding="UTF-8"?> <!-- ================================================================= --> <!-- --> <!-- Generated by Maven Help Plugin on 2012-07-05T11:41:51 --> <!-- See: http://maven.apache.org/plugins/maven-help-plugin/ --> <!-- --> <!-- ================================================================= --> <!-- ================================================================= --> <!-- --> <!-- Effective POM for project --> <!-- 'com.companyname.project-group:project-name:jar:1.0' --> <!-- --> <!-- ================================================================= --> <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 h ttp://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.companyname.project-group</groupId> <artifactId>project</artifactId> <version>1.0</version> <build> <sourceDirectory>C:\MVN\project\src\main\java</sourceDirectory> <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory> <testSourceDirectory>C:\MVN\project\src\test\java</testSourceDirectory> <outputDirectory>C:\MVN\project\target\classes</outputDirectory> <testOutputDirectory>C:\MVN\project\target\test-classes</testOutputDirectory> <resources> <resource> <mergeId>resource-0</mergeId> <directory>C:\MVN\project\src\main\resources</directory> </resource> </resources> <testResources> <testResource> <mergeId>resource-1</mergeId> <directory>C:\MVN\project\src\test\resources</directory> </testResource> </testResources> <directory>C:\MVN\project\target</directory> <finalName>project-1.0</finalName> <pluginManagement> <plugins> <plugin> <artifactId>maven-antrun-plugin</artifactId> <version>1.3</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.4</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.5</version> </plugin> <plugin> <artifactId>maven-plugin-plugin</artifactId> <version>2.4.3</version> </plugin> <plugin> <artifactId>maven-rar-plugin</artifactId> <version>2.2</version> </plugin> <plugin> <artifactId>maven-release-plugin</artifactId> <version>2.0-beta-8</version> </plugin> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>2.3</version> </plugin> <plugin> <artifactId>maven-site-plugin</artifactId> <version>2.0-beta-7</version> </plugin> <plugin> <artifactId>maven-source-plugin</artifactId> <version>2.0.4</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.4.3</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.1-alpha-2</version> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <artifactId>maven-help-plugin</artifactId> <version>2.1.1</version> </plugin> </plugins> </build> <repositories> <repository> <snapshots> <enabled>false</enabled> </snapshots> <id>central</id> <name>Maven Repository Switchboard</name> <url>http://repo1.maven.org/maven2</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <releases> <updatePolicy>never</updatePolicy> </releases> <snapshots> <enabled>false</enabled> </snapshots> <id>central</id> <name>Maven Plugin Repository</name> <url>http://repo1.maven.org/maven2</url> </pluginRepository> </pluginRepositories> <reporting> <outputDirectory>C:\MVN\project\target/site</outputDirectory> </reporting> </project>
在上麵的pom.xml中,可以看到默認的項目的源文件夾結構,輸出目錄,插件需要,資料庫,報表目錄,Maven將使用在執行預期的目標。
Maven pom.xml 無須手動寫入。
Maven提供了大量的的原型插件創建項目,以創建項目結構和pom.xml