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:

Maven has the following three standard lifecycles:

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:

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:

-

compile: compile main code

-

testCompile: compile test code

-

test: run unit tests

Plugin Binding Methods

1. Built-in binding

Maven has pre-bound plugin goals for lifecycle phases:

Lifecycle phase Bound plugin goals
compile maven-compiler-plugin:compile
test maven-surefire-plugin:test
package maven-jar-plugin:jar(or maven-war-plugin:war)

2. Custom binding

Explicitly configure in pom.xml:





    

        

            org.apache.maven.plugins

            maven-compiler-plugin

            3.8.1

            

                

                    compile  

                    

                        compile

                    

                

            

        

    




Common Official Plugins

Compilation related

Plugin Purpose Common goals
maven-compiler-plugin Compile Java code compile, testCompile
maven-resources-plugin Process resource files resources, testResources

Configuration example:





    org.apache.maven.plugins

    maven-compiler-plugin

    3.8.1

    

        11  

        11

    



Testing related

Plugin Purpose Common goals
maven-surefire-plugin Run unit tests test
maven-failsafe-plugin Run integration tests integration-test

Skip test configuration:





    org.apache.maven.plugins

    maven-surefire-plugin

    2.22.2

    

        true  

    



Packaging related

Plugin Purpose Common goals
maven-jar-plugin Package JAR files jar
maven-war-plugin Package WAR files war
maven-assembly-plugin Custom packaging single

Executable JAR configuration:





    org.apache.maven.plugins

    maven-jar-plugin

    3.2.0

    

        

            

                com.example.Main  

            

        

    



Deployment related

Plugin Purpose Common goals
maven-install-plugin Install to the local repository install
maven-deploy-plugin Deploy to a remote repository deploy

Plugin Invocation Methods

Invoke through lifecycle:



mvn compile  # 触发 maven-compiler-plugin:compile

Directly invoke plugin goals:



mvn compiler:compile  # 直接调用编译目标

Command-line parameter configuration:



mvn compiler:compile -Dmaven.compiler.source=11

Custom Plugin Configuration

Parameter configuration

Set through the configuration tag:





    org.apache.maven.plugins

    maven-surefire-plugin

    2.22.2

    

        

            **/*Test.java  

        

    



Multi-goal execution:





    

        run-tests

        test

        

            test

        

    




Plugin Development Basics

Mojo Concept

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.

<

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.companyname.projectgroup

groupId

<

artifactId

project

artifactId

<

version

1.0

version

<

build

<

plugins

<

plugin

<

groupId

org.apache.maven.plugins

groupId

<

artifactId

maven-antrun-plugin

artifactId

<

version

1.1

version

<

executions

<

execution

<

id

id.clean

id

<

phase

clean

phase

<

goals

<

goal

run

goal

goals

<

configuration

<

tasks

<

echo

clean phase

echo

tasks

configuration

execution

executions

plugin

plugins

build

project

Next, open the command terminal, navigate to the directory where pom.xml is located, and execute the following mvn command.

mvn clean

Maven will start processing and display the clean phase of the clean lifecycle.

[INFO] Scanning for projects...

[INFO] ------------------------------------------------------------------

[INFO] Building Unnamed - com.companyname.projectgroup:project:jar:1.0

[INFO]    task-segment: [post-clean]

[INFO] ------------------------------------------------------------------

[INFO] [clean:clean {execution: default-clean}]

[INFO] [antrun:run {execution: id.clean}]

[INFO] Executing tasks

     [echo] clean phase

[INFO] Executed tasks

[INFO] ------------------------------------------------------------------

[INFO] BUILD SUCCESSFUL

[INFO] ------------------------------------------------------------------

[INFO] Total time: < 1 second

[INFO] Finished at: Sat Jul 07 13:38:59 IST 2012

[INFO] Final Memory: 4M/44M

[INFO] ------------------------------------------------------------------

The above example demonstrates the following key concepts: