除了內置綁定以外,用戶還能夠自己選擇將某個插件目標綁定到生命週期的某個階段上,這種自定義綁定方式能讓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] ------------------------------------------------------------------------
我們知道,當插件目標被綁定到不同的生命週期階段的時候,其執行順序會由生命週期階段的先後順序決定。如果多個目標被綁定到同一個階段,它們的執行順序會是怎樣?答案很簡單,當多個插件目標綁定到同一個階段的時候,這些插件聲明的先後順序決定了目標的執行順序。