- 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 |
1 | Build plugins | They execute during the build process and should be configured in the <build/> element of pom.xml.Part of Build life cycle |
2 | Reporting plugins | They execute during the site generation process and they should be configured in the <reporting/> element of the pom.xml.Part of Site life cycle |
1 | clean | Cleans up the target after the build. Deletes the target directory and artifact |
2 | compiler | Compiles Java source files. |
3 | surefire | Runs the JUnit unit tests. Creates the test reports. |
4 | jar | Build artifact with jar format for the project |
5 | war | Build artifact with war format for the project |
6 | antrun | Runs a set of ant tasks from any phase mentioned of the build. |
7 | javadoc | Generates Javadoc for the project. |
<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> |
Move to the directory, when we have the maven project(Where we have the pom.xml) | cd <project directory> |
Run the command | C:\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> section | Contains plugin(<plugin>) one after one |
2 | groupId | groupId |
3 | artifactId | artifactId |
4 | version | version |
5 | <executions> section | Contains 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.