1 | |
package org.apache.maven.shared.artifact.filter.collection; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
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 | |
|
40 | |
|
41 | |
|
42 | |
|
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 | 0 | { |
60 | 0 | this.factory = factory; |
61 | 0 | this.local = local; |
62 | 0 | this.remote = remote; |
63 | |
|
64 | 0 | Artifact rootArtifactPom = |
65 | |
factory.createArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), "", "pom" ); |
66 | |
|
67 | 0 | MavenProject rootArtifactProject = builder.buildFromRepository( rootArtifactPom, remote, local ); |
68 | |
|
69 | |
|
70 | 0 | transitiveArtifacts = |
71 | |
rootArtifactProject.createArtifacts( this.factory, Artifact.SCOPE_TEST, |
72 | |
new ScopeArtifactFilter( Artifact.SCOPE_TEST ) ); |
73 | |
|
74 | 0 | } |
75 | |
|
76 | |
public ArtifactTransitivityFilter( Dependency dependency, ArtifactFactory factory, ArtifactRepository local, |
77 | |
List remote, MavenProjectBuilder builder ) |
78 | |
throws ProjectBuildingException, InvalidDependencyVersionException |
79 | 0 | { |
80 | |
|
81 | 0 | this.factory = factory; |
82 | 0 | this.local = local; |
83 | 0 | this.remote = remote; |
84 | |
|
85 | 0 | Artifact rootArtifactPom = |
86 | |
factory.createArtifact( dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(), "", |
87 | |
"pom" ); |
88 | |
|
89 | 0 | MavenProject rootArtifactProject = builder.buildFromRepository( rootArtifactPom, remote, local ); |
90 | |
|
91 | |
|
92 | 0 | transitiveArtifacts = |
93 | |
rootArtifactProject.createArtifacts( this.factory, Artifact.SCOPE_TEST, |
94 | |
new ScopeArtifactFilter( Artifact.SCOPE_TEST ) ); |
95 | |
|
96 | 0 | } |
97 | |
|
98 | |
public Set filter( Set artifacts ) |
99 | |
{ |
100 | |
|
101 | 0 | Set result = new HashSet(); |
102 | 0 | Iterator iterator = artifacts.iterator(); |
103 | 0 | while ( iterator.hasNext() ) |
104 | |
{ |
105 | 0 | Artifact artifact = (Artifact) iterator.next(); |
106 | 0 | if ( artifactIsATransitiveDependency( artifact ) ) |
107 | |
{ |
108 | 0 | result.add( artifact ); |
109 | |
} |
110 | |
} |
111 | 0 | return result; |
112 | |
} |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
public boolean artifactIsATransitiveDependency( Artifact artifact ) |
121 | |
{ |
122 | 0 | boolean result = false; |
123 | 0 | Iterator iterator = transitiveArtifacts.iterator(); |
124 | 0 | while ( iterator.hasNext() ) |
125 | |
{ |
126 | 0 | Artifact trans = (Artifact) iterator.next(); |
127 | 0 | if ( trans.getGroupId().equals( artifact.getGroupId() ) && |
128 | |
trans.getArtifactId().equals( artifact.getArtifactId() ) ) |
129 | |
{ |
130 | 0 | result = true; |
131 | 0 | break; |
132 | |
} |
133 | |
} |
134 | 0 | return result; |
135 | |
} |
136 | |
} |