Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Apache Maven plug-in


May 26, 2021 Maven


Table of contents


Maven - plug-in

What is a Maven plug-in?

Maven is actually a framework that relies on plug-in execution, and each task is actually done by the plug-in. Maven plug-ins are often used to:

  • Create a jar file
  • Create a war file
  • Compile the code file
  • Code unit tests
  • Create a project document
  • Create an engineering report

Plug-ins typically provide a collection of targets and can be executed using the following syntax:

mvn [plugin-name]:[goal-name]

For example, a Java project can be compiled using maven-compiler-plugin's compile-goal, using the following command:

mvn compiler:compile

The type of plug-in

Maven provides two types of plug-ins:

Type Describe
Build plugins Executed at build time and configured .xml element of the pom.
Reporting plugins Performs during site generation and is configured in the .xml of the pom."

Here's a list of some common plug-ins:

Plug - ins Describe
clean Clean up the target file after construction. Delete the target directory.
compiler Compile the Java source file.
surefile Run JUnit unit tests. Create a test report.
Jar Build the JAR file from the current project.
war Build a WAR file from the current project.
Javadoc Build Javadoc for the project.
antrun Run a collection of ant tasks from any stage of the build process.

Example

We've used maven-antrun-plugin a lot in our example to output data to the console. C heck out the Maven - Build Profiles section. Let's understand this in a better way, creating a pom-and-file in the C:?MVN.xml 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 to jump to the directory .xml where the pom terminal is located, and execute the following mvn command.

C:\MVN\project>mvn clean

Maven will begin processing and displaying 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 shows the following key concepts:

  • Plug-ins are .xml the plugins element in the pom element.
  • Each plug-in can have multiple targets.
  • You can define a stage, and the plug-in uses its phase element to start processing. We've already used the clean phase.
  • You can configure the tasks to perform by binding to the target of the plug-in. We have bound the echo task to the run target of maven-antrun-plugin.
  • That's it, Maven will take care of the rest. It will download plug-ins that are not available in the local repository and start processing.