SAGARFIVE

Tutorials

3.10 Plugins

  • Maven is actually a plugin execution framework where every task is actually done by plugins. Maven Plugins are generally used to
  • create jar file
  • create war file
  • compile code files
  • unit testing of code
  • create project documentation
  • create project reports
  • A plugin generally provides a set of goals, which can be executed using the following syntax −

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

Maven Plugin Types :

1Build pluginsThey execute during the build process and should be configured in the <build/> element of pom.xml.Part of Build life cycle

2
Reporting pluginsThey execute during the site generation process and they should be configured in the <reporting/> element of the pom.xml.Part of Site life cycle


Some of plugins :

1cleanCleans up the target after the build. Deletes the target directory and artifact
2compilerCompiles Java source files.
3surefireRuns the JUnit unit tests. Creates the test reports.
4jarBuild artifact with jar format for the project
5warBuild artifact with war format for the project
6antrunRuns a set of ant tasks from any phase mentioned of the build.
7javadocGenerates Javadoc for the project.

Example :

<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.orgname.orggroup</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>

To execute this code on cmd :

Move to the directory, when we have the maven project(Where we have the pom.xml)cd <project directory>
Run the commandC:\users\gajan\artifactId-MavenApp > mvn clean

The response from the console :

C:\users\gajan\artifactId-MavenApp>mvn clean[INFO] Scanning for projects…[INFO][INFO] —————-< com.companyname.projectgroup:project >—————-[INFO] Building project 1.0[INFO] ——————————–[ jar ]———————————[INFO][INFO] — maven-clean-plugin:2.5:clean (default-clean) @ project —[INFO] Deleting C:\users\gajan\artifactId-MavenApp\target[INFO][INFO] — maven-antrun-plugin:1.1:run (id.clean) @ project —[INFO] Executing tasks       [echo] clean phase[INFO] Executed tasks[INFO] ————————————————————————[INFO] BUILD SUCCESS[INFO] ————————————————————————[INFO] Total time:  1.266 s[INFO] Finished at: 2023-05-16T13:46:11+05:30[INFO] ————————————————————————

Plugin configuration in pom.xml :


<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>

1<plugins> sectionContains plugin(<plugin>) one after one
2groupIdgroupId
3artifactIdartifactId
4versionversion
5<executions> sectionContains execution(<execution>) one after oneStagesIdPhaseGoals > goal one after oneConfigurations > tasks(linux commands /commands) one after one

Keynotes :

  • Plugins are specified in pom.xml using the plugins element.
  • Each plugin can have multiple goals.
  • You can define the phase from where the plugin should start its processing using its phase element. We’ve used the clean phase.
  • You can configure tasks to be executed by binding them to the goals of the plugin. We’ve bound echo tasks with run goal of maven-antrun-plugin.
  • Maven will then download the plugin if not available in the local repository and start its processing.