public void addExtension()

in maven-core/src/main/java/org/apache/maven/extension/DefaultExtensionManager.java [106:221]


    public void addExtension( Extension extension,
                              MavenProject project,
                              ProjectBuilderConfiguration builderConfig )
        throws ArtifactResolutionException, PlexusContainerException, ArtifactNotFoundException
    {
        String extensionId = ArtifactUtils.versionlessKey( extension.getGroupId(), extension.getArtifactId() );

        getLogger().debug( "Initialising extension: " + extensionId );

        Artifact artifact = (Artifact) project.getExtensionArtifactMap().get( extensionId );

        if ( artifact != null )
        {
            ArtifactFilter filter = new ProjectArtifactExceptionFilter( artifactFilter, project.getArtifact() );
            
            ArtifactMetadataSource metadataSource = builderConfig.getMetadataSource();
            if ( metadataSource == null )
            {
                metadataSource = artifactMetadataSource;
            }

            ResolutionGroup resolutionGroup;
            try
            {
                resolutionGroup = metadataSource.retrieve( artifact, builderConfig.getLocalRepository(),
                                                                   project.getRemoteArtifactRepositories() );
            }
            catch ( ArtifactMetadataRetrievalException e )
            {
                throw new ArtifactResolutionException( "Unable to download metadata from repository for plugin '"
                    + artifact.getId() + "': " + e.getMessage(), artifact, e );
            }

            // We use the same hack here to make sure that plexus 1.1 is available for extensions that do
            // not declare plexus-utils but need it. MNG-2900
            Set<Artifact> rgArtifacts = resolutionGroup.getArtifacts();
            rgArtifacts = DefaultPluginManager.checkPlexusUtils( rgArtifacts, artifactFactory );

            Set<Artifact> dependencies = new LinkedHashSet<Artifact>();
            dependencies.add( artifact );
            dependencies.addAll( rgArtifacts );

            // Make sure that we do not influence the dependenecy resolution of extensions with the project's
            // dependencyManagement

            ArtifactResolutionResult result = artifactResolver.resolveTransitively( dependencies, project.getArtifact(),
                                                                                    Collections.EMPTY_MAP,
                                                                                    //project.getManagedVersionMap(),
                                                                                    builderConfig.getLocalRepository(),
                                                                                    project.getRemoteArtifactRepositories(),
                                                                                    metadataSource, filter );

            // gross hack for some backwards compat (MNG-2749)
            // if it is a lone artifact, then we assume it to be a resource package, and put it in the main container
            // as before. If it has dependencies, that's when we risk conflict and exile to the child container
            // jvz: we have to make this 2 because plexus is always added now.

            Set<Artifact> artifacts = result.getArtifacts();

            // Lifecycles are loaded by the Lifecycle executor by looking up lifecycle definitions from the
            // core container. So we need to look if an extension has a lifecycle mapping and use the container
            // and not an extension container. (MNG-2831)

            if ( extensionContainsLifeycle( artifact.getFile() ) )
            {
                for ( Artifact a : artifacts )
                {
                    if ( artifactFilter.include( a ) )
                    {
                        getLogger().debug( "Adding extension to core container: " + a.getFile() );

                        container.addJarResource( a.getFile() );
                    }
                }
            }
            else if ( artifacts.size() == 2 )
            {
                for ( Artifact a : artifacts )
                {
                    if ( !a.getArtifactId().equals( "plexus-utils" ) )
                    {
                        a = project.replaceWithActiveArtifact( a );

                        getLogger().debug( "Adding extension to core container: " + a.getFile() );

                        container.addJarResource( a.getFile() );
                    }
                }
            }
            else
            {
                // create a child container for the extension
                // TODO: this could surely be simpler/different on trunk with the new classworlds

                if ( extensionContainer == null )
                {
                    extensionContainer = createContainer();
                }

                for ( Artifact a : (Set<Artifact>) result.getArtifacts() )
                {
                    a = project.replaceWithActiveArtifact( a );

                    getLogger().debug( "Adding to extension classpath: " + a.getFile() );

                    extensionContainer.addJarResource( a.getFile() );
                }

                if ( getLogger().isDebugEnabled() )
                {
                    getLogger().debug( "Extension container contents:" );
                    extensionContainer.getContainerRealm().display();
                }
            }
        }
    }