A custom Maven plugin is Java code packaged as a Maven artifact that hooks into the build lifecycle to perform logic not covered by existing plugins, implemented using Maven's Plugin API (typically via the maven-plugin-plugin and annotations like @Mojo). It's used when standard plugins can't achieve a project-specific requirement.
Key Points: • A Mojo (Maven plain Old Java Object) is the unit of work in a plugin, annotated with @Mojo(name = "...") and bound to a lifecycle phase. • Plugin parameters are injected via @Parameter annotations, letting users configure the plugin's behavior from pom.xml. • The maven-plugin-plugin generates the plugin descriptor needed for Maven to discover and invoke the Mojo. • Custom plugins are useful for organization-specific automation that isn't broadly reusable enough to justify an existing open-source plugin. • Once built, the plugin is installed/published like any artifact and referenced in the consuming project's <build><plugins> section.
Example: A team needing to generate a version-metadata file after every build wrote a Mojo bound to the package phase that reads project properties and writes a version.properties file into the final JAR, something no existing plugin did out of the box.
Code Example:
@Mojo(name = "generate-version", defaultPhase = LifecyclePhase.PACKAGE)
public class VersionMojo extends AbstractMojo {
@Parameter(defaultValue = "${project.version}", readonly = true)
private String version;
public void execute() {
getLog().info("Writing version: " + version);
}
}Interview Tip: A concise interview answer is:
"When I needed build automation that no existing plugin covered, I wrote a custom Maven plugin using the Plugin API, defining a Mojo with @Mojo bound to the right lifecycle phase and exposing configuration through @Parameter. The main challenge was learning the plugin development framework itself, but once built it hooked cleanly into the existing build without disrupting other teams' workflows."