Maven-07: 插件的自定义绑定

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

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

 1          <plugin>
 2               <groupId>org.apache.maven.plugins</groupId>
 3               <artifactId>maven-source-plugin</artifactId>
 4               <version>2.1.1</version>
 5               <executions>
 6                   <execution>
 7                       <id>attach-sources</id>
 8                       <phase>verify</phase>
 9                       <goals>
10                           <goal>jar-no-fork</goal>
11                       </goals>
12                   </execution>
13               </executions>
14           </plugin>

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

pom.xml:

 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
 4     http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5   <modelVersion>4.0.0</modelVersion>
 6   <groupId>com.amaze</groupId>
 7   <artifactId>customBindings</artifactId>
 8   <version>0.0.1-SNAPSHOT</version>
 9   <name>Maven Custom Binding Plugin</name>
10
11   <build>
12       <plugins>
13           <plugin>
14               <groupId>org.apache.maven.plugins</groupId>
15               <artifactId>maven-source-plugin</artifactId>
16               <version>2.1.1</version>
17               <executions>
18                   <execution>
19                       <id>attach-sources</id>
20                       <phase>verify</phase>
21                       <goals>
22                           <goal>jar-no-fork</goal>
23                       </goals>
24                   </execution>
25               </executions>
26           </plugin>
27
28           <plugin>
29               <groupId>org.apache.maven.plugins</groupId>
30               <artifactId>maven-compiler-plugin</artifactId>
31               <configuration>
32                   <source>1.6</source>
33                   <target>1.6</target>
34               </configuration>
35           </plugin>
36       </plugins>
37   </build>
38 </project>

HelloWorld.java:

1 package com.amaze.custombindings;
2
3 public class HelloWorld {
4
5     public String sayHello(String name){
6         return "Hello "+name;
7     }
8 }

命令行中到项目根目录下执行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.1

Name: Maven Source Plugin
Description: The Maven 2 Source Plugin creates a JAR archive of the source
  files of the current project.
Group Id: org.apache.maven.plugins
Artifact Id: maven-source-plugin
Version: 2.1.1
Goal Prefix: source

This plugin has 6 goals:

source:aggregate
  Description: Aggregate sources for all modules in an aggregator project.
  Implementation: org.apache.maven.plugin.source.AggregatorSourceJarMojo
  Language: java
  Bound to phase: package
  Before this mojo executes, it will call:
    Phase: ‘generate-sources‘

  Available parameters:

    archive
      The archive configuration to use. See Maven Archiver Reference.

    attach (Default: true)
      User property: attach
      Specifies whether or not to attach the artifact to the project

    excludeResources (Default: false)
      User property: source.excludeResources
      Specifies whether or not to exclude resources from the sources-jar. This
      can be convenient if your project includes large resources, such as
      images, and you don‘t want to include them in the sources-jar.

    excludes
      List of files to exclude. Specified as fileset patterns which are
      relative to the input directory whose contents is being packaged into the
      JAR.

    finalName (Default: ${project.build.finalName})
      The filename to be used for the generated archive file. For the
      source:jar goal, ‘-sources‘ is appended to this filename. For the
      source:test-jar goal, ‘-test-sources‘ is appended.

    forceCreation (Default: false)
      User property: source.forceCreation
      Whether creating the archive should be forced. If set to true, the jar
      will always be created. If set to false, the jar will only be created
      when the sources are newer than the jar.

    includePom (Default: false)
      User property: source.includePom
      Specifies whether or not to include the POM file in the sources-jar.

    includes
      List of files to include. Specified as fileset patterns which are
      relative to the input directory whose contents is being packaged into the
      JAR.

    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 are
      defined in the plexus FileUtils.getDefaultExcludes()

    useDefaultManifestFile (Default: false)
      Set this to true to enable the use of the defaultManifestFile.

source:help
  Description: Display help information on maven-source-plugin.
    Call
     mvn source:help -Ddetail=true -Dgoal=<goal-name>
    to display parameter details.
  Implementation: org.apache.maven.plugin.source.HelpMojo
  Language: java

  Available parameters:

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

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

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

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

source:jar
  Description: This plugin bundles all the sources into a jar archive.
  Implementation: org.apache.maven.plugin.source.SourceJarMojo
  Language: java
  Bound to phase: package
  Before this mojo executes, it will call:
    Phase: ‘generate-sources‘

  Available parameters:

    archive
      The archive configuration to use. See Maven Archiver Reference.

    attach (Default: true)
      User property: attach
      Specifies whether or not to attach the artifact to the project

    excludeResources (Default: false)
      User property: source.excludeResources
      Specifies whether or not to exclude resources from the sources-jar. This
      can be convenient if your project includes large resources, such as
      images, and you don‘t want to include them in the sources-jar.

    excludes
      List of files to exclude. Specified as fileset patterns which are
      relative to the input directory whose contents is being packaged into the
      JAR.

    finalName (Default: ${project.build.finalName})
      The filename to be used for the generated archive file. For the
      source:jar goal, ‘-sources‘ is appended to this filename. For the
      source:test-jar goal, ‘-test-sources‘ is appended.

    forceCreation (Default: false)
      User property: source.forceCreation
      Whether creating the archive should be forced. If set to true, the jar
      will always be created. If set to false, the jar will only be created
      when the sources are newer than the jar.

    includePom (Default: false)
      User property: source.includePom
      Specifies whether or not to include the POM file in the sources-jar.

    includes
      List of files to include. Specified as fileset patterns which are
      relative to the input directory whose contents is being packaged into the
      JAR.

    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 are
      defined in the plexus FileUtils.getDefaultExcludes()

    useDefaultManifestFile (Default: false)
      Set this to true to enable the use of the defaultManifestFile.

source:jar-no-fork
  Description: This goal bundles all the sources into a jar archive. This
    goal functions the same as the jar goal but does not fork the build and is
    suitable for attaching to the build lifecycle.
  Implementation: org.apache.maven.plugin.source.SourceJarNoForkMojo
  Language: java
  Bound to phase: package

  Available parameters:

    archive
      The archive configuration to use. See Maven Archiver Reference.

    attach (Default: true)
      User property: attach
      Specifies whether or not to attach the artifact to the project

    excludeResources (Default: false)
      User property: source.excludeResources
      Specifies whether or not to exclude resources from the sources-jar. This
      can be convenient if your project includes large resources, such as
      images, and you don‘t want to include them in the sources-jar.

    excludes
      List of files to exclude. Specified as fileset patterns which are
      relative to the input directory whose contents is being packaged into the
      JAR.

    finalName (Default: ${project.build.finalName})
      The filename to be used for the generated archive file. For the
      source:jar goal, ‘-sources‘ is appended to this filename. For the
      source:test-jar goal, ‘-test-sources‘ is appended.

    forceCreation (Default: false)
      User property: source.forceCreation
      Whether creating the archive should be forced. If set to true, the jar
      will always be created. If set to false, the jar will only be created
      when the sources are newer than the jar.

    includePom (Default: false)
      User property: source.includePom
      Specifies whether or not to include the POM file in the sources-jar.

    includes
      List of files to include. Specified as fileset patterns which are
      relative to the input directory whose contents is being packaged into the
      JAR.

    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 are
      defined in the plexus FileUtils.getDefaultExcludes()

    useDefaultManifestFile (Default: false)
      Set this to true to enable the use of the defaultManifestFile.

source:test-jar
  Description: This plugin bundles all the test sources into a jar archive.
  Implementation: org.apache.maven.plugin.source.TestSourceJarMojo
  Language: java
  Bound to phase: package
  Before this mojo executes, it will call:
    Phase: ‘generate-sources‘

  Available parameters:

    archive
      The archive configuration to use. See Maven Archiver Reference.

    attach (Default: true)
      User property: attach
      Specifies whether or not to attach the artifact to the project

    excludeResources (Default: false)
      User property: source.excludeResources
      Specifies whether or not to exclude resources from the sources-jar. This
      can be convenient if your project includes large resources, such as
      images, and you don‘t want to include them in the sources-jar.

    excludes
      List of files to exclude. Specified as fileset patterns which are
      relative to the input directory whose contents is being packaged into the
      JAR.

    finalName (Default: ${project.build.finalName})
      The filename to be used for the generated archive file. For the
      source:jar goal, ‘-sources‘ is appended to this filename. For the
      source:test-jar goal, ‘-test-sources‘ is appended.

    forceCreation (Default: false)
      User property: source.forceCreation
      Whether creating the archive should be forced. If set to true, the jar
      will always be created. If set to false, the jar will only be created
      when the sources are newer than the jar.

    includePom (Default: false)
      User property: source.includePom
      Specifies whether or not to include the POM file in the sources-jar.

    includes
      List of files to include. Specified as fileset patterns which are
      relative to the input directory whose contents is being packaged into the
      JAR.

    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 are
      defined in the plexus FileUtils.getDefaultExcludes()

    useDefaultManifestFile (Default: false)
      Set this to true to enable the use of the defaultManifestFile.

source:test-jar-no-fork
  Description: 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 the
    build, and is suitable for attaching to the build lifecycle.
  Implementation: org.apache.maven.plugin.source.TestSourceJarNoForkMojo
  Language: java
  Bound to phase: package

  Available parameters:

    archive
      The archive configuration to use. See Maven Archiver Reference.

    attach (Default: true)
      User property: attach
      Specifies whether or not to attach the artifact to the project

    excludeResources (Default: false)
      User property: source.excludeResources
      Specifies whether or not to exclude resources from the sources-jar. This
      can be convenient if your project includes large resources, such as
      images, and you don‘t want to include them in the sources-jar.

    excludes
      List of files to exclude. Specified as fileset patterns which are
      relative to the input directory whose contents is being packaged into the
      JAR.

    finalName (Default: ${project.build.finalName})
      The filename to be used for the generated archive file. For the
      source:jar goal, ‘-sources‘ is appended to this filename. For the
      source:test-jar goal, ‘-test-sources‘ is appended.

    forceCreation (Default: false)
      User property: source.forceCreation
      Whether creating the archive should be forced. If set to true, the jar
      will always be created. If set to false, the jar will only be created
      when the sources are newer than the jar.

    includePom (Default: false)
      User property: source.includePom
      Specifies whether or not to include the POM file in the sources-jar.

    includes
      List of files to include. Specified as fileset patterns which are
      relative to the input directory whose contents is being packaged into the
      JAR.

    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 are
      defined 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] ------------------------------------------------------------------------

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

时间: 2024-07-30 19:36:38

Maven-07: 插件的自定义绑定的相关文章

Maven之——插件配置与解析

Maven之--插件配置与解析 插件配置就是为绑定插件指定其预定义的参数值.来进一步调整插件目标所执行的任务.可以通过命令行和POM中插件全局配置的形式来配置.不是所有的插件配置都可以通过命令行配置的. 1.    命令行插件配置 如maven-surefire-plugin插件提供一个maven.test.skip参数.当值为true时会跳过执行测试.命令行执行方式: mvninstall –Dmaven.test.skip=true mvn install 可以对比两者在控制台的输出就发现.

knockoutjs + easyui.treegrid 可编辑的自定义绑定插件【转】

目前仅支持URL的CRUD.不需要的话可以却掉相关代码,把treegrid的data直接赋值给viewModel,然后用ko提交整个data 1.支持双击编辑 2.单击Cell,自动保存编辑. 3.4个功能按钮. 插件源码: ko.bindingHandlers.etreegrid = { editing: false, editIndex: 0, init: function (element, valueAccessor, allBindings, viewModel, bindingCon

Maven 的插件和生命周期的绑定

一.Maven 的生命周期 Maven 的生命周期是对所有的构建过程进行抽象和统一.Maven 的生命周期是抽象的,这意味着生命周期本身不做任何实际的工作,生命周期只是定义了一系列的阶段,并确定这些阶段的执行顺序. Maven 有三套相互独立的生命周期,分别是 clean.default和 site.生命周期( lifecycle )由多个阶段( phase )组成,每个阶段( phase )会挂接一到多个goal.Goal 是 maven 里定义任务的最小单元,如下表: 生命周期( Lifec

HighCharts 图表插件 自定义绑定 时间轴数据

HighCharts 图表插件 自定义绑定 时间轴数据,解决时间轴自动显示数据与实际绑定数据时间不对应问题! 可能要用到的源码片段:http://code.662p.com/list/14_1.html     学习示例如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quo

Maven常用插件配置和使用

主要介绍Maven的几个常见第三方插件(cobertura.findbugs.source.assembly.插件开发)配置和使用,接Maven介绍 maven本质上是一个插件框架,它的所有工作都交给插件来做,每个插件可以有多个goal.除了自带的插件之外还有很多比较成熟的第三方插件,我们也很容易上手进行简单的插件开发,下面一一介绍 1 自带插件maven自带的核心插件为Build plugins和Reporting plugins.mvn compile编译源码实际上就利用到了maven-co

生命周期阶段与插件目标任务绑定

1.生命周期   1.1 clean--三个阶段 preclean clean postclean   1.2 default:     --validate    --initialize    --generate-resources   --process-resources     --compile    --process-classes     --generate-test-resources    --process-test-resources     --test-comp

maven常用插件pom配置

一.问题描述: 部署一个maven打包项目时,jar包,依赖lib包全部手动上传至服务器,然后用maven部署报错:Exception in thread "main" java.lang.NoClassDefFoundError:,当时心想可能是依赖的lib包有问题,各种重新部署(以为是依赖的包没有更新),确忽略了一个大问题:pom.xml没仔细检查.解决方法:最终发现<plugin>                <groupId>org.apache.ma

jQuery插件 -- 动态事件绑定插件jquery.livequery.js

http://blog.csdn.net/zzq58157383/article/details/7721974 动态事件绑定插件livequery, 可以利用它给相应的DOM元素注册事件或者触发回调函数.不仅当选择器匹配的元素会被绑定事件,而且后来通过JavaScript添加的元素都会被绑定事件.当元素不再和选择器匹配时,livequery会自动取消事件注册,使得开发者不再需要关注HTML元素的来源,只需要关注如何编写其绑定的事件即可 通过jQuery选择器选择一个DOM元素,livequer

eclipse maven plugin 插件 安装 和 配置

环境准备: eclipse(Helios) 3.6 maven 3.0.4 maven3 安装: 安装 Maven 之前要求先确定你的 JDK 已经安装配置完成.Maven是 Apache 下的一个项目,目前最新版本是 3.0.4,我用的也是这个. 首先去官网下载 Maven:http://www.apache.org/dyn/closer.cgi/maven/binaries/apache-maven-3.0.4-bin.tar.gz 下载完成之后将其解压,我将解压后的文件夹重命名成 mave