除了内置绑定以外,用户还能够自己选择将某个插件目标绑定到生命周期的某个阶段上,这种自定义绑定方式能让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.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] ------------------------------------------------------------------------

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

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

  1. Maven assembly插件进行自定义构建

    众所周知,Maven是一个约定优于配置的java构建工具,通常我们只需要定义非常少的内容,就可以根据package标签属性来构建生成的jar, war包的相关内容. 如果想要对maven中依赖的内容一 ...

  2. Maven自定义绑定插件目标:创建项目的源码jar

    <build> <plugins> <!-- 自定义绑定,创建项目的源码jar --> <plugin> <groupId>org.apac ...

  3. maven学习(五)插件和自定义插件

    插件是可以配置在settings.xml和pom.xml中的 插件目标: 在了解插件和生命周期的绑定关系之前,先来说一下插件目标.在实际项目构建的过程中,需要经历编译.打包等等许许多多的操作,为每个操 ...

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

    一.Maven 的生命周期 Maven 的生命周期是对所有的构建过程进行抽象和统一.Maven 的生命周期是抽象的,这意味着生命周期本身不做任何实际的工作,生命周期只是定义了一系列的阶段,并确定这些阶 ...

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

    HighCharts 图表插件 自定义绑定 时间轴数据,解决时间轴自动显示数据与实际绑定数据时间不对应问题! 可能要用到的源码片段:http://code.662p.com/list/14_1.htm ...

  6. 【Maven实战技巧】「插件使用专题」Maven-Archetype插件创建自定义maven项目骨架

    技术推荐 自定义Archetype Maven骨架/以当前项目为模板创建maven骨架,可以参考http://maven.apache.org/archetype/maven-archetype-pl ...

  7. 13 Maven 编写插件

    Maven 编写插件 Maven 的任何行为都是由插件完成的,包括项目的清理.绵编译.测试以及打包等操作都有其对应的 Maven 插件.每个插件拥有一个或者多个目标,用户可以直接从命令行运行这些插件目 ...

  8. maven常用插件pom配置

    一.问题描述: 部署一个maven打包项目时,jar包,依赖lib包全部手动上传至服务器,然后用maven部署报错:Exception in thread "main" java. ...

  9. maven常用插件总结

    maven本质上是一个插件框架,几乎所有的功能都是通过各种各样的插件来实现的.maven默认会依据项目类型自动把构建时的各阶段(Lifecycle和phase)自动绑定(Lifecycle Mappi ...

随机推荐

  1. 单例模式,堆,BST,AVL树,红黑树

    单例模式 第一种(懒汉,线程不安全): public class Singleton { private static Singleton instance; private Singleton () ...

  2. pc浏览器css和js计算浏览器宽度的差异以及和滚动条的关系

    如图: css宽度:1250 不包括滚动条宽度 用控制台箭头选取元素显示的左边的宽度:1250  不包含滚动条宽度 缩放浏览器右上角显示的宽度:1267 包含了滚动条宽度 再看下控制台: 由此可计算浏 ...

  3. 【前端】Vue2全家桶案例《看漫画》之一、添加四个导航页

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/vue_vux_app_1.html 项目github地址:https://github.com/shamoyuu/ ...

  4. Linux PCI/PCI-E设备配置空间读取与修改

    Linux PCI/PCI-E设备配置空间读取与修改 1 前言 PCI和PCI Express,是计算机常使用的一种高速总线.操作系统中的PCI/PCI-E设备驱动以及操作系统内核,都需要访问PCI及 ...

  5. AVS、MPEG-2、H264标准文档

    联合信源对AVS解码源码和相应的AVS码流.AVS码流太大,可以从http://cosoft.org.cn/projects/avsdec下载.解压avsdec_source.zip后,用VC6编译a ...

  6. Linux下搭建SVN服务器遇到的问题及解决方法,

    1.checkout时,提示:URL svn://192.168.1.99/svntest doesn't exist... 奇怪,怎么会提示库不存在呢?肯定是哪里配置问题.后来尝试了半天,也在网上搜 ...

  7. RFM模型及R语言实现

    每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- 一.基本概念 根据美国数据库营销研究所Arth ...

  8. (二十四)mongodb中group的问题二

    今天的工作还是继续昨天没有完成的,由于对mongodb数据库的不熟悉,导致昨天的思路上也出了一点问题,我需要查询出同一个ruleID中不同的processingID的条数,然后根据条数来排列先后顺序, ...

  9. hdu5977 Garden of Eden

    都不好意思写题解了 跑了4000多ms 纪念下自己A的第二题 (我还有一道freetour II wa20多发没A...呜呜呜 #include<bits/stdc++.h> using ...

  10. QOpenGLTexture 两个纹理叠加

    如何做纹理混合? 方法是,定义多个QOpenGLTexture,然后在fragment shader中添加相应的变量,然后把texture bind到对应的uniform变量上. 废话不多说 text ...