Maven - Maven Plugin - maven tutorial
maven tutorial tags : apache maven , maven repository , maven central
What is Maven 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 and which can be executed using following syntax:
learn maven tutorial - apache maven maven plugin based architecture - Apache Maven example programs
Learn Maven Tutorial - Maven Plugin - Maven Example
- For example, a Java project can be compiled with the maven-compiler-plugin's compile-goal by running following command
Plugin Types
- Maven provided following two types of Plugins:
Type | Description |
---|---|
Build plugins | They execute during the build and should be configured in the <build/> element of pom.xml |
Reporting plugins | They execute during the site generation and they should be configured in the <reporting/> element of the pom.xml |
- Following is the list of few common plugins:
Plugin | Description |
---|---|
clean | Clean up target after the build. Deletes the target directory. |
compiler | Compiles Java source files. |
surefire | Run the 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 mentioned of the build. |
Example:
- We've used maven-antrun-plugin extensively in our examples to print data on console.
- Then create a pom.xml in C:\MVN\project folder.
maven tutorial tags : apache maven , maven repository , maven central
Sample Code
- Next, open command console and go to the folder containing pom.xml and execute the following mvn command.
- Maven will start processing and show clean phase of clean life cycle
maven tutorial tags : apache maven , maven repository , maven central
Output
The above example illustrates the following key concepts:
- Plugins are specified in pom.xml using plugins element.
- Each plugin can have multiple goals.
- You can define phase from where plugin should starts its processing using its phase element. We've used clean phase.
- You can configure tasks to be executed by binding them to goals of plugin.\
- We've bound echo task with run goal of maven-antrun-plugin.
- That's it, Maven will handle the rest. It will download the plugin if not available in local repository and starts its processing.