The API for plugins - Mojos - development
npx @tessl/cli install tessl/maven-org-apache-maven--maven-plugin-api@3.9.0The API for plugins - Mojos - development. This library provides the core interfaces, classes, and abstractions needed to develop Maven plugins that extend Maven's functionality during the build lifecycle.
pom.xml dependencies:<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.9.11</version>
</dependency>import org.apache.maven.plugin.Mojo;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.monitor.logging.DefaultLog;import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
/**
* Example Maven plugin that demonstrates basic usage
* @goal hello
*/
public class HelloMojo extends AbstractMojo {
/**
* @parameter property="greeting" default-value="Hello World!"
*/
private String greeting;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
Log log = getLog();
log.info(greeting);
try {
// Plugin logic here
log.debug("Executing hello goal");
} catch (Exception e) {
throw new MojoExecutionException("Unexpected error", e);
}
}
}The Maven Plugin API is built around several key components:
Essential interfaces and base classes for creating Maven plugins. Provides the fundamental Mojo contract and ready-to-use AbstractMojo base class with logging and context support.
public interface Mojo {
String ROLE = Mojo.class.getName();
void execute() throws MojoExecutionException, MojoFailureException;
void setLog(Log log);
Log getLog();
}
public abstract class AbstractMojo implements Mojo, ContextEnabled {
public void setLog(Log log);
public Log getLog();
public Map getPluginContext();
public void setPluginContext(Map pluginContext);
}Exception classes for proper error reporting during plugin execution. Distinguishes between unexpected errors and expected failures to provide appropriate Maven build messages.
public class MojoExecutionException extends AbstractMojoExecutionException {
public MojoExecutionException(String message);
public MojoExecutionException(String message, Exception cause);
public MojoExecutionException(String message, Throwable cause);
public MojoExecutionException(Object source, String shortMessage, String longMessage);
}
public class MojoFailureException extends AbstractMojoExecutionException {
public MojoFailureException(String message);
public MojoFailureException(String message, Throwable cause);
public MojoFailureException(Object source, String shortMessage, String longMessage);
}
public class MojoNotFoundException extends Exception {
public MojoNotFoundException(String goal, PluginDescriptor pluginDescriptor);
public String getGoal();
public PluginDescriptor getPluginDescriptor();
}Standardized logging interface for Maven plugins with support for debug, info, warn, and error levels. Integrates with Maven's output system and supports both message-only and message-with-throwable logging.
public interface Log {
boolean isDebugEnabled();
void debug(CharSequence content);
void debug(CharSequence content, Throwable error);
void debug(Throwable error);
boolean isInfoEnabled();
void info(CharSequence content);
void info(CharSequence content, Throwable error);
void info(Throwable error);
boolean isWarnEnabled();
void warn(CharSequence content);
void warn(CharSequence content, Throwable error);
void warn(Throwable error);
boolean isErrorEnabled();
void error(CharSequence content);
void error(CharSequence content, Throwable error);
void error(Throwable error);
}Metadata classes for describing plugins, mojos, and their parameters. Used by Maven tools, IDEs, and the runtime to understand plugin structure and configuration options.
public class PluginDescriptor extends ComponentSetDescriptor implements Cloneable {
public String getGroupId();
public String getArtifactId();
public String getVersion();
public String getGoalPrefix();
public List<MojoDescriptor> getMojos();
public MojoDescriptor getMojo(String goal);
public void addMojo(MojoDescriptor mojoDescriptor) throws DuplicateMojoDescriptorException;
}
public class MojoDescriptor extends ComponentDescriptor<Mojo> implements Cloneable {
public static final String SINGLE_PASS_EXEC_STRATEGY = "once-per-session";
public static final String MULTI_PASS_EXEC_STRATEGY = "always";
public String getGoal();
public String getPhase();
public List<Parameter> getParameters();
public Map<String, Parameter> getParameterMap();
}public interface ContextEnabled {
void setPluginContext(Map pluginContext);
Map getPluginContext();
}
public abstract class AbstractMojoExecutionException extends Exception {
protected Object source;
protected String longMessage;
public String getLongMessage();
public Object getSource();
}
public class SystemStreamLog implements Log {
// Default Log implementation using System.out and System.err
}
public class DefaultLog implements Log {
/**
* Create DefaultLog wrapping Plexus logger
* @param logger Plexus logger instance
*/
public DefaultLog(Logger logger);
}