View Javadoc

1   package org.apache.maven.shared.artifact.filter.collection;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
20   */
21  
22  import java.util.Collection;
23  import java.util.HashSet;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Set;
27  
28  import org.apache.maven.artifact.Artifact;
29  import org.apache.maven.artifact.factory.ArtifactFactory;
30  import org.apache.maven.artifact.repository.ArtifactRepository;
31  import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
32  import org.apache.maven.model.Dependency;
33  import org.apache.maven.project.MavenProject;
34  import org.apache.maven.project.MavenProjectBuilder;
35  import org.apache.maven.project.ProjectBuildingException;
36  import org.apache.maven.project.artifact.InvalidDependencyVersionException;
37  
38  /**
39   * This filter will exclude everything that is not a dependency of the selected artifact.
40   * 
41   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
42   * @version $Id: ArtifactTransitivityFilter.java 744326 2009-02-14 01:09:41Z brianf $
43   */
44  public class ArtifactTransitivityFilter
45      extends AbstractArtifactsFilter
46  {
47  
48      Collection transitiveArtifacts;
49  
50      ArtifactFactory factory;
51  
52      ArtifactRepository local;
53  
54      List remote;
55  
56      public ArtifactTransitivityFilter( Artifact artifact, ArtifactFactory factory, ArtifactRepository local,
57                                         List remote, MavenProjectBuilder builder )
58          throws ProjectBuildingException, InvalidDependencyVersionException
59      {
60          this.factory = factory;
61          this.local = local;
62          this.remote = remote;
63  
64          Artifact rootArtifactPom =
65              factory.createArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), "", "pom" );
66  
67          MavenProject rootArtifactProject = builder.buildFromRepository( rootArtifactPom, remote, local );
68  
69          // load all the artifacts.
70          transitiveArtifacts =
71              rootArtifactProject.createArtifacts( this.factory, Artifact.SCOPE_TEST,
72                                                   new ScopeArtifactFilter( Artifact.SCOPE_TEST ) );
73  
74      }
75  
76      public ArtifactTransitivityFilter( Dependency dependency, ArtifactFactory factory, ArtifactRepository local,
77                                         List remote, MavenProjectBuilder builder )
78          throws ProjectBuildingException, InvalidDependencyVersionException
79      {
80  
81          this.factory = factory;
82          this.local = local;
83          this.remote = remote;
84  
85          Artifact rootArtifactPom =
86              factory.createArtifact( dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(), "",
87                                      "pom" );
88  
89          MavenProject rootArtifactProject = builder.buildFromRepository( rootArtifactPom, remote, local );
90  
91          // load all the artifacts.
92          transitiveArtifacts =
93              rootArtifactProject.createArtifacts( this.factory, Artifact.SCOPE_TEST,
94                                                   new ScopeArtifactFilter( Artifact.SCOPE_TEST ) );
95  
96      }
97  
98      public Set filter( Set artifacts )
99      {
100 
101         Set result = new HashSet();
102         Iterator iterator = artifacts.iterator();
103         while ( iterator.hasNext() )
104         {
105             Artifact artifact = (Artifact) iterator.next();
106             if ( artifactIsATransitiveDependency( artifact ) )
107             {
108                 result.add( artifact );
109             }
110         }
111         return result;
112     }
113 
114     /**
115      * Compares the artifact to the list of dependencies to see if it is directly included by this project
116      * 
117      * @param artifact representing the item to compare.
118      * @return true if artifact is a transitive dependency
119      */
120     public boolean artifactIsATransitiveDependency( Artifact artifact )
121     {
122         boolean result = false;
123         Iterator iterator = transitiveArtifacts.iterator();
124         while ( iterator.hasNext() )
125         {
126             Artifact trans = (Artifact) iterator.next();
127             if ( trans.getGroupId().equals( artifact.getGroupId() ) &&
128                 trans.getArtifactId().equals( artifact.getArtifactId() ) )
129             {
130                 result = true;
131                 break;
132             }
133         }
134         return result;
135     }
136 }