My notes about how to start developing Maven plugins, based on https://maven.apache.org/guides/plugin/guide-java-plugin-development.html.
Table of contents
Maven Plugins
- the properties to configure are defined in plugin goals
- the versions of dependencies in the plugin override the dependency versions of the project
- configuration can be placed:
- under the plugin node - it is the global plugin configuration
- under the executions/execution node - it is the configuration for this particular execution
Below are the minimal sample steps of developing a Maven plugin:
1. Define the plugin
package sample.plugin;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
/**
* Says "Hi" to the user.
*
*/
@Mojo( name = "sayhi")
public class GreetingMojo extends AbstractMojo
{
public void execute() throws MojoExecutionException
{
getLog().info( "Hello, world." );
}
}
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>sample.plugin</groupId>
<artifactId>hello-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
<name>Sample Parameter-less Maven Plugin</name>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
</project>
2. Add the plugin to another project
<build>
<plugins>
<plugin>
<groupId>sample.plugin</groupId>
<artifactId>hello-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
</plugin>
</plugins>
</build>
3. Run the plugin
mvn sample.plugin:hello-maven-plugin:1.0-SNAPSHOT:sayhi
mvn hello:sayhi
4. Attach the plugin to a build lifecycle phase
<build>
<plugins>
<plugin>
<groupId>sample.plugin</groupId>
<artifactId>hello-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>sayhi</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Maven Archetypes
"Maven archetype" is a short name for "maven archetype plugin"!
For example the maven-archetype-quickstart plugin we run like this:
>mvn archetype:generate
-DgroupId=com.companyname.bank
-DartifactId=consumerBanking
-DarchetypeArtifactId=maven-archetype-quickstart
-DinteractiveMode=false</div>
Comments
Want to leave a comment? Visit this post's issue page
on GitHub and just post your comment as the issue's comment (you'll need a GitHub account. What? Like you don't have one yet?!).
Comments: