Maven Core classes - the core engine of Apache Maven build system
—
Maven Core's plugin system provides comprehensive APIs for managing, loading, configuring, and executing Maven plugins. This includes plugin discovery, classloader management, caching mechanisms, and execution coordination. The plugin system is central to Maven's extensible architecture.
Primary interface for plugin management operations including plugin loading, realm setup, and Mojo configuration.
public interface MavenPluginManager {
/**
* Get plugin descriptor for a plugin.
*
* @param plugin the plugin to get descriptor for
* @param repositories list of repositories to search
* @param session repository system session
* @return plugin descriptor containing plugin metadata
* @throws PluginDescriptorParsingException if descriptor parsing fails
* @throws InvalidPluginDescriptorException if descriptor is invalid
*/
PluginDescriptor getPluginDescriptor(Plugin plugin, List<RemoteRepository> repositories,
RepositorySystemSession session) throws PluginDescriptorParsingException, InvalidPluginDescriptorException;
/**
* Setup plugin classloader realm.
*
* @param pluginDescriptor plugin metadata
* @param session Maven session
* @param parent parent classloader
* @param imports list of package imports to expose
* @param filter dependency filter for plugin dependencies
* @return configured class realm for plugin
* @throws PluginManagerException if realm setup fails
*/
ClassRealm setupPluginRealm(PluginDescriptor pluginDescriptor, MavenSession session,
ClassLoader parent, List<String> imports, DependencyFilter filter) throws PluginManagerException;
/**
* Get configured Mojo instance.
*
* @param mojoInterface the Mojo interface class
* @param session Maven session
* @param mojoExecution mojo execution descriptor
* @return configured Mojo instance ready for execution
* @throws PluginConfigurationException if configuration fails
* @throws PluginContainerException if container operations fail
*/
<T> T getConfiguredMojo(Class<T> mojoInterface, MavenSession session, MojoExecution mojoExecution)
throws PluginConfigurationException, PluginContainerException;
/**
* Release Mojo resources.
*
* @param mojo the mojo instance to release
* @param mojoExecution mojo execution descriptor
* @param session Maven session
*/
void releaseMojo(Object mojo, MojoExecution mojoExecution, MavenSession session);
/**
* Extract plugin artifacts for dependency resolution.
*
* @param plugin the plugin
* @param artifact the plugin artifact
* @param session Maven session
* @return plugin artifact resolution result
* @throws PluginResolutionException if resolution fails
*/
PluginArtifactsResult resolvePluginArtifacts(Plugin plugin, Artifact artifact, MavenSession session)
throws PluginResolutionException;
}Interface for build-time plugin operations including plugin loading and execution.
public interface BuildPluginManager {
/**
* Load plugin for execution.
*
* @param plugin plugin configuration
* @param repositories list of plugin repositories
* @param session repository system session
* @return loaded plugin descriptor
* @throws PluginNotFoundException if plugin cannot be found
* @throws PluginResolutionException if plugin resolution fails
*/
PluginDescriptor loadPlugin(Plugin plugin, List<RemoteRepository> repositories,
RepositorySystemSession session) throws PluginNotFoundException, PluginResolutionException;
/**
* Execute a Mojo.
*
* @param session Maven session
* @param execution mojo execution configuration
* @throws MojoFailureException if mojo execution fails
* @throws MojoExecutionException if mojo execution encounters error
* @throws PluginConfigurationException if plugin configuration is invalid
* @throws PluginManagerException if plugin management fails
*/
void executeMojo(MavenSession session, MojoExecution execution)
throws MojoFailureException, MojoExecutionException, PluginConfigurationException, PluginManagerException;
/**
* Get plugin classloader realm.
*
* @param session Maven session
* @param pluginDescriptor plugin metadata
* @return plugin class realm
* @throws PluginManagerException if realm access fails
*/
ClassRealm getPluginRealm(MavenSession session, PluginDescriptor pluginDescriptor) throws PluginManagerException;
}Plugin Management Example:
import org.apache.maven.plugin.MavenPluginManager;
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugin.PluginDescriptor;
import org.apache.maven.plugin.MojoExecution;
@Component
private MavenPluginManager pluginManager;
@Component
private BuildPluginManager buildPluginManager;
public void executeCustomPlugin(MavenSession session) throws Exception {
// Create plugin reference
Plugin plugin = new Plugin();
plugin.setGroupId("org.apache.maven.plugins");
plugin.setArtifactId("maven-compiler-plugin");
plugin.setVersion("3.11.0");
// Load plugin
PluginDescriptor pluginDescriptor = buildPluginManager.loadPlugin(
plugin, session.getCurrentProject().getPluginArtifactRepositories(),
session.getRepositorySession());
// Create mojo execution
MojoExecution execution = new MojoExecution(pluginDescriptor.getMojo("compile"));
execution.setLifecyclePhase("compile");
// Configure execution
Xpp3Dom configuration = new Xpp3Dom("configuration");
Xpp3Dom source = new Xpp3Dom("source");
source.setValue("17");
configuration.addChild(source);
Xpp3Dom target = new Xpp3Dom("target");
target.setValue("17");
configuration.addChild(target);
execution.setConfiguration(configuration);
// Execute mojo
buildPluginManager.executeMojo(session, execution);
System.out.println("Plugin executed successfully: " +
pluginDescriptor.getGroupId() + ":" +
pluginDescriptor.getArtifactId() + ":" +
pluginDescriptor.getVersion());
}Represents the execution context and configuration for a single Mojo.
public class MojoExecution {
/**
* Create mojo execution for a mojo descriptor.
*
* @param mojoDescriptor the mojo to execute
*/
public MojoExecution(MojoDescriptor mojoDescriptor);
/**
* Create mojo execution with execution ID.
*
* @param mojoDescriptor the mojo to execute
* @param executionId unique execution identifier
*/
public MojoExecution(MojoDescriptor mojoDescriptor, String executionId);
/**
* Create mojo execution with configuration.
*
* @param mojoDescriptor the mojo to execute
* @param configuration mojo configuration
*/
public MojoExecution(MojoDescriptor mojoDescriptor, PlexusConfiguration configuration);
// Execution Identity
public String getExecutionId();
public void setExecutionId(String executionId);
// Plugin Information
public Plugin getPlugin();
public void setPlugin(Plugin plugin);
// Goal Information
public String getGoal();
public void setGoal(String goal);
// Lifecycle Information
public String getLifecyclePhase();
public void setLifecyclePhase(String lifecyclePhase);
// Mojo Descriptor
public MojoDescriptor getMojoDescriptor();
public void setMojoDescriptor(MojoDescriptor mojoDescriptor);
// Configuration
public PlexusConfiguration getConfiguration();
public void setConfiguration(PlexusConfiguration configuration);
// Source Location
public InputLocation getLocation(Object key);
public void setLocation(Object key, InputLocation location);
// String representation
public String toString();
public String identify();
}Mojo Execution Example:
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
public MojoExecution createCompilerExecution() {
// Get mojo descriptor (typically from plugin descriptor)
MojoDescriptor compilerMojo = pluginDescriptor.getMojo("compile");
// Create execution
MojoExecution execution = new MojoExecution(compilerMojo, "default-compile");
execution.setLifecyclePhase("compile");
// Configure mojo parameters
Xpp3Dom config = new Xpp3Dom("configuration");
Xpp3Dom source = new Xpp3Dom("source");
source.setValue("17");
config.addChild(source);
Xpp3Dom target = new Xpp3Dom("target");
target.setValue("17");
config.addChild(target);
Xpp3Dom encoding = new Xpp3Dom("encoding");
encoding.setValue("UTF-8");
config.addChild(encoding);
execution.setConfiguration(new XmlPlexusConfiguration(config));
return execution;
}
// Execute with build plugin manager
public void executeCompilation(MavenSession session, MojoExecution execution) throws Exception {
try {
buildPluginManager.executeMojo(session, execution);
System.out.println("Compilation successful");
} catch (MojoFailureException e) {
System.err.println("Compilation failed: " + e.getMessage());
throw e;
} catch (MojoExecutionException e) {
System.err.println("Compilation error: " + e.getMessage());
throw e;
}
}Caches plugin classloader realms for performance optimization.
public interface PluginRealmCache {
/**
* Cache key for plugin realm entries.
*/
interface Key {
// Key implementation details
}
/**
* Cached plugin realm record.
*/
class CacheRecord {
public ClassRealm getRealm();
public List<Artifact> getArtifacts();
public List<ComponentDescriptor<?>> getComponentDescriptors();
}
/**
* Get cached plugin realm.
*
* @param key cache key for the plugin
* @return cached realm record or null if not cached
*/
CacheRecord get(Key key);
/**
* Put plugin realm in cache.
*
* @param key cache key
* @param record realm record to cache
* @return previous cached record or null
*/
CacheRecord put(Key key, CacheRecord record);
/**
* Remove plugin realm from cache.
*
* @param key cache key
* @return removed record or null if not present
*/
CacheRecord remove(Key key);
/**
* Clear all cached plugin realms.
*/
void clear();
/**
* Register cache cleanup callback.
*
* @param record realm record to monitor
* @param callback cleanup callback
*/
void register(MavenProject project, Key key, CacheRecord record, CacheCleanupCallback callback);
}Caches plugin descriptors to avoid repeated parsing and loading.
public interface PluginDescriptorCache {
/**
* Cache key for plugin descriptors.
*/
interface Key {
// Key implementation details
}
/**
* Get cached plugin descriptor.
*
* @param key cache key
* @return cached plugin descriptor or null if not cached
*/
PluginDescriptor get(Key key);
/**
* Put plugin descriptor in cache.
*
* @param key cache key
* @param descriptor plugin descriptor to cache
* @return previous cached descriptor or null
*/
PluginDescriptor put(Key key, PluginDescriptor descriptor);
/**
* Remove plugin descriptor from cache.
*
* @param key cache key
* @return removed descriptor or null if not present
*/
PluginDescriptor remove(Key key);
/**
* Clear all cached plugin descriptors.
*/
void clear();
}Caches resolved plugin artifact dependencies.
public interface PluginArtifactsCache {
/**
* Cache key for plugin artifacts.
*/
interface Key {
// Key implementation details
}
/**
* Get cached plugin artifacts.
*
* @param key cache key
* @return cached artifacts or null if not cached
*/
PluginArtifactsResult get(Key key);
/**
* Put plugin artifacts in cache.
*
* @param key cache key
* @param result artifacts result to cache
* @return previous cached result or null
*/
PluginArtifactsResult put(Key key, PluginArtifactsResult result);
/**
* Remove plugin artifacts from cache.
*
* @param key cache key
* @return removed result or null if not present
*/
PluginArtifactsResult remove(Key key);
/**
* Clear all cached plugin artifacts.
*/
void clear();
}Caching Usage Example:
import org.apache.maven.plugin.PluginRealmCache;
import org.apache.maven.plugin.PluginDescriptorCache;
@Component
private PluginRealmCache realmCache;
@Component
private PluginDescriptorCache descriptorCache;
public PluginDescriptor getCachedPluginDescriptor(Plugin plugin, RepositorySystemSession session) {
// Create cache key (implementation specific)
PluginDescriptorCache.Key key = createDescriptorKey(plugin, session);
// Try cache first
PluginDescriptor descriptor = descriptorCache.get(key);
if (descriptor != null) {
System.out.println("Plugin descriptor found in cache");
return descriptor;
}
// Load and cache descriptor
try {
descriptor = pluginManager.getPluginDescriptor(plugin, repositories, session);
descriptorCache.put(key, descriptor);
System.out.println("Plugin descriptor loaded and cached");
return descriptor;
} catch (Exception e) {
System.err.println("Failed to load plugin descriptor: " + e.getMessage());
throw new RuntimeException(e);
}
}
public ClassRealm getCachedPluginRealm(PluginDescriptor descriptor, MavenSession session) {
// Create cache key
PluginRealmCache.Key key = createRealmKey(descriptor, session);
// Check cache
PluginRealmCache.CacheRecord record = realmCache.get(key);
if (record != null) {
System.out.println("Plugin realm found in cache");
return record.getRealm();
}
// Setup and cache realm
try {
ClassRealm realm = pluginManager.setupPluginRealm(descriptor, session, null, null, null);
PluginRealmCache.CacheRecord newRecord = new PluginRealmCache.CacheRecord();
// Set record properties...
realmCache.put(key, newRecord);
System.out.println("Plugin realm created and cached");
return realm;
} catch (Exception e) {
System.err.println("Failed to setup plugin realm: " + e.getMessage());
throw new RuntimeException(e);
}
}Resolves plugin prefixes to full plugin coordinates.
public interface PluginPrefixResolver {
/**
* Resolve plugin prefix to plugin coordinates.
*
* @param prefix plugin prefix (e.g., "compiler", "surefire")
* @param pluginGroups list of plugin groups to search
* @param session repository system session
* @return plugin prefix resolution result
* @throws PluginPrefixResolutionException if resolution fails
*/
PluginPrefixResult resolve(String prefix, List<String> pluginGroups,
RepositorySystemSession session) throws PluginPrefixResolutionException;
}Resolves plugin versions when not explicitly specified.
public interface PluginVersionResolver {
/**
* Resolve plugin version.
*
* @param request plugin version resolution request
* @return plugin version resolution result
* @throws PluginVersionResolutionException if resolution fails
*/
PluginVersionResult resolve(PluginVersionRequest request) throws PluginVersionResolutionException;
}Plugin Resolution Example:
import org.apache.maven.plugin.prefix.PluginPrefixResolver;
import org.apache.maven.plugin.version.PluginVersionResolver;
@Component
private PluginPrefixResolver prefixResolver;
@Component
private PluginVersionResolver versionResolver;
public Plugin resolvePluginFromPrefix(String prefix, MavenSession session) throws Exception {
// Resolve prefix to coordinates
List<String> pluginGroups = Arrays.asList(
"org.apache.maven.plugins",
"org.codehaus.mojo"
);
PluginPrefixResult prefixResult = prefixResolver.resolve(
prefix, pluginGroups, session.getRepositorySession());
// Create plugin with resolved coordinates
Plugin plugin = new Plugin();
plugin.setGroupId(prefixResult.getGroupId());
plugin.setArtifactId(prefixResult.getArtifactId());
// Resolve version if not specified
if (plugin.getVersion() == null) {
PluginVersionRequest versionRequest = new DefaultPluginVersionRequest(plugin, session);
PluginVersionResult versionResult = versionResolver.resolve(versionRequest);
plugin.setVersion(versionResult.getVersion());
}
System.out.println("Resolved plugin: " + plugin.getGroupId() + ":" +
plugin.getArtifactId() + ":" + plugin.getVersion() +
" from prefix: " + prefix);
return plugin;
}
// Example usage
public void executePluginByPrefix(String prefix, String goal, MavenSession session) throws Exception {
// Resolve plugin from prefix
Plugin plugin = resolvePluginFromPrefix(prefix, session);
// Load plugin descriptor
PluginDescriptor descriptor = buildPluginManager.loadPlugin(
plugin, session.getCurrentProject().getPluginArtifactRepositories(),
session.getRepositorySession());
// Create and execute mojo
MojoExecution execution = new MojoExecution(descriptor.getMojo(goal));
buildPluginManager.executeMojo(session, execution);
}public interface PluginDescriptor {
String getGroupId();
String getArtifactId();
String getVersion();
String getGoalPrefix();
List<MojoDescriptor> getMojos();
MojoDescriptor getMojo(String goal);
ClassRealm getClassRealm();
void setClassRealm(ClassRealm classRealm);
List<Artifact> getArtifacts();
void setArtifacts(List<Artifact> artifacts);
List<ComponentDescriptor<?>> getComponents();
}
public interface MojoDescriptor {
String getGoal();
String getPhase();
String getExecutePhase();
String getExecuteGoal();
String getExecuteLifecycle();
String getImplementation();
String getLanguage();
String getComponentType();
String getInstantiationStrategy();
boolean isAggregator();
boolean isDirectInvocationOnly();
boolean isProjectRequired();
boolean isOnlineRequired();
boolean isInheritedByDefault();
List<Parameter> getParameters();
Parameter getParameter(String name);
PluginDescriptor getPluginDescriptor();
}
public interface Parameter {
String getName();
String getType();
boolean isRequired();
boolean isEditable();
String getDescription();
String getExpression();
String getDefaultValue();
}
public interface PluginArtifactsResult {
List<Artifact> getArtifacts();
Map<String, Artifact> getArtifactMap();
}
public interface ClassRealm extends ClassLoader {
String getId();
ClassWorld getWorld();
void importFrom(String realmId, String packageName);
void importFromParent(String packageName);
ClassRealm getParentRealm();
Collection<ClassRealm> getImportRealms();
void addURL(URL url);
URL[] getURLs();
void display();
}
public interface CacheCleanupCallback {
void cleanup(CacheRecord record);
}
public interface PluginPrefixResult {
String getGroupId();
String getArtifactId();
}
public interface PluginVersionResult {
String getVersion();
Repository getRepository();
}Install with Tessl CLI
npx tessl i tessl/maven-org-apache-maven--maven-core