《项目管理利器Maven》学习(五):Maven核心之POM

       什么是POM?

       POM (Project Object Model)作为项目对象模型。POM通过xml格式保存的pom.xml文件表示maven项目,使

用pom.xml来实现,作用类似ant的build.xml文件,功能更强大。一个项目所有的配置都放置在POM文件中:该文

件用于管理:源代码、配置文件、开发者的信息和角色、问题追踪系统、组织信息、项目授权、项目的url、项目的依

赖关系等等。

       POM是工作在Maven的基本单位,它始终保存在该项目基本目录中的 pom.xml 文件。在执行任务或目标时,

Maven会使用当前目录中的POM。它读取POM,得到所需要的配置信息,然后执行目标。

       一个完整的pom.xml文件,放置在项目的根目录下。

       创建一POM之前,我们应该先决定项目组(groupId),它的名字(artifactId)和版本(version),因为这些

属性在项目仓库是唯一标识的。现在看一下项目helloworld中示例的POM。

      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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.mycompany.helloworld</groupId>
  <artifactId>helloworld</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>helloworld</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

       应当指出,每个项目应该有一个单一的POM文件。所有的POM文件要项目元素必须三个必填字段: groupId,

artifactId,version,pom.xml的根元素是 project。

       在POM中,groupId,artifactId,version,packaging叫作Maven坐标,它能唯一的确定一个项目。有了

Maven坐标,我们就可以用它来指定我们的项目所依赖的其他项目,插件,或者父项目。一般Maven坐标写成如下

的格式,像我们的例子那样写就可以。

       我们的项目helloworld示例很简单,但是大项目一般会分成几个子项目。在这种情况下,每个子项目就会有自己

的POM文件,然后它们会有一个共同的父项目。这样只要构建父项目就能够构建所有的子项目了。子项目的POM会

继承父项目的POM。另外,所有的POM都继承了一个超级POM。超级POM设置了一些默认值,比如提到的默认的

目录结构,默认的插件等等,它遵循了惯例优于配置的原则。

       超级POM

       所有的POM继承自父类(尽管明确界定)。这个基础的POM被称为超级POM,并包含继承默认值。 Maven使

用有效的POM(超级POM加项目配置的配置)执行有关目标。它可以帮助开发人员指定最低配置的详细信息写在

pom.xml中。虽然配置可以很容易被覆盖。 一个简单的方法来看看超级POM的默认配置,通过运行下面的命令:

mvn help:effective-pom。 在下面的例子中,我们已经创建了一个pom.xml在maven05项目文件夹中。 现在,打

开命令控制台,进入包含pom.xml文件夹并执行以下mvn help:effective-pom命令。

       pom.xml配置:

       

        控制台输出:

[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.demo.maven05:maven05-model:jar:0.0.1SNAPSHOT
[WARNING] 'version' uses an unsupported snapshot version format, should be '*-SNAPSHOT' instead. @ line 11, column 14
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven05-model 0.0.1SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-help-plugin:2.2:effective-pom (default-cli) @ maven05-model ---
[INFO]
Effective POMs, after inheritance, interpolation, and profiles are applied:

<?xml version="1.0" encoding="UTF-8"?>
<!-- ====================================================================== -->
<!--                                                                        -->
<!-- Generated by Maven Help Plugin on 2016-12-28T10:32:20                  -->
<!-- See: http://maven.apache.org/plugins/maven-help-plugin/                -->
<!--                                                                        -->
<!-- ====================================================================== -->

<!-- ====================================================================== -->
<!--                                                                        -->
<!-- Effective POM for project                                              -->
<!-- 'com.demo.maven05:maven05-model:jar:0.0.1SNAPSHOT'                     -->
<!--                                                                        -->
<!-- ====================================================================== -->

<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.demo.maven05</groupId>
  <artifactId>maven05-model</artifactId>
  <version>0.0.1SNAPSHOT</version>
  <properties>
    <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
  <repositories>
    <repository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
    </pluginRepository>
  </pluginRepositories>
  <build>
    <sourceDirectory>E:\Java\develop\code\maven05\src\main\java</sourceDirectory>
    <scriptSourceDirectory>E:\Java\develop\code\maven05\src\main\scripts</scriptSourceDirectory>
    <testSourceDirectory>E:\Java\develop\code\maven05\src\test\java</testSourceDirectory>
    <outputDirectory>E:\Java\develop\code\maven05\target\classes</outputDirectory>
    <testOutputDirectory>E:\Java\develop\code\maven05\target\test-classes</testOutputDirectory>
    <resources>
      <resource>
        <directory>E:\Java\develop\code\maven05\src\main\resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>E:\Java\develop\code\maven05\src\test\resources</directory>
      </testResource>
    </testResources>
    <directory>E:\Java\develop\code\maven05\target</directory>
    <finalName>maven05-model-0.0.1SNAPSHOT</finalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-5</version>
        </plugin>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.8</version>
        </plugin>
        <plugin>
          <artifactId>maven-release-plugin</artifactId>
          <version>2.3.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>2.5</version>
        <executions>
          <execution>
            <id>default-clean</id>
            <phase>clean</phase>
            <goals>
              <goal>clean</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.6</version>
        <executions>
          <execution>
            <id>default-testResources</id>
            <phase>process-test-resources</phase>
            <goals>
              <goal>testResources</goal>
            </goals>
          </execution>
          <execution>
            <id>default-resources</id>
            <phase>process-resources</phase>
            <goals>
              <goal>resources</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <executions>
          <execution>
            <id>default-jar</id>
            <phase>package</phase>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <executions>
          <execution>
            <id>default-compile</id>
            <phase>compile</phase>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>
          <execution>
            <id>default-testCompile</id>
            <phase>test-compile</phase>
            <goals>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12.4</version>
        <executions>
          <execution>
            <id>default-test</id>
            <phase>test</phase>
            <goals>
              <goal>test</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-install-plugin</artifactId>
        <version>2.4</version>
        <executions>
          <execution>
            <id>default-install</id>
            <phase>install</phase>
            <goals>
              <goal>install</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>2.7</version>
        <executions>
          <execution>
            <id>default-deploy</id>
            <phase>deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-site-plugin</artifactId>
        <version>3.3</version>
        <executions>
          <execution>
            <id>default-site</id>
            <phase>site</phase>
            <goals>
              <goal>site</goal>
            </goals>
            <configuration>
              <outputDirectory>E:\Java\develop\code\maven05\target\site</outputDirectory>
              <reportPlugins>
                <reportPlugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-project-info-reports-plugin</artifactId>
                </reportPlugin>
              </reportPlugins>
            </configuration>
          </execution>
          <execution>
            <id>default-deploy</id>
            <phase>site-deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
            <configuration>
              <outputDirectory>E:\Java\develop\code\maven05\target\site</outputDirectory>
              <reportPlugins>
                <reportPlugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-project-info-reports-plugin</artifactId>
                </reportPlugin>
              </reportPlugins>
            </configuration>
          </execution>
        </executions>
        <configuration>
          <outputDirectory>E:\Java\develop\code\maven05\target\site</outputDirectory>
          <reportPlugins>
            <reportPlugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-project-info-reports-plugin</artifactId>
            </reportPlugin>
          </reportPlugins>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <reporting>
    <outputDirectory>E:\Java\develop\code\maven05\target\site</outputDirectory>
  </reporting>
</project>

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.956 s
[INFO] Finished at: 2016-12-28T10:32:20+08:00
[INFO] Final Memory: 10M/155M
[INFO] ------------------------------------------------------------------------

        Maven将开始处理,并显示有效的effective-pom,在控制台显示结果:有效POM,继承,插值,应用配置文

件。在上面的pom.xml 中,可以看到默认的项目源文件夹结构,输出目录,插件,资料库,报表目录,Maven将使

用它们来执行预期的目标。

       POM基本设置

       快速先来看pom.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!-- project是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">
  <!-- 表示模式版本,目前固定为4.0.0 -->
  <modelVersion>4.0.0</modelVersion>
  
  <!-- 基础设置 -->
  <!-- 表示本项目组Id,通常是组织包名 -->
  <groupId>...</groupId>
  <!-- 表示本项目的模块名 -->
  <artifactId>...</artifactId>
   <!-- 
    当前项目的版本号:
    第一个数字表示大版本号
    第二个数字表示分支版本号
    第三个数字表示小版本号 
    示例:0.0.1
    snapshot快照
    alpha内部测试
    beta公测
    release稳定
    GA正式发布
  -->
  <version>...</version>
  <packaging>...</packaging>

  <!-- 项目描述名 -->
  <name>...</name>

  <!-- 项目地址 -->
  <url>...</url>
  <!-- 表示本项目所依赖的所有其他项目 -->
  <dependencies>...</dependencies>
  <!-- 继承 -->
  <parent>...</parent>
  <!-- 依赖管理 -->
  <dependencyManagement>...</dependencyManagement>
  <!-- 聚合 -->
  <modules>...</modules>
  <properties>...</properties>

  <!--构建设置 -->
  <build>...</build>
  <reporting>...</reporting>

  <!-- 更多项目信息 -->
  <name>...</name>
  <!-- 项目描述信息 -->
  <description>...</description>
  <url>...</url>
  <inceptionYear>...</inceptionYear>
  <!-- 许可证信息 -->
  <licenses>...</licenses>
  <!-- 组织信息 --> 
  <organization>...</organization>
  <!-- 开发人员列表信息 -->
  <developers>...</developers>
  <contributors>...</contributors>

  <!-- 环境设置-->
  <issueManagement>...</issueManagement>
  <ciManagement>...</ciManagement>
  <mailingLists>...</mailingLists> 
  <scm>...</scm>
  <prerequisites>...</prerequisites>
  <repositories>...</repositories>
  <pluginRepositories>...</pluginRepositories>
  <distributionManagement>...</distributionManagement>
  <profiles>...</profiles>
  
</project>

       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.mycompany.helloworld</groupId>
  <artifactId>helloworld</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>helloworld</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

</project>

       groupId:项目或者组织的唯一标志,并且配置时生成路径也是由此生成,如com.mycompany.helloworld生成

的相对路径为:/com/mycompany/helloworld

       artifactId:项目的通用名称

       version:项目的版本

       packaging:打包机制,如pom,jar,maven-plugin,ejb,war,ear,rar,par

       name:用户描述项目的名称,无关紧要的东西,可选

       url:应该是只是写明开发团队的网站,无关紧要,可选

       classifer:分类

       其中groupId,artifactId,version,packaging这四项组成了项目的唯一坐标。一般情况下,前面三项就可以组成

项目的唯一坐标了。

       POM关系

       POM关系主要为依赖,继承,合成,用于POM文件的复用。

       依赖关系

       看一个例子:

    <dependencies>
  
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.0</version>
      <type>jar</type>
      <scope>test</scope>
      <optional>true</optional>
    </dependency>

    <dependency>
        <groupId>com.alibaba.china.shared</groupId>
        <artifactId>alibaba.apollo.webx</artifactId>
        <version>2.5.0</version>
        <exclusions>
          <exclusion>
            <artifactId>org.slf4j.slf4j-api</artifactId>
            <groupId>com.alibaba.external</groupId>
          </exclusion>
        </exclusions>
	<dependency>

    </dependencies>

       dependency元素里的属性介绍:

       type:默认为jar类型,常用的类型有:jar,ejb-client,test-jar等,可设置plugins中的extensions值为true后在增

加新的类型;

       scope:是用来指定当前包的依赖范围,maven的依赖范围

       optional:设置指依赖是否可选,默认为false,即子项目默认都继承,为true,则子项目必需显示的引入,与

dependencyManagement里定义的依赖类似 。

       exclusions:如果X需要A,A包含B依赖,那么X可以声明不要B依赖,只要在exclusions中声明exclusion;

       exclusion:是将B从依赖树中删除,如上配置,alibaba.apollo.webx不想使用com.alibaba.external  ,但是

alibaba.apollo.webx是集成了com.alibaba.external,r所以就需要排除掉。

       继承关系

       Maven另一个强大的变化就是带来的是项目继承。

       我们定义的父项目配置:

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mygroup </groupId>
    <artifactId>my-parent</artifactId>
    <version>2.0</version>
    <packaging>pom</packaging>
</project>

       packaging类型,定义值为pom用于定义为parent和合成多个项目。 当然我们创建的maven项目的pom都继承

maven的超级POM。

       接下来子项目配置的就是这样子的:

<project>
   <modelVersion>4.0.0</modelVersion>

   <groupId>com.mygroup</groupId>
   <artifactId>my-child-project</artifactId>
   <parent>
        <groupId>com.mygroup </groupId>
        <artifactId>my-parent</artifactId>
        <version>2.0</version>
        <relativePath>../my-parent</relativePath>
    </parent>
</project>

       relativePath可以不需要,但是用于指明parent的目录,用于快速查询。

       合成关系

       一个项目有多个模块,也叫做多重模块,或者合成项目。 如下的定义:

<project>
   <modelVersion>4.0.0</modelVersion>

   <groupId>com.mygroup</groupId>
   <artifactId>my-parent</artifactId>
   <version>2.0</version>
   <modules>
       <module>my-child-project1<module>
       <module>my-child-project2<module>
   </modules>
</project>
       其中module描述的是子项目的相对路径 。

       POM其它主要配置元素介绍

       dependencyManagement

       Maven还我们提供了一个dependencyManagement元素,用来提供了一种方式来统一依赖版本号。

dependencyManagement元素一 般用在顶层的父POM。使用pom.xml中的dependencyManagement元素能让

你在子项目中引用一个依赖而不用显式的列出版本号。 Maven会沿着父子层次向上走,直到找到一个拥有

dependencyManagement元素的项目,然后它就会使用在这个dependencyManagement元素中指定的版本号,

这样就解决了修改依赖版本号不完全的问题。

       例如如果parent使用dependencyManagement定义了一个dependencyon junit:junit4.0,那么它的children

就可以只引用 groupId和artifactId,而version就可以通过parent来设置,这样的好处就是可以集中管理 依赖的详情

       properties

       properties是为pom定义一些常量,在pom中的其它地方可以直接引用。定义方式如下:

<properties>
      <file.encoding>UTF-8</file_encoding>
      <java.source.version>1.8</java_source_version>
      <java.target.version>1.8</java_target_version>
</properties>

       使用的如下:${file.encoding}

       还可以使用project.xx引用pom里定义的其它属性:如$(project.version} 

       build设置中的plugins配置

       看一个在打包的过程中同时打包源文件:

	<build>
	    <plugins>
	        <plugin>
	              <groupId>org.apache.maven.plugins</groupId>
	              <artifactId>maven-source-plugin</artifactId>
		       <version>2.4</version>
			<executions>
			            <execution>
			                 <phase>package</phase>
			                 <goals>
			                     <goal>jar-no-fork</goal>
			                 </goals>
			            </execution>
			 </executions>	  	        
		   </plugin>
	    </plugins>
	</build>

       extensions:true or false, 决定是否要load这个plugin的extensions,默认为true。plugin也有很多个目标,每

个目标具有不同的配置,executions就是设定plugin的目标,

       goals:里面列出一系列的goals元素,例如上面的jar-no-fork

       phase:声明goals执行的时期,例如:package

       对于POM关系和其它一些重要的元素我们在后面会详细使用示例详解。      


       其它的还可以参考更为详细的文章:maven核心,pom.xml详解(转)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值