在Maven的pom.xml文件中,Build相关配置包含两个部分,一个是<build>,另一个是<reporting>,这里我们只介绍<build>。

1. 在Maven的pom.xml文件中,存在如下两种<build>:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. ...
  5. <!-- "Project Build" contains elements of the BaseBuild set and the Build set-->
  6. <build>...</build>
  7. <profiles>
  8. <profile>
  9. <!-- "Profile Build" contains elements of the BaseBuild set only -->
  10. <build>...</build>
  11. </profile>
  12. </profiles>
  13. </project>

说明:

一种<build>被称为Project Build,即是<project>的直接子元素。另一种<build>被称为Profile Build,即是<profile>的直接子元素。

Profile Build包含了基本的build元素,而Project Build还包含两个特殊的元素,即各种<...Directory>和<extensions>。

2. Profile Build和Project Build共用的基本build元素

1) 示例如下:

  1. <build>
  2. <defaultGoal>install</defaultGoal>
  3. <directory>${basedir}/target</directory>
  4. <finalName>${artifactId}-${version}</finalName>
  5. ...
  6. </build>

说明:

  • defaultGoal,执行构建时默认的goal或phase,如jar:jar或者package等
  • directory,构建的结果所在的路径,默认为${basedir}/target目录
  • finalName,构建的最终结果的名字,该名字可能在其他plugin中被改变

2) <resources>

资源往往不是代码,无需编译,而是一些properties或XML配置文件,构建过程中会往往会将资源文件从源路径复制到指定的目标路径。

<resources>给出各个资源在Maven项目中的具体路径。示例如下:

  1. <build>
  2. ...
  3. <filters>
  4. <filter>filters/filter1.properties</filter>
  5. </filters>
  6. <resources>
  7. <resource>
  8. <targetPath>META-INF/plexus</targetPath>
  9. <filtering>false</filtering>
  10. <directory>${basedir}/src/main/plexus</directory>
  11. <includes>
  12. <include>configuration.xml</include>
  13. </includes>
  14. <excludes>
  15. <exclude>**/*.properties</exclude>
  16. </excludes>
  17. </resource>
  18. </resources>
  19. <testResources>
  20. ...
  21. </testResources>
  22. ...
  23. </build>

说明:

  • resources,build过程中涉及的资源文件

    • targetPath,资源文件的目标路径
    • filtering,构建过程中是否对资源进行过滤,默认false
    • directory,资源文件的路径,默认位于${basedir}/src/main/resources/目录下
    • includes,一组文件名的匹配模式,被匹配的资源文件将被构建过程处理
    • excludes,一组文件名的匹配模式,被匹配的资源文件将被构建过程忽略。同时被includes和excludes匹配的资源文件,将被忽略。
  • filters,给出对资源文件进行过滤的属性文件的路径,默认位于${basedir}/src/main/filters/目录下。属性文件中定义若干键值对。在构建过程中,对于资源文件中出现的变量(键),将使用属性文件中该键对应的值替换。
  • testResources,test过程中涉及的资源文件,默认位于${basedir}/src/test/resources/目录下。这里的资源文件不会被构建到目标构件中

3) <plugins>

<plugins>给出构建过程中所用到的插件。

  1. <build>
  2. ...
  3. <plugins>
  4. <plugin>
  5. <groupId>org.apache.maven.plugins</groupId>
  6. <artifactId>maven-jar-plugin</artifactId>
  7. <version>2.6</version>
  8. <extensions>false</extensions>
  9. <inherited>true</inherited>
  10. <configuration>
  11. <classifier>test</classifier>
  12. </configuration>
  13. <dependencies>...</dependencies>
  14. <executions>...</executions>
  15. </plugin>
  16. </plugins>
  17. </build>

说明:

  • groupId
  • artifactId
  • version
  • extensions,是否加载该插件的扩展,默认false
  • inherited,该插件的configuration中的配置是否可以被(继承该POM的其他Maven项目)继承,默认true
  • configuration,该插件所需要的特殊配置,在父子项目之间可以覆盖或合并
  • dependencies,该插件所特有的依赖类库
  • executions,该插件的某个goal(一个插件中可能包含多个goal)的执行方式。一个execution有如下设置:
    • id,唯一标识
    • goals,要执行的插件的goal(可以有多个),如<goal>run</goal>
    • phase,插件的goal要嵌入到Maven的phase中执行,如verify
    • inherited,该execution是否可被子项目继承
    • configuration,该execution的其他配置参数

4) <pluginManagement>

在<build>中,<pluginManagement>与<plugins>并列,两者之间的关系类似于<dependencyManagement>与<dependencies>之间的关系。<pluginManagement>中也配置<plugin>,其配置参数与<plugins>中的<plugin>完全一致。只是,<pluginManagement>往往出现在父项目中,其中配置的<plugin>往往通用于子项目。子项目中只要在<plugins>中以<plugin>声明该插件,该插件的具体配置参数则继承自父项目中<pluginManagement>对该插件的配置,从而避免在子项目中进行重复配置。

3. Project Build特有的<...Directory>

往往配置在父项目中,供所有父子项目使用。示例如下:

  1. <build>
  2. <sourceDirectory>${basedir}/src/main/java</sourceDirectory>
  3. <scriptSourceDirectory>${basedir}/src/main/scripts</scriptSourceDirectory>
  4. <testSourceDirectory>${basedir}/src/test/java</testSourceDirectory>
  5. <outputDirectory>${basedir}/target/classes</outputDirectory>
  6. <testOutputDirectory>${basedir}/target/test-classes</testOutputDirectory>
  7. ...
  8. </build>
  9. </project>

目录可以使用绝对路径,如示例所示。如果使用相对路径,则所有的相对路径都是在${basedir}目录下。

4. Project Build特有的<extensions>

<extensions>是执行构建过程中可能用到的其他工具,在执行构建的过程中被加入到classpath中。

也可以通过<extensions>激活构建插件,从而改变构建的过程。

通常,通过<extensions>给出通用插件的一个具体实现,用于构建过程。

<extensions>的使用示例如下:

  1. <build>
  2. ...
  3. <extensions>
  4. <extension>
  5. <groupId>org.apache.maven.wagon</groupId>
  6. <artifactId>wagon-ftp</artifactId>
  7. <version>1.0-alpha-3</version>
  8. </extension>
  9. </extensions>
  10. ...
  11. </build>
  12. t;/project>

maven pom.xml中的 build说明的更多相关文章

  1. Java 在pom.xml中配置build resources, 来防止我们资源导出失败问题(Maven项目)

    在pom.xml中配置build, 来防止我们资源导出失败问题 <!--在build中配置resources, 来防止我们资源导出失败问题--> <build> <res ...

  2. 在maven pom.xml中加载不同的properties ,如localhost 和 dev master等jdbc.properties 中的链接不一样

    [参考]:maven pom.xml加载不同properties配置[转] 首先 看看效果: 点开我们项目中的Maven projects 后,会发现右侧 我们profile有个可勾选选项.默认勾选l ...

  3. Maven pom.xml中的元素modules、parent、properties以及import

    前言 项目中用到了maven,而且用到的内容不像利用maven/eclipse搭建ssm(spring+spring mvc+mybatis)用的那么简单:maven的核心是pom.xml,那么我就它 ...

  4. Maven pom.xml中的元素modules、parent、properties以及import(转)

    前言 项目中用到了maven,而且用到的内容不像利用maven/eclipse搭建ssm(spring+spring mvc+mybatis)用的那么简单:maven的核心是pom.xml,那么我就它 ...

  5. Maven pom.xml中添加指定的中央仓库

    中央仓库就是Maven的一个默认的远程仓库,Maven的安装文件中自带了中央仓库的配置($M2_HOME/lib/maven-model-builder.jar) 在很多情况下,默认的中央仓库无法满足 ...

  6. [转]关于maven pom.xml中dependency type 为pom的应用

    原文地址:http://blog.csdn.net/yao123long/article/details/49925659 dependency为什么会有type为pom,默认的值是什么?depend ...

  7. maven pom.xml 中各个标签元素的作用

    <groupId> : 项目或者组织的唯一标识 <artifactId>项目的通用名称 <artifactId>项目的通用名称 <version> 项目 ...

  8. maven pom.xml几个特殊的插件

    1. surefire插件 Maven Surefire 插件有一个 test 目标,该目标被绑定在了 test 阶段.  test 目标执行项目中所有能在 src/test/java 找到的并且文件 ...

  9. eclipse中基于maven构建的web项目pom.xml中指定的jar包无法发布到tomcat中

    eclipse运行maven web项目报错: 信息: Starting Servlet Engine: Apache Tomcat/7.0.57 一月 07, 2015 11:50:44 下午 or ...

随机推荐

  1. k8s PersistentVolume hostpath 简单使用

    kubernets host PersistentVolume 测试 因为yaml 格式的问题 ,我修改为了json 创建 pv pv.json { "kind": "P ...

  2. ballerina 学习二十一 http2

    ballerina 支持http2 协议,包含server push http2 协议 参考代码 import ballerina/http; import ballerina/log;endpoin ...

  3. 希尔排序算法-python实现

    #-*- coding: UTF-8 -*- import numpy as np def ShellSort(a): gap = a.size / 2 while gap >= 1: for ...

  4. Python 迭代对象、迭代器、生成器

    原文出处: liuzhijun 本文源自RQ作者的一篇博文,原文是Iterables vs. Iterators vs. Generators,俺写的这篇文章是按照自己的理解做的参考翻译,算不上是原文 ...

  5. 黄聪:wordpress向mysql字段中保存html代码(使用add_option()方法),然后无法显示出问题

    你可以把" 引号去掉了再进库,或者使用 stripslashes_deep() <?php $str = "Is your name O\'reilly?"; // ...

  6. Bootstrap-Plugin:滚动监听(Scrollspy)插件

    ylbtech-Bootstrap-Plugin:滚动监听(Scrollspy)插件 1.返回顶部 1. Bootstrap 滚动监听(Scrollspy)插件 滚动监听(Scrollspy)插件,即 ...

  7. CentOS6.5 安装mysql-5.7.9

    转自:http://forrest-lv.iteye.com/blog/2260703 安装前,需要检查是否已经有mysql服务进程,是否已经装过mysql;  这点很重要,我之前安装CentOS的同 ...

  8. PHP中的=>,->,@,&,::,%

    在php中数组默认键名是整数,也可以自己定义任意字符键名(最好是有实际意义).如: $css=array('style'=>'0',‘color’=>‘green‘), 则$css['st ...

  9. 20165233 Java第一章学习总结

    20165233 2017-2018-2 <Java程序设计>第一周学习总结 教材学习内容总结 第一章 Java特点:语法简单.面向对象.与平台无关.动态. 字节码不能被任何平台直接识别. ...

  10. 461. Hamming Distance + 477. Total Hamming Distance

    ▶ 与 Hamming  距离相关的两道题. ▶ 461. 求两个数 x 与 y 的哈夫曼距离. ● 代码,4 ms,对 x 和 y 使用异或,然后求值为 1 的位的个数. class Solutio ...