码迷,mamicode.com
首页 > 其他好文 > 详细

maven权威指南学习笔记(五)—— POM

时间:2016-01-10 22:40:58      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:

Archetype插件通过 pom.xml 文件创建了一个项目。这就是项目对象模型 (POM),一个项目的声明性描述。

当Maven运行一个目标的时候,每个目标都会访问定 义在项目POM里的信息。

这个POM文件在maven1中是project.xml,在maven2时改为pom.xml。

<?xml version="1.0" encoding="UTF-8"?>
<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>MavenLearn</groupId>
    <artifactId>simple</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>
上面是一个简单的pom.xml文件的例子,可以看到,在上面这个文件中主要定义了,项目的基本信息 和 项目的依赖信息(dependencies)。

项目的基本信息中最重要的就是: groupId,artifactId,version,这三个关键信息组成了一个项目的坐标(Coordinate),它可以唯一的标示一个项目,也是项目依赖的根据。

groupId:指定 团体,公司,小组,组织,项目,或者其它团体。团体标识的约定是,它以创 建这个项目的组织名称的逆向域名(reverse domain name)开头。例如,来自Sonatype 的项目有一个以com.sonatype开头的groupId,而Apache Software的项目有以 org.apache开头的groupId。

artifactId:在groupId下的表示一个单独项目的唯一标识符。

version:指定一个项目的特定版本。发布的项目有一个固定的版本标识来指向该项目的某一个 特定的版本。例如,对于在开发中的项目用“SNAPSHOT”标记。

以上三个标记 加上 modelVersion(设置为4.0.0) 都是pom.xml中必需的。

POM文件中除了可以指定上面提到的项目基本信息,dependencies,还可以定义插件, profiles,以及项目描述(description),研发人员(developer),邮件列表(mailing list)等。

在没有特别指定的情况下,我们使用的pom文件都是继承自maven的Super POM,文章最后列出的就是Maven 2.1.x 的Super POM,也可以参看maven document 的 Introduction to the POM

我们在定义pom.xml 的时候,尤其是定义 dependencies时,通常会引入同一个版本的好多文件,比如spring 框架下,某个版本的spring-context,spring-test,spring-core,spring-beans,spring-webmvc 等等,即便它们springframework下面有些是有依赖关系的,可以由maven帮我们做了,但是还有好多都是要自己写的,相当麻烦,如果这个你还可以接受,毕竟只定义一次嘛,那如果spring在未来哪天出了一个新特性,你特别想用,要升级一下,那又得重新改一遍,没准还改错了呢。

<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>${project.basedir}/target</directory>
        <outputDirectory>${project.build.directory}/classes</outputDirectory>
        <finalName>${project.artifactId}-${project.version}</finalName>
        <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
        <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
        <!-- TODO: MNG-3731 maven-plugin-tools-api < 2.4.4 expect this to be relative... -->
        <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
        <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
        <resources>
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
            </resource>
        </resources>
        <testResources>
            <testResource>
                <directory>${project.basedir}/src/test/resources</directory>
            </testResource>
        </testResources>
        <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>
    </build>

    <reporting>
        <outputDirectory>${project.build.directory}/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>

 

 

 

 

参考:

Introduction to the POM,maven document,http://maven.apache.org/guides/introduction/introduction-to-the-pom.html

maven权威指南

maven权威指南学习笔记(五)—— POM

标签:

原文地址:http://www.cnblogs.com/chrischennx/p/5119673.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!