除了内置绑定以外,用户还能够自己选择将某个插件目标绑定到生命周期的某个阶段上,这种自定义绑定方式能让Maven项目在构建过程中执行更多更富特色的任务。

一个常见的例子是创建项目的源码jar包。内置的插件绑定关系中没有涉及这一任务,因此需要用户自行配置。maven-source-plugin可以帮助我们完成该任务,它的jar-no-fork目标能够将项目的主代码打包成jar文件,可以将其绑定到default生命周期的verify阶段上,在执行完集成测试后和安装构件之前创建源码jar包。具体配置见下:

<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-source-plugin</artifactId><version>2.1.1</version><executions><execution><id>attach-sources</id><phase>verify</phase><goals><goal>jar-no-fork</goal></goals></execution></executions></plugin>

上述配置中,除了基本的插件坐标声明外,还有插件执行配置,executions下每个execution子元素可以用来配置执行一个任务。

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.amaze</groupId><artifactId>customBindings</artifactId><version>0.0.1-SNAPSHOT</version><name>Maven Custom Binding Plugin</name><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-source-plugin</artifactId><version>2.1.1</version><executions><execution><id>attach-sources</id><phase>verify</phase><goals><goal>jar-no-fork</goal></goals></execution></executions></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.6</source><target>1.6</target></configuration></plugin></plugins></build>
</project>

HelloWorld.java:

package com.amaze.custombindings;public class HelloWorld {public String sayHello(String name){return "Hello "+name;}
}

命令行中到项目根目录下执行mvn clean verify命令,完成后project_home\target下会生成两个jar:

有很多插件的目标在编写时已经定义了默认绑定阶段,在上述配置中删除<pahse>verify</phase>一行,构建仍然可以顺利完成。可以使用maven-help-plugin查看插件详细信息,了解插件目标的默认绑定阶段,运行命令如下:

因为输出内容比较多,屏幕放不下,我们将其生成txt文件:

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-help-plugin:2.2:describe (default-cli) @ standalone-pom ---
[INFO] org.apache.maven.plugins:maven-source-plugin:2.1.1Name: Maven Source Plugin
Description: The Maven 2 Source Plugin creates a JAR archive of the sourcefiles of the current project.
Group Id: org.apache.maven.plugins
Artifact Id: maven-source-plugin
Version: 2.1.1
Goal Prefix: sourceThis plugin has 6 goals:source:aggregateDescription: Aggregate sources for all modules in an aggregator project.Implementation: org.apache.maven.plugin.source.AggregatorSourceJarMojoLanguage: javaBound to phase: packageBefore this mojo executes, it will call:Phase: 'generate-sources'Available parameters:archiveThe archive configuration to use. See Maven Archiver Reference.attach (Default: true)User property: attachSpecifies whether or not to attach the artifact to the projectexcludeResources (Default: false)User property: source.excludeResourcesSpecifies whether or not to exclude resources from the sources-jar. Thiscan be convenient if your project includes large resources, such asimages, and you don't want to include them in the sources-jar.excludesList of files to exclude. Specified as fileset patterns which arerelative to the input directory whose contents is being packaged into theJAR.finalName (Default: ${project.build.finalName})The filename to be used for the generated archive file. For thesource:jar goal, '-sources' is appended to this filename. For thesource:test-jar goal, '-test-sources' is appended.forceCreation (Default: false)User property: source.forceCreationWhether creating the archive should be forced. If set to true, the jarwill always be created. If set to false, the jar will only be createdwhen the sources are newer than the jar.includePom (Default: false)User property: source.includePomSpecifies whether or not to include the POM file in the sources-jar.includesList of files to include. Specified as fileset patterns which arerelative to the input directory whose contents is being packaged into theJAR.outputDirectory (Default: ${project.build.directory})The directory where the generated archive file will be put.useDefaultExcludes (Default: true)Exclude commonly excluded files such as SCM configuration. These aredefined in the plexus FileUtils.getDefaultExcludes()useDefaultManifestFile (Default: false)Set this to true to enable the use of the defaultManifestFile.source:helpDescription: Display help information on maven-source-plugin.Callmvn source:help -Ddetail=true -Dgoal=<goal-name>to display parameter details.Implementation: org.apache.maven.plugin.source.HelpMojoLanguage: javaAvailable parameters:detail (Default: false)User property: detailIf true, display all settable properties for each goal.goalUser property: goalThe name of the goal for which to show help. If unspecified, all goalswill be displayed.indentSize (Default: 2)User property: indentSizeThe number of spaces per indentation level, should be positive.lineLength (Default: 80)User property: lineLengthThe maximum length of a display line, should be positive.source:jarDescription: This plugin bundles all the sources into a jar archive.Implementation: org.apache.maven.plugin.source.SourceJarMojoLanguage: javaBound to phase: packageBefore this mojo executes, it will call:Phase: 'generate-sources'Available parameters:archiveThe archive configuration to use. See Maven Archiver Reference.attach (Default: true)User property: attachSpecifies whether or not to attach the artifact to the projectexcludeResources (Default: false)User property: source.excludeResourcesSpecifies whether or not to exclude resources from the sources-jar. Thiscan be convenient if your project includes large resources, such asimages, and you don't want to include them in the sources-jar.excludesList of files to exclude. Specified as fileset patterns which arerelative to the input directory whose contents is being packaged into theJAR.finalName (Default: ${project.build.finalName})The filename to be used for the generated archive file. For thesource:jar goal, '-sources' is appended to this filename. For thesource:test-jar goal, '-test-sources' is appended.forceCreation (Default: false)User property: source.forceCreationWhether creating the archive should be forced. If set to true, the jarwill always be created. If set to false, the jar will only be createdwhen the sources are newer than the jar.includePom (Default: false)User property: source.includePomSpecifies whether or not to include the POM file in the sources-jar.includesList of files to include. Specified as fileset patterns which arerelative to the input directory whose contents is being packaged into theJAR.outputDirectory (Default: ${project.build.directory})The directory where the generated archive file will be put.useDefaultExcludes (Default: true)Exclude commonly excluded files such as SCM configuration. These aredefined in the plexus FileUtils.getDefaultExcludes()useDefaultManifestFile (Default: false)Set this to true to enable the use of the defaultManifestFile.source:jar-no-forkDescription: This goal bundles all the sources into a jar archive. Thisgoal functions the same as the jar goal but does not fork the build and issuitable for attaching to the build lifecycle.Implementation: org.apache.maven.plugin.source.SourceJarNoForkMojoLanguage: javaBound to phase: packageAvailable parameters:archiveThe archive configuration to use. See Maven Archiver Reference.attach (Default: true)User property: attachSpecifies whether or not to attach the artifact to the projectexcludeResources (Default: false)User property: source.excludeResourcesSpecifies whether or not to exclude resources from the sources-jar. Thiscan be convenient if your project includes large resources, such asimages, and you don't want to include them in the sources-jar.excludesList of files to exclude. Specified as fileset patterns which arerelative to the input directory whose contents is being packaged into theJAR.finalName (Default: ${project.build.finalName})The filename to be used for the generated archive file. For thesource:jar goal, '-sources' is appended to this filename. For thesource:test-jar goal, '-test-sources' is appended.forceCreation (Default: false)User property: source.forceCreationWhether creating the archive should be forced. If set to true, the jarwill always be created. If set to false, the jar will only be createdwhen the sources are newer than the jar.includePom (Default: false)User property: source.includePomSpecifies whether or not to include the POM file in the sources-jar.includesList of files to include. Specified as fileset patterns which arerelative to the input directory whose contents is being packaged into theJAR.outputDirectory (Default: ${project.build.directory})The directory where the generated archive file will be put.useDefaultExcludes (Default: true)Exclude commonly excluded files such as SCM configuration. These aredefined in the plexus FileUtils.getDefaultExcludes()useDefaultManifestFile (Default: false)Set this to true to enable the use of the defaultManifestFile.source:test-jarDescription: This plugin bundles all the test sources into a jar archive.Implementation: org.apache.maven.plugin.source.TestSourceJarMojoLanguage: javaBound to phase: packageBefore this mojo executes, it will call:Phase: 'generate-sources'Available parameters:archiveThe archive configuration to use. See Maven Archiver Reference.attach (Default: true)User property: attachSpecifies whether or not to attach the artifact to the projectexcludeResources (Default: false)User property: source.excludeResourcesSpecifies whether or not to exclude resources from the sources-jar. Thiscan be convenient if your project includes large resources, such asimages, and you don't want to include them in the sources-jar.excludesList of files to exclude. Specified as fileset patterns which arerelative to the input directory whose contents is being packaged into theJAR.finalName (Default: ${project.build.finalName})The filename to be used for the generated archive file. For thesource:jar goal, '-sources' is appended to this filename. For thesource:test-jar goal, '-test-sources' is appended.forceCreation (Default: false)User property: source.forceCreationWhether creating the archive should be forced. If set to true, the jarwill always be created. If set to false, the jar will only be createdwhen the sources are newer than the jar.includePom (Default: false)User property: source.includePomSpecifies whether or not to include the POM file in the sources-jar.includesList of files to include. Specified as fileset patterns which arerelative to the input directory whose contents is being packaged into theJAR.outputDirectory (Default: ${project.build.directory})The directory where the generated archive file will be put.useDefaultExcludes (Default: true)Exclude commonly excluded files such as SCM configuration. These aredefined in the plexus FileUtils.getDefaultExcludes()useDefaultManifestFile (Default: false)Set this to true to enable the use of the defaultManifestFile.source:test-jar-no-forkDescription: This goal bundles all the test sources into a jar archive.This goal functions the same as the test-jar goal but does not fork thebuild, and is suitable for attaching to the build lifecycle.Implementation: org.apache.maven.plugin.source.TestSourceJarNoForkMojoLanguage: javaBound to phase: packageAvailable parameters:archiveThe archive configuration to use. See Maven Archiver Reference.attach (Default: true)User property: attachSpecifies whether or not to attach the artifact to the projectexcludeResources (Default: false)User property: source.excludeResourcesSpecifies whether or not to exclude resources from the sources-jar. Thiscan be convenient if your project includes large resources, such asimages, and you don't want to include them in the sources-jar.excludesList of files to exclude. Specified as fileset patterns which arerelative to the input directory whose contents is being packaged into theJAR.finalName (Default: ${project.build.finalName})The filename to be used for the generated archive file. For thesource:jar goal, '-sources' is appended to this filename. For thesource:test-jar goal, '-test-sources' is appended.forceCreation (Default: false)User property: source.forceCreationWhether creating the archive should be forced. If set to true, the jarwill always be created. If set to false, the jar will only be createdwhen the sources are newer than the jar.includePom (Default: false)User property: source.includePomSpecifies whether or not to include the POM file in the sources-jar.includesList of files to include. Specified as fileset patterns which arerelative to the input directory whose contents is being packaged into theJAR.outputDirectory (Default: ${project.build.directory})The directory where the generated archive file will be put.useDefaultExcludes (Default: true)Exclude commonly excluded files such as SCM configuration. These aredefined in the plexus FileUtils.getDefaultExcludes()useDefaultManifestFile (Default: false)Set this to true to enable the use of the defaultManifestFile.[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.837 s
[INFO] Finished at: 2015-11-18T11:58:14+08:00
[INFO] Final Memory: 7M/96M
[INFO] ------------------------------------------------------------------------

我们知道,当插件目标被绑定到不同的生命周期阶段的时候,其执行顺序会由生命周期阶段的先后顺序决定。如果多个目标被绑定到同一个阶段,它们的执行顺序会是怎样?答案很简单,当多个插件目标绑定到同一个阶段的时候,这些插件声明的先后顺序决定了目标的执行顺序。

Maven插件自定义绑定相关推荐

  1. java自定义maven插件_自定义Maven插件

    第一.自定义Maven插件概述 Mojo:Maven plain Old Java Object.每一个 Mojo 就是 Maven 中的一个执行目标(executable goal),而插件则是对单 ...

  2. java自定义maven插件_Maven自定义插件的实现

    为了快速学习自定义 Maven 插件的过程,接下来将实现一个简单的 Hello Maven 插件,功能很简单:输出 Hello World 插件.具体步骤和操作如下. 创建 Maven 新项目,选择 ...

  3. java自定义maven插件_java – 自定义Maven插件托管和前缀解析

    我已经编写了自己的自定义Maven插件并将其上传到我的Archiva服务器.它与指定的全名一起工作正常: mvn com.mjolnirr:maven-plugin:manifest 但是当我想通过前 ...

  4. 自定义maven插件

    文章目录 maven插件介绍 自定义插件 使用 运行 maven插件介绍 maven插件有很多的作用,maven插件简介 说个用的比较多的,就是jetty插件,通过配置jetty可以通过插件一键运行代 ...

  5. 自定义maven插件:自动生成API的word文档

    继上次开发完Maven插件开发:根据库表生成实体类&根据实体类生成库表之后,博主对开发maven插件喜爱得一塌糊涂.这不,今天给大家带来了<自定义maven插件:自动生成API的word ...

  6. Maven(插件配置和生命周期的绑定)

    1.这篇文章很好,介绍的maven插件的. 2.maven的source插件为例,可以把源代码打成包. Goals Overview就可以查看该插件下面所有的目标. 这里我们要使用的是source:j ...

  7. 常用Maven插件介绍(上)

    转自 : http://www.infoq.com/cn/news/2011/04/xxb-maven-7-plugin 我们都知道Maven本质上是一个插件框架,它的核心并不执行任何具体的构建任务, ...

  8. Maven实战——常用Maven插件介绍

    http://www.infoq.com/cn/news/2011/04/xxb-maven-7-plugin 我们都知道Maven本质上是一个插件框架,它的核心并不执行任何具体的构建任务,所有这些任 ...

  9. maven 插件深入了解

    maven 插件深入了解 http://www.infoq.com/cn/news/2011/04/xxb-maven-7-plugin http://www.infoq.com/cn/news/20 ...

最新文章

  1. 阿里一面,给了几条SQL,问需要执行几次树搜索操作?
  2. react-router使用教程
  3. opencv java_opencv的Java开发环境配置(IntelliJ idea)
  4. 解构华为云HE2E项目中的容器技术应用
  5. Microsoft Project 2010
  6. java 栈_Java实现单链表、栈、队列三种数据结构
  7. Android NDK开发之 NEON 矩阵转置,矩阵乘法实现
  8. Centos给/根分区扩容(图文讲解)
  9. linux 安装so文件,Linux下查找和安装依赖的.so文件
  10. plsql导出表结构到excel_plsql基本操作 复制表 导出表 导出表结构 及其导入
  11. SI4463 空中唤醒教程
  12. win10 实现远程连接linux系统
  13. 彻底搞懂CSS层叠上下文、层叠等级、层叠顺序、z-index
  14. luogu p4556 [Vani有约会]雨天的尾巴 树上差分,最近公共祖先,线段树合并
  15. 32位计算机64位计算机实质,4G内存电脑安装32位还是64位win7系统?实际详细性能测试!...
  16. 14.css中的定位的参照物
  17. 三体归零者和盘龙鸿蒙,《三体》中归零者这样的大神级文明已经脱离黑暗森林和猜疑链了吗,为什么?...
  18. 今日头条阅读量怎么刷_揭露头条刷阅读量内幕:怎么刷的今日头条阅读量
  19. 【数据库】HIVE SQL实现本月一号、月底的提取
  20. java画板中画直线_java实现画图板上画一条直线

热门文章

  1. 凑零钱问题_凑零钱的组合
  2. 房讯通:房产估价背后的混合云服务
  3. 如何pdf转word文档?这几个小技巧你值得拥有
  4. 如何提高自己的阅读能力
  5. 两个字与三个字对齐html,css怎么设置两个字和三个字对齐
  6. 东华|递归|青蛙跳台阶
  7. 通达OA批量更改流程审批人
  8. 子程序调用与宏定义的异同_微机原理 宏指令及子程序设计实验:宏指令和子程序的区别...
  9. 零点之战!探访阿里巴巴8大技术专家,提前揭秘2017双11关键技术
  10. 网线水晶头平行接线和交叉接线的区别