| Method from org.apache.maven.plugin.DefaultPluginManager Detail: |
protected void addPlugin(Plugin plugin,
Artifact pluginArtifact,
MavenProject project,
ArtifactRepository localRepository) throws InvalidPluginException, PluginManagerException {
PlexusContainer child;
try
{
child = container.createChildContainer( plugin.getKey(),
Collections.singletonList( pluginArtifact.getFile() ),
Collections.EMPTY_MAP,
Collections.singletonList( pluginCollector ) );
try
{
child.getContainerRealm().importFrom( "plexus.core", "org.codehaus.plexus.util.xml.Xpp3Dom" );
child.getContainerRealm().importFrom( "plexus.core", "org.codehaus.plexus.util.xml.pull" );
// MNG-2878
child.getContainerRealm().importFrom( "plexus.core", "/default-report.xml" );
}
catch ( NoSuchRealmException e )
{
// won't happen
}
}
catch ( PlexusContainerException e )
{
throw new PluginManagerException(
"Failed to create plugin container for plugin '" + plugin + "': " + e.getMessage(), e );
}
// this plugin's descriptor should have been discovered in the child creation, so we should be able to
// circle around and set the artifacts and class realm
PluginDescriptor addedPlugin = pluginCollector.getPluginDescriptor( plugin );
if ( addedPlugin == null )
{
throw new IllegalStateException( "The PluginDescriptor for the plugin " + plugin + " was not found." );
}
addedPlugin.setClassRealm( child.getContainerRealm() );
// we're only setting the plugin's artifact itself as the artifact list, to allow it to be retrieved
// later when the plugin is first invoked. Retrieving this artifact will in turn allow us to
// transitively resolve its dependencies, and add them to the plugin container...
addedPlugin.setArtifacts( Collections.singletonList( pluginArtifact ) );
try
{
// the only Plugin instance which will have dependencies is the one specified in the project.
// We need to look for a Plugin instance there, in case the instance we're using didn't come from
// the project.
Plugin projectPlugin = (Plugin) project.getBuild().getPluginsAsMap().get( plugin.getKey() );
if ( projectPlugin == null )
{
projectPlugin = plugin;
}
Set artifacts = MavenMetadataSource.createArtifacts( artifactFactory, projectPlugin.getDependencies(), null,
null, project );
// Set artifacts =
// MavenMetadataSource.createArtifacts( artifactFactory, plugin.getDependencies(), null, null, project );
addedPlugin.setIntroducedDependencyArtifacts( artifacts );
}
catch ( InvalidDependencyVersionException e )
{
throw new InvalidPluginException( "Plugin '" + plugin + "' is invalid: " + e.getMessage(), e );
}
}
|
public static void checkPlexusUtils(ResolutionGroup resolutionGroup,
ArtifactFactory artifactFactory) {
// ----------------------------------------------------------------------------
// If the plugin already declares a dependency on plexus-utils then we're all
// set as the plugin author is aware of its use. If we don't have a dependency
// on plexus-utils then we must protect users from stupid plugin authors who
// did not declare a direct dependency on plexus-utils because the version
// Maven uses is hidden from downstream use. We will also bump up any
// anything below 1.1 to 1.1 as this mimics the behaviour in 2.0.5 where
// plexus-utils 1.1 was being forced into use.
// ----------------------------------------------------------------------------
VersionRange vr = null;
try
{
vr = VersionRange.createFromVersionSpec( "[1.1,)" );
}
catch ( InvalidVersionSpecificationException e )
{
// Won't happen
}
boolean plexusUtilsPresent = false;
for ( Iterator i = resolutionGroup.getArtifacts().iterator(); i.hasNext(); )
{
Artifact a = (Artifact) i.next();
if ( a.getArtifactId().equals( "plexus-utils" ) &&
vr.containsVersion( new DefaultArtifactVersion( a.getVersion() ) ) )
{
plexusUtilsPresent = true;
break;
}
}
if ( !plexusUtilsPresent )
{
// We will add plexus-utils as every plugin was getting this anyway from Maven itself. We will set the
// version to the latest version we know that works as of the 2.0.6 release. We set the scope to runtime
// as this is what's implicitly happening in 2.0.6.
resolutionGroup.getArtifacts().add( artifactFactory.createArtifact( "org.codehaus.plexus",
"plexus-utils", "1.1",
Artifact.SCOPE_RUNTIME, "jar" ) );
}
}
|
public void contextualize(Context context) throws ContextException {
container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
mojoLogger = new DefaultLog( container.getLoggerManager().getLoggerForComponent( Mojo.ROLE ) );
}
|
public static PlexusConfiguration copyConfiguration(PlexusConfiguration src) {
// TODO: shouldn't be necessary
XmlPlexusConfiguration dom = new XmlPlexusConfiguration( src.getName() );
dom.setValue( src.getValue( null ) );
String[] attributeNames = src.getAttributeNames();
for ( int i = 0; i < attributeNames.length; i++ )
{
String attributeName = attributeNames[i];
dom.setAttribute( attributeName, src.getAttribute( attributeName, null ) );
}
PlexusConfiguration[] children = src.getChildren();
for ( int i = 0; i < children.length; i++ )
{
dom.addChild( copyConfiguration( children[i] ) );
}
return dom;
}
|
public static String createPluginParameterRequiredMessage(MojoDescriptor mojo,
Parameter parameter,
String expression) {
StringBuffer message = new StringBuffer();
message.append( "The '" );
message.append( parameter.getName() );
message.append( "' parameter is required for the execution of the " );
message.append( mojo.getFullGoalName() );
message.append( " mojo and cannot be null." );
if ( expression != null )
{
message.append( " The retrieval expression was: " ).append( expression );
}
return message.toString();
}
|
public void executeMojo(MavenProject project,
MojoExecution mojoExecution,
MavenSession session) throws ArtifactResolutionException, PluginManagerException, InvalidDependencyVersionException, MojoExecutionException, ArtifactNotFoundException, MojoFailureException, PluginConfigurationException {
MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
// NOTE: I'm putting these checks in here, since this is the central point of access for
// anything that wants to execute a mojo.
if ( mojoDescriptor.isProjectRequired() && !session.isUsingPOMsFromFilesystem() )
{
throw new MojoExecutionException( "Cannot execute mojo: " + mojoDescriptor.getGoal() +
". It requires a project with an existing pom.xml, but the build is not using one." );
}
if ( mojoDescriptor.isOnlineRequired() && session.getSettings().isOffline() )
{
// TODO: Should we error out, or simply warn and skip??
throw new MojoExecutionException( "Mojo: " + mojoDescriptor.getGoal() +
" requires online mode for execution. Maven is currently offline." );
}
if ( mojoDescriptor.isDependencyResolutionRequired() != null )
{
Collection projects;
if ( mojoDescriptor.isAggregator() )
{
projects = session.getSortedProjects();
}
else
{
projects = Collections.singleton( project );
}
for ( Iterator i = projects.iterator(); i.hasNext(); )
{
MavenProject p = (MavenProject) i.next();
resolveTransitiveDependencies( session, artifactResolver,
mojoDescriptor.isDependencyResolutionRequired(), artifactFactory, p, mojoDescriptor.isAggregator() );
}
downloadDependencies( project, session, artifactResolver );
}
String goalName = mojoDescriptor.getFullGoalName();
Mojo plugin;
PluginDescriptor pluginDescriptor = mojoDescriptor.getPluginDescriptor();
String goalId = mojoDescriptor.getGoal();
String groupId = pluginDescriptor.getGroupId();
String artifactId = pluginDescriptor.getArtifactId();
String executionId = mojoExecution.getExecutionId();
Xpp3Dom dom = project.getGoalConfiguration( groupId, artifactId, executionId, goalId );
Xpp3Dom reportDom = project.getReportConfiguration( groupId, artifactId, executionId );
dom = Xpp3Dom.mergeXpp3Dom( dom, reportDom );
if ( mojoExecution.getConfiguration() != null )
{
dom = Xpp3Dom.mergeXpp3Dom( dom, mojoExecution.getConfiguration() );
}
plugin = getConfiguredMojo( session, dom, project, false, mojoExecution );
// Event monitoring.
String event = MavenEvents.MOJO_EXECUTION;
EventDispatcher dispatcher = session.getEventDispatcher();
String goalExecId = goalName;
if ( mojoExecution.getExecutionId() != null )
{
goalExecId += " {execution: " + mojoExecution.getExecutionId() + "}";
}
dispatcher.dispatchStart( event, goalExecId );
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
try
{
Thread.currentThread().setContextClassLoader(
mojoDescriptor.getPluginDescriptor().getClassRealm().getClassLoader() );
plugin.execute();
dispatcher.dispatchEnd( event, goalExecId );
}
catch ( MojoExecutionException e )
{
session.getEventDispatcher().dispatchError( event, goalExecId, e );
throw e;
}
catch ( MojoFailureException e )
{
session.getEventDispatcher().dispatchError( event, goalExecId, e );
throw e;
}
catch ( LinkageError e )
{
if ( getLogger().isFatalErrorEnabled() )
{
getLogger().fatalError(
plugin.getClass().getName() + "#execute() caused a linkage error ("
+ e.getClass().getName() + ") and may be out-of-date. Check the realms:" );
ClassRealm pluginRealm = mojoDescriptor.getPluginDescriptor().getClassRealm();
StringBuffer sb = new StringBuffer();
sb.append( "Plugin realm = " + pluginRealm.getId() ).append( '\n" );
for ( int i = 0; i < pluginRealm.getConstituents().length; i++ )
{
sb.append( "urls[" + i + "] = " + pluginRealm.getConstituents()[i] );
if ( i != ( pluginRealm.getConstituents().length - 1 ) )
{
sb.append( '\n" );
}
}
getLogger().fatalError( sb.toString() );
ClassRealm containerRealm = container.getContainerRealm();
sb = new StringBuffer();
sb.append( "Container realm = " + containerRealm.getId() ).append( '\n" );
for ( int i = 0; i < containerRealm.getConstituents().length; i++ )
{
sb.append( "urls[" + i + "] = " + containerRealm.getConstituents()[i] );
if ( i != ( containerRealm.getConstituents().length - 1 ) )
{
sb.append( '\n" );
}
}
getLogger().fatalError( sb.toString() );
}
session.getEventDispatcher().dispatchError( event, goalExecId, e );
throw e;
}
finally
{
Thread.currentThread().setContextClassLoader( oldClassLoader );
try
{
PlexusContainer pluginContainer = getPluginContainer( mojoDescriptor.getPluginDescriptor() );
pluginContainer.release( plugin );
}
catch ( ComponentLifecycleException e )
{
if ( getLogger().isErrorEnabled() )
{
getLogger().error( "Error releasing plugin - ignoring.", e );
}
}
}
}
|
public Object getPluginComponent(Plugin plugin,
String role,
String roleHint) throws PluginManagerException, ComponentLookupException {
PluginDescriptor pluginDescriptor = pluginCollector.getPluginDescriptor( plugin );
PlexusContainer pluginContainer = getPluginContainer( pluginDescriptor );
return pluginContainer.lookup( role, roleHint );
}
|
public Map getPluginComponents(Plugin plugin,
String role) throws PluginManagerException, ComponentLookupException {
PluginDescriptor pluginDescriptor = pluginCollector.getPluginDescriptor( plugin );
PlexusContainer pluginContainer = getPluginContainer( pluginDescriptor );
return pluginContainer.lookupMap( role );
}
|
public Plugin getPluginDefinitionForPrefix(String prefix,
MavenSession session,
MavenProject project) {
// TODO: since this is only used in the lifecycle executor, maybe it should be moved there? There is no other
// use for the mapping manager in here
return pluginMappingManager.getByPrefix( prefix, session.getSettings().getPluginGroups(),
project.getPluginArtifactRepositories(),
session.getLocalRepository() );
}
|
public PluginDescriptor getPluginDescriptorForPrefix(String prefix) {
return pluginCollector.getPluginDescriptorForPrefix( prefix );
}
|
public MavenReport getReport(MavenProject project,
MojoExecution mojoExecution,
MavenSession session) throws ArtifactResolutionException, PluginManagerException, ArtifactNotFoundException, PluginConfigurationException {
MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
PluginDescriptor descriptor = mojoDescriptor.getPluginDescriptor();
Xpp3Dom dom = project.getReportConfiguration( descriptor.getGroupId(), descriptor.getArtifactId(),
mojoExecution.getExecutionId() );
if ( mojoExecution.getConfiguration() != null )
{
dom = Xpp3Dom.mergeXpp3Dom( dom, mojoExecution.getConfiguration() );
}
return (MavenReport) getConfiguredMojo( session, dom, project, true, mojoExecution );
}
|
public void initialize() {
artifactFilter = MavenArtifactFilterManager.createStandardFilter();
}
|
public PluginDescriptor verifyPlugin(Plugin plugin,
MavenProject project,
Settings settings,
ArtifactRepository localRepository) throws InvalidVersionSpecificationException, PluginVersionResolutionException, ArtifactResolutionException, PluginManagerException, InvalidPluginException, PluginNotFoundException, ArtifactNotFoundException, PluginVersionNotFoundException {
// TODO: this should be possibly outside
// All version-resolution logic has been moved to DefaultPluginVersionManager.
if ( plugin.getVersion() == null )
{
String version = pluginVersionManager.resolvePluginVersion( plugin.getGroupId(), plugin.getArtifactId(),
project, settings, localRepository );
plugin.setVersion( version );
}
return verifyVersionedPlugin( plugin, project, localRepository );
}
|
public PluginDescriptor verifyReportPlugin(ReportPlugin reportPlugin,
MavenProject project,
MavenSession session) throws InvalidVersionSpecificationException, ArtifactResolutionException, PluginVersionResolutionException, PluginManagerException, InvalidPluginException, PluginNotFoundException, ArtifactNotFoundException, PluginVersionNotFoundException {
String version = reportPlugin.getVersion();
if ( version == null )
{
version = pluginVersionManager.resolveReportPluginVersion( reportPlugin.getGroupId(),
reportPlugin.getArtifactId(), project,
session.getSettings(),
session.getLocalRepository() );
reportPlugin.setVersion( version );
}
Plugin forLookup = new Plugin();
forLookup.setGroupId( reportPlugin.getGroupId() );
forLookup.setArtifactId( reportPlugin.getArtifactId() );
forLookup.setVersion( version );
return verifyVersionedPlugin( forLookup, project, session.getLocalRepository() );
}
|