001package org.apache.maven;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.HashSet;
025import java.util.LinkedHashSet;
026import java.util.List;
027import java.util.Set;
028
029import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
030import org.apache.maven.artifact.resolver.filter.ExclusionSetFilter;
031import org.codehaus.plexus.PlexusContainer;
032import org.codehaus.plexus.component.annotations.Component;
033import org.codehaus.plexus.component.annotations.Requirement;
034import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
035
036/**
037 * @author Jason van Zyl
038 * @todo this should probably be a component with some dynamic control of filtering
039 */
040@Component( role = ArtifactFilterManager.class )
041public class DefaultArtifactFilterManager 
042    implements ArtifactFilterManager
043{
044
045    private static final Set<String> DEFAULT_EXCLUSIONS;
046
047    @Requirement
048    private PlexusContainer plexus;
049
050    static
051    {
052        Set<String> artifacts = new HashSet<String>();
053
054        artifacts.add( "classworlds:classworlds" );
055        artifacts.add( "org.codehaus.plexus:plexus-classworlds" );
056        artifacts.add( "org.codehaus.plexus:plexus-component-api" );
057        artifacts.add( "org.codehaus.plexus:plexus-container-default" );
058        artifacts.add( "plexus:plexus-container-default" );
059        artifacts.add( "org.sonatype.spice:spice-inject-plexus" );
060        artifacts.add( "org.sonatype.sisu:sisu-inject-plexus" );
061        artifacts.add( "org.eclipse.sisu:org.eclipse.sisu.plexus" );
062        artifacts.add( "org.apache.maven:maven-artifact" );
063        artifacts.add( "org.apache.maven:maven-aether-provider" );
064        artifacts.add( "org.apache.maven:maven-artifact-manager" );
065        artifacts.add( "org.apache.maven:maven-compat" );
066        artifacts.add( "org.apache.maven:maven-core" );
067        artifacts.add( "org.apache.maven:maven-error-diagnostics" );
068        artifacts.add( "org.apache.maven:maven-lifecycle" );
069        artifacts.add( "org.apache.maven:maven-model" );
070        artifacts.add( "org.apache.maven:maven-model-builder" );
071        artifacts.add( "org.apache.maven:maven-monitor" );
072        artifacts.add( "org.apache.maven:maven-plugin-api" );
073        artifacts.add( "org.apache.maven:maven-plugin-descriptor" );
074        artifacts.add( "org.apache.maven:maven-plugin-parameter-documenter" );
075        artifacts.add( "org.apache.maven:maven-plugin-registry" );
076        artifacts.add( "org.apache.maven:maven-profile" );
077        artifacts.add( "org.apache.maven:maven-project" );
078        artifacts.add( "org.apache.maven:maven-repository-metadata" );
079        artifacts.add( "org.apache.maven:maven-settings" );
080        artifacts.add( "org.apache.maven:maven-settings-builder" );
081        artifacts.add( "org.apache.maven:maven-toolchain" );
082        artifacts.add( "org.apache.maven.wagon:wagon-provider-api" );
083        artifacts.add( "org.eclipse.aether:aether-api" );
084        artifacts.add( "org.eclipse.aether:aether-spi" );
085        artifacts.add( "org.eclipse.aether:aether-impl" );
086        //
087        // We must also filter out the old or NoClassDefFoundErrors will surface
088        //
089        artifacts.add( "org.sonatype.aether:aether-api" );
090        artifacts.add( "org.sonatype.aether:aether-spi" );
091        artifacts.add( "org.sonatype.aether:aether-impl" );
092
093        /*
094         * NOTE: Don't exclude the wagons or any of their dependencies (apart from the wagon API). This would otherwise
095         * provoke linkage errors for wagons contributed by build extensions. We also don't need to exclude the wagons
096         * from plugins. Plugins that use wagons directly and declare the corresponding dependency will simply use a
097         * wagon from their plugin realm.
098         */
099
100        DEFAULT_EXCLUSIONS = Collections.unmodifiableSet( artifacts );
101    }
102
103    protected Set<String> excludedArtifacts = new HashSet<String>( DEFAULT_EXCLUSIONS );
104
105    /**
106     * @deprecated Use this class as a component instead, and then use getArtifactFilter().
107     */
108    public static ArtifactFilter createStandardFilter()
109    {
110        // TODO: configure this from bootstrap or scan lib
111        return new ExclusionSetFilter( DEFAULT_EXCLUSIONS );
112    }
113
114    /**
115     * Returns the artifact filter for the core + extension artifacts.
116     *
117     * @see org.apache.maven.ArtifactFilterManager#getArtifactFilter()
118     */
119    public ArtifactFilter getArtifactFilter()
120    {
121        Set<String> excludes = new LinkedHashSet<String>( excludedArtifacts );
122
123        for ( ArtifactFilterManagerDelegate delegate : getDelegates() )
124        {
125            delegate.addExcludes( excludes );
126        }
127
128        return new ExclusionSetFilter( excludes );
129    }
130
131    /**
132     * Returns the artifact filter for the standard core artifacts.
133     *
134     * @see org.apache.maven.ArtifactFilterManager#getExtensionDependencyFilter()
135     */
136    public ArtifactFilter getCoreArtifactFilter()
137    {
138        return new ExclusionSetFilter( getCoreArtifactExcludes() );
139    }
140
141    private List<ArtifactFilterManagerDelegate> getDelegates()
142    {
143        try
144        {
145            return plexus.lookupList( ArtifactFilterManagerDelegate.class );
146        }
147        catch ( ComponentLookupException e )
148        {
149            return new ArrayList<ArtifactFilterManagerDelegate>();
150        }
151    }
152
153    /* (non-Javadoc)
154     * @see org.apache.maven.ArtifactFilterManager#excludeArtifact(java.lang.String)
155     */
156    public void excludeArtifact( String artifactId )
157    {
158        excludedArtifacts.add( artifactId );
159    }
160
161    public Set<String> getCoreArtifactExcludes()
162    {
163        Set<String> excludes = new LinkedHashSet<String>( DEFAULT_EXCLUSIONS );
164
165        for ( ArtifactFilterManagerDelegate delegate : getDelegates() )
166        {
167            delegate.addCoreExcludes( excludes );
168        }
169
170        return excludes;
171    }
172
173}