Maven 插件

1 简介

 在博客Maven 生命周期中,我们了解到可以通过命令行调用Maven的生命周期,而三个生命周期是相互独立的,而在某一个生命周期中是存在前后依赖关系的。生命周期为构建规定了整体架构,而具体的工作则由插件提供,Maven提供了一套默认的插件来完成通用的清理、初始化、编译、测试、打包、集成测试、验证、部署和站点生成等工作。但Maven并不是以一种封闭式的机制来设计的,它在提供极大便利性的同时,仍然是灵活的、可扩展的,这可以通过用户配置现有插件或者自行编写插件来自定义构建行为。可以为插件配置那些参数?参数的含义、类型和内容如何查看?配置的方式如何进行是本文的重点所在。
本文的核心骨架如下:
在这里插入图片描述

2 为什么需要插件配置

 完成了插件和生命周期的绑定之后,用户还可以通过配置插件目标的参数,进一步调整插件目标所执行的任务,以满足项目的需求。比如某个插件的作用是在控制台打印一串字符串,那么配置打印的内容如果可以由用户 以某种方式提供,会非常优雅。

3 获取插件信息

在这里插入图片描述

3.1 在线浏览插件信息

 访问页面
maven插件首页可以看到插件列表信息

3.1.1 compiler

在这里插入图片描述
 以compiler为例,点击链接标签comiler,则可以直接跳转到compiler对应的网址上查看该插件的简介、包含的目标、使用介绍、FAQ以及很多实例。
在这里插入图片描述
 可以看到在compiler首页上,演示了使用-source和-target编译选项,参见下图:
在这里插入图片描述

3.1.2 surfire

 阅读surfire插件首页可以看到如下的内容
在这里插入图片描述
 点击Skipping Tests,查看细节
在这里插入图片描述
 也可以通过阅读Running a single Test或者Inclusions and Exclusions of Tests等实例更加灵活的配置执行的测试项。

3.1.3 使用maven-help-plugin描述插件

在命令行执行如下命令:

mvn help:describe -Dplugin=org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M2 –Ddetail

 该命令可以输出对应插件的详细信息,更加重要的是,执行了上述命令会打印出每个插件目标默认绑定的生命周期中的阶段和该插件目标的的可用参数Avaliable parameters

surefire:test
  Description: Run tests using Surefire.
  Implementation: org.apache.maven.plugin.surefire.SurefirePlugin
  Language: java
  Bound to phase: test
Available parameters:
……
skip (Default: false)
   User property: maven.test.skip
   Set this to 'true' to bypass unit tests entirely. Its use is NOT
   RECOMMENDED, especially if you enable it using the 'maven.test.skip'
   property, because maven.test.skip disables both running the tests and
   compiling the tests. Consider using the skipTests parameter instead.

 skipAfterFailureCount (Default: 0)
   User property: surefire.skipAfterFailureCount
   Set to error/failure count in order to skip remaining tests. Due to race
   conditions in parallel/forked execution this may not be fully guaranteed.
   Enable with system property -Dsurefire.skipAfterFailureCount=1 or any
   number greater than zero. Defaults to '0'.
   See the prerequisites and limitations in documentation:
   http://maven.apache.org/plugins/maven-surefire-plugin/examples/skip-after-failure.html

 skipExec
   User property: maven.test.skip.exec
   This old parameter is just like skipTests, but bound to the old property
   'maven.test.skip.exec'.
   Deprecated. Use skipTests instead.

 skipTests (Default: false)
   User property: skipTests
   Set this to 'true' to skip running tests, but still compile them. Its use
   is NOT RECOMMENDED, but quite convenient on occasion.
   Failsafe plugin deprecated the parameter skipTests and the parameter will
   be removed in Failsafe 3.0.0 as it is a source of conflicts between
   Failsafe and Surefire plugin.
……

 省去版本信息,让Maven自动获取最新插件来进行表述

mvn help:describe –Dplugin= org.apache.maven.plugins:maven-surefire-plugin

 进一步简化,使用插件前缀来替换坐标

mvn help:describe –Dplugin=compiler

 如果要输出更详细的信息,可以加上detail参数

mvn help:describe-Dplugin=compiler –Ddetail

 执行第一条命令,输出结果如下:
在这里插入图片描述
 从输出结果可以看到maven-surefire-plugin在3.0.0-M2的插件前缀为surefire,并且有两个插件目标,分别为help和test。
 另外,也可以查看到插件目标的对应参数,使用如下的命令

mvn surefire:help –Ddetail=true –Dgoal=test

表 1mvn surefire:help –Ddetail=true –Dgoal=test执行结果

C:\Users\lenovo\Desktop>mvn surefire:help -Ddetail=true -Dgoal=help
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] --- maven-surefire-plugin:3.0.0-M3:help (default-cli) @ standalone-pom ---
[INFO] Maven Surefire Plugin 3.0.0-M3
  Maven Surefire MOJO in maven-surefire-plugin.

surefire:help
  Display help information on maven-surefire-plugin.
  Call mvn surefire:help -Ddetail=true -Dgoal=<goal-name> to display parameter
  details.

  Available parameters:

    detail (Default: false)
      If true, display all settable properties for each goal.
      User property: detail

    goal
      The name of the goal for which to show help. If unspecified, all goals
      will be displayed.
      User property: goal

    indentSize (Default: 2)
      The number of spaces per indentation level, should be positive.
      User property: indentSize

    lineLength (Default: 80)
      The maximum length of a display line, should be positive.
      User property: lineLength


[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.891 s
[INFO] Finished at: 2019-06-22T16:16:01+08:00
[INFO] ------------------------------------------------------------------------

C:\Users\lenovo\Desktop>
mvn org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M1:help -Ddetail=true -Dgoal=help

命令则可以指定插件的版本,上述命令表示执行3.0.0-M1插件的help目标,配置了detail和goal参数。
 通过如上的方式可以获得
 从上述几个命令也可以理解从命令行调用插件的细节。以及插件目标的可用参数Avaliable parameters

4 插件配置的方式

在这里插入图片描述

4.1 命令行配置

 很多插件目标的参数都支持命令行配置,用户可以在maven命令中输入-D参数,并伴随一个参数键=参数值的形式。

$ mvn install –Dmaven.test=true

4.2 POM插件配置

4.2.1 POM全局配置

 并不是所有的命令行都适合从命令行进行配置,有些参数从项目创建到项目发布都不会改变,或者很少改变,对于这种情况,在POM中一次性配置显然比在命令行中重复配置优雅的多。POM全局配置方式见表 2

**表 2 POM全局配置**
<build>
    <plugins>
        <!-- 插件定义 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.1</version>
            <!-- 对插件进行全局设置,不管此插件绑定到什么阶段都使用同样的配置 -->
            <configuration>
                <!-- 编译1.7版本的源文件 -->
                <source>1.8</source>
                <!-- 生成与JVM 1.7 兼容的字节码文件 -->
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

 上述的为全局配置,这样不管绑定到comile阶段的compiler任务还是绑定到testCompiler任务,都可以使用该配置.

4.2.2 POM任务配置

 除了为插件配置全局参数,用户还可以为某个插件任务配置特定的参数
在这里插入图片描述

**图 1POM插件任务配置**

插件全局配置中的元素位于元素下,而这里的则位于元素下,表示这是特定任务的配置,而非插件整体的配置。如图 1所示,配置了两个任务,第一个任务ant-validate,配置了一个echo Ant任务,向命令行输出一段文字,表示该任务是被绑定到validate阶段的。第二个任务的id为ant-verify,它绑定到了verify阶段,同样,它也是输出一段文字到命令行,告诉该任务绑定到了verify阶段。

5 总结

 在Maven的世界,插件和依赖是同等重要的构建,它们有着相似之处,也有较大的不同,可以这么理解插件是用来管理的构建的,而依赖则是作为项目的一部分而存在的。插件与依赖遵循相同的坐标机制,并且插件与依赖的解析机制也有着十分相似的特征。通过相互佐证理解,可以加深对Maven架构的理解,这对于熟练的使用Maven和掌握Maven工作的原理是十分必要的。
在这里插入图片描述
 如上图所示:本文以Maven插件为核心,详细的讨论了如何获取插件的信息,包括插件的简要描述,插件目标,以及插件目标的可用参数,并且,讲述了插件配置的两种方式,命令行调用差价则在前两个部分的内容中贯穿。希望通过本文,可以加深Maven使用者对于Maven的理解。

6下载

Maven 插件

												201962217:17:10于马塍路36
  • 6
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值