protected void addPlugin()

in maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java [299:405]


    protected void addPlugin( Plugin plugin,
                              Artifact pluginArtifact,
                              MavenProject project,
                              ArtifactRepository localRepository )
        throws PluginManagerException, InvalidPluginException
    {
        PlexusContainer child;

        try
        {
            MavenPluginValidator validator = new MavenPluginValidator( pluginArtifact );

            String key = PluginUtils.constructVersionedKey( plugin ).intern();
            child = container.createChildContainer( key,
                                                    Collections.singletonList( pluginArtifact.getFile() ),
                                                    Collections.EMPTY_MAP,
                                                    Arrays.asList( new ComponentDiscoveryListener[] { validator, pluginCollector } ) );

            // remove listeners for efficiency since they are only needed for the initial stage and
            // should not be applied to the plugin's dependencies
            child.removeComponentDiscoveryListener( validator );
            child.removeComponentDiscoveryListener( pluginCollector );

            if ( validator.hasErrors() )
            {
                String msg = "Plugin '" + key + "' has an invalid descriptor:";
                int count = 1;
                for ( Iterator i = validator.getErrors().iterator(); i.hasNext(); )
                {
                    msg += "\n" + count + ") " + i.next();
                    count++;
                }
                throw new PluginManagerException( msg );
            }

            try
            {
                child.getContainerRealm().importFrom( "plexus.core", "org.codehaus.plexus.util.xml.Xpp3Dom" );
                child.getContainerRealm().importFrom( "plexus.core", "org.codehaus.plexus.util.xml.pull.XmlPullParser" );
                child.getContainerRealm().importFrom( "plexus.core", "org.codehaus.plexus.util.xml.pull.XmlPullParserException" );
                child.getContainerRealm().importFrom( "plexus.core", "org.codehaus.plexus.util.xml.pull.XmlSerializer" );

                // 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 plugin descriptor for the plugin " + plugin + " was not found."
                + " Please verify that the plugin JAR " + pluginArtifact.getFile() + " is intact." );
        }

        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 ) );
        addedPlugin.setPluginArtifact( 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;
            }

						for( Dependency dependency: projectPlugin.getDependencies() )
						{
			          if ( !Artifact.SCOPE_SYSTEM.equals( dependency.getScope() ) )
			          {
			              dependency.setScope( Artifact.SCOPE_RUNTIME );
			          }
						}

            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 );
        }
    }