Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original
Maven Plugins
Maven Plugins
A Maven plugin is an executable component that extends Maven's functionality, each plugin contains one or more goals (goals), used to perform specific tasks (such as compiling, testing, packaging, etc.).
The relationship between plugins and the lifecycle:
Lifecycle (Lifecycle): defines the order of build phases (phase)
Plugin (Plugin): provides concrete implementations (goal) to complete the tasks of these phases
Maven has the following three standard lifecycles:
clean: handling of project cleanup
default (or build): handling of project deployment
site: handling of project site documentation creation
Each lifecycle contains a series of phases (phase). These phases are equivalent to the unified interface provided by Maven, and the implementation of these phases is completed by Maven's plugins.
When we enter mvn commands, such as mvn clean, clean corresponds to the clean phase in the Clean lifecycle. But the specific operation of clean is performed by maven-clean-plugin method internally calls
So the concrete implementation of each phase of the Maven lifecycle is implemented by Maven plugins.
Maven is actually a framework that relies on plugin execution, each task is actually completed by a plugin. Maven plugins are commonly used to:
Create jar files
Create war files
Compile code files
Unit test code
Create project documentation
Create project reports
Plugins usually provide a collection of goals, and can be executed using the following syntax:
mvn [plugin-name]:[goal-name]
For example, a Java project can be compiled using the compile-goal of maven-compiler-plugin, using the following command:
mvn compiler:compile
Plugin Core Mechanisms
Plugin Coordinates
Similar to dependencies, plugins are also uniquely identified by groupId, artifactId, version:
property
Description
Common values
groupId
Plugin organization
org.apache.maven.plugins(official plugins)
artifactId
Plugin name
maven-compiler-plugin
version
Plugin version
3.8.1
Plugin Goals (Goals)
Each plugin can contain multiple goals, for example:
maven-compiler-plugin:
compile: compile main code
testCompile: compile test code
-
compile: compile main code
-
testCompile: compile test code
maven-surefire-plugin:
test: run unit tests
-
test: run unit tests
Plugin Binding Methods
1. Built-in binding
Maven has pre-bound plugin goals for lifecycle phases:
The Java class corresponding to a plugin goal is called Mojo(Maven plain Old Java Object)。
must extend AbstractMojo and implement execute() method.
Simple Plugin Example
Example
@Mojo
(
name
=
"greet"
, defaultPhase
=
LifecyclePhase.
COMPILE
)
public
class
GreetingMojo
extends
AbstractMojo
{
@Parameter
(
property
=
"name"
, defaultValue
=
"World"
)
private
String
name
;
public
void
execute
(
)
throws
MojoExecutionException
{
getLog
(
)
.
info
(
"Hello, "
name
"!"
)
;
}
}
Package and install the plugin
mvn clean install # 将插件安装到本地仓库
Plugin Types
Maven provides the following two types of plugins:
Type
Description
Build plugins
Executed at build time, and configured in pom.xml's element.
Reporting plugins
Executed during site generation, and in pom.xml's element.
The following is a list of some common plugins:
Plugin
Description
clean
Clean target files after build. Delete the target directory.
compiler
Compile Java source files.
surefile
Run JUnit unit tests. Create test reports.
jar
Build a JAR file from the current project.
war
Build a WAR file from the current project.
javadoc
Generate Javadoc for the project.
antrun
Run a collection of ant tasks from any phase of the build process.
Example
We have made extensive use in our examples of maven-antrun-plugin to output data to the console. Please see Maven - Build Profiles chapter. Let's understand this content in a better way, by creating a pom.xml file in the C:\MVN\project directory.