As we all know Maven is a powerful build tool for Java projects. As Maven is developed in Java, it is used more for Java projects. Maven is centered around the concept of POM files (Project Object Model). A POM file is an XML representation of project resources like source code, test code, dependencies (external JARs used) etc. The POM contains references to these resources. The POM file should be in the root directory of the project it belongs to.
POM.xml is read and dependencies are downloaded in local repository. Execute lifecycles, build phases and/or goals. At last execute plugins. Everything executed per selected build profile.
Talking more in details about Build Plugins. These are used to insert extra goals into a build phase. If you need to perform a set of actions for your project which are not covered by standard Maven build phases and goals, you can add a plugin to the POM file. Maven has some standard plugins to use from and if needed a custom maven plugin can also be implemented on your own.
Plugins are the war rooms. They can be used for compilation or creation of binaries along with documentation and report creation. It generally provides a set of goals which can be used according to our needs.
Plugins are specified in pom.xml using plugins element. Plugin execute a particular task based on the goals they are bind in pom.xml file.
If a plugin is not present in local repository, maven will download it and processing will begin.
Writing Custom Maven Plugins
To create your first maven plugin, you must create a new Maven Project in Eclipse with maven-archtype-quickstart
Fill out the Group-Id and Artifact-Id like below :
The generated project contains the class MyMojo which is inherited from AbstractMojo which also implements the method execute().
This execute() method will be entry point for your maven plugin.
Note that the Goal for this Mojo is annotated using Java annotations with the name attribute. It can also be done using XDoclet using goal attribute.
Compiling/Packaging/Installing/Running the project
Issue the following command for :compiling the maven plugin project:
Compilation : mvn compile
Packaging : mvn package
Installing : mvn install (on Windows, the local repository is %USER_HOME%/.m2/repository)
Running : mvn groupId:artifactId:version:goalName
Integrating with pre-existing life-cycle
Here we will see how to bind our plugin(“hello from custom plugin”) to any life cycle.
Take for instance that we have 2 execution phase – compile and install.
So it is apparent now that if we run mvn compile and mvn install for any project that contains the above configuration, goal “hello” will be invoked during phase-compile and phase install.
Alternatively, this can also be achieved using the @phase annotation in the Mojo.
In this article, we discussed about the MOJOs, the extension capabilities provided by Maven for pluging in. Thus, we can see how easy it is to create a Maven plugin.