Easy Tutorial
❮ Maven Build Test Project Maven Tutorial ❯

Maven Plugins

Maven has the following three standard lifecycles:

Each lifecycle contains a series of phases. These phases serve as unified interfaces provided by Maven, and their implementations are completed by Maven plugins.

When we input a Maven command, such as mvn clean, clean corresponds to the clean phase in the Clean lifecycle. However, the specific operations of clean are implemented by the maven-clean-plugin.

Therefore, the implementation of each phase in the Maven lifecycle is accomplished by Maven plugins.

Maven is essentially a framework that relies on plugins to execute, with each task being completed by a plugin. Maven plugins are commonly used for:

Plugins typically offer 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 the maven-compiler-plugin with the following command:

mvn compiler:compile

Plugin Types

Maven provides the following two types of plugins:

Type Description
Build plugins Execute during the build and are configured in the pom.xml under the <build> element.
Reporting plugins Execute during the site generation and are configured in the pom.xml under the <reporting> element.

Below is a list of some commonly used plugins:

Plugin Description
clean Cleans up target files after the build. Deletes the target directory.
compiler Compiles Java source files.
surefire Runs JUnit unit tests. Creates test reports.
jar Builds a JAR file from the current project.
war Builds a WAR file from the current project.
javadoc Generates Javadoc for the project.
antrun Runs a set of Ant tasks from any phase in the build process.

Example

We have extensively used the maven-antrun-plugin in our examples to output data to the console. Please refer to the Maven - Build Profiles section. Let's understand this better by creating a pom.xml file in the C:\MVN\project directory.

&lt;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 containing the pom.xml file, 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 example above illustrates the following key concepts:

❮ Maven Build Test Project Maven Tutorial ❯