除了内置绑定以外,用户还能够自己选择将某个插件目标绑定到生命周期的某个阶段上,这种自定义绑定方式能让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. 2022-2028年中国光掩膜行业市场行情监测及未来前景规划报告
  2. Traceback (most recent call last): File AttributeError: 'NoneType' object has no attribute 'group'
  3. 单卡手机怎么变双卡双待全过程
  4. 0.0 目录-深度学习第一课《神经网络与深度学习》-Stanford吴恩达教授
  5. sudo su 和 sudo -s区别
  6. linux天气软件,类似智能手机!Linux中安装Conky天气插件
  7. python 多线程同步_Python利用多线程同步锁实现多窗口订票系统(推荐)
  8. 基础知识(十二)Opengl回顾记录
  9. mysql存储过程 分表统计_mysql 存储过程:把昨天的数据找出来并保存到对应的月份分表...
  10. JavaScript中的私有函数;Javascript构造函数的私有方法中访问其属性和公有方法
  11. 小孔怎么用_奶粉罐我再也不扔了,在盖子上打几个孔放床边,全家人都抢着用...
  12. 基于气象站点气象数据的空间插值
  13. elk logstach收集交换机日志
  14. GB2312编码的字符串如何转换为ISO-8859-1编码?
  15. 基于华为云服务的人脸识别功能实现
  16. 12生肖年份和星座组合图
  17. walking机器人入门教程-离线建图-cartographer算法建图
  18. java 6 17 32_java学习class6(1)
  19. GitBlit(代码提交)
  20. 通过身份证号计算年龄

热门文章

  1. 使用ivx制作进场动画的经验总结
  2. Bootstrap 插件-折叠
  3. 华为 HarmonyOS 系统一键升级来了!
  4. 一文搞定大众点评字体加密
  5. javascript 算法相关,如何利用指针特性求数组并集与交集
  6. 零基础学习java编程,真的很难吗?手把手将你带入编程世界。
  7. fastadmin后台login登录页面增加canvas粒子动画背景
  8. 佳能hdr_佳能5D3如何使用HDR功能?佳能EOS5D3的HDR功能使用方法介绍
  9. 《炬丰科技-半导体工艺》 PVA 刷擦洗
  10. 输入一个url到显示页面经过哪些过程