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 org.apache.maven.RepositoryUtils;
23 import org.apache.maven.artifact.Artifact;
24 import org.apache.maven.project.DefaultProjectBuildingRequest;
25 import org.apache.maven.project.DependencyResolutionResult;
26 import org.apache.maven.project.ProjectBuilder;
27 import org.apache.maven.project.ProjectBuildingException;
28 import org.apache.maven.project.ProjectBuildingRequest;
29 import org.apache.maven.project.ProjectBuildingResult;
30
31 import java.lang.reflect.InvocationTargetException;
32 import java.util.HashSet;
33 import java.util.List;
34 import java.util.Set;
35
36
37
38
39
40
41
42 public class ArtifactTransitivityFilter
43 extends AbstractArtifactsFilter
44 {
45
46
47
48 private Set<String> transitiveArtifacts;
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 public ArtifactTransitivityFilter( Artifact artifact, ProjectBuildingRequest buildingRequest,
71 ProjectBuilder projectBuilder )
72 throws ProjectBuildingException
73 {
74 ProjectBuildingRequest request = new DefaultProjectBuildingRequest( buildingRequest );
75
76 request.setResolveDependencies( true );
77
78 ProjectBuildingResult buildingResult = projectBuilder.build( artifact, request );
79
80 DependencyResolutionResult resolutionResult = buildingResult.getDependencyResolutionResult();
81 if ( resolutionResult != null )
82 {
83 if ( isMaven31() )
84 {
85 try
86 {
87 @SuppressWarnings( "unchecked" ) List<org.eclipse.aether.graph.Dependency> dependencies =
88 (List<org.eclipse.aether.graph.Dependency>) Invoker.invoke( resolutionResult,
89 "getDependencies" );
90
91 for ( org.eclipse.aether.graph.Dependency dependency : dependencies )
92 {
93 Artifact mavenArtifact =
94 (Artifact) Invoker.invoke( RepositoryUtils.class, "toArtifact",
95 org.eclipse.aether.artifact.Artifact.class,
96 dependency.getArtifact() );
97
98 transitiveArtifacts.add( mavenArtifact.getDependencyConflictId() );
99 }
100 }
101 catch ( IllegalAccessException e )
102 {
103
104 throw new RuntimeException( e.getMessage(), e );
105 }
106 catch ( InvocationTargetException e )
107 {
108
109 throw new RuntimeException( e.getMessage(), e );
110 }
111 catch ( NoSuchMethodException e )
112 {
113
114 throw new RuntimeException( e.getMessage(), e );
115 }
116 }
117 else
118 {
119 try
120 {
121 @SuppressWarnings( "unchecked" ) List<org.sonatype.aether.graph.Dependency> dependencies =
122 (List<org.sonatype.aether.graph.Dependency>) Invoker.invoke( resolutionResult,
123 "getDependencies" );
124
125 for ( org.sonatype.aether.graph.Dependency dependency : dependencies )
126 {
127 Artifact mavenArtifact =
128 (Artifact) Invoker.invoke( RepositoryUtils.class, "toArtifact",
129 org.sonatype.aether.artifact.Artifact.class,
130 dependency.getArtifact() );
131
132 transitiveArtifacts.add( mavenArtifact.getDependencyConflictId() );
133 }
134 }
135 catch ( IllegalAccessException e )
136 {
137
138 throw new RuntimeException( e.getMessage(), e );
139 }
140 catch ( InvocationTargetException e )
141 {
142
143 throw new RuntimeException( e.getMessage(), e );
144 }
145 catch ( NoSuchMethodException e )
146 {
147
148 throw new RuntimeException( e.getMessage(), e );
149 }
150 }
151 }
152 }
153
154
155
156
157 protected static boolean isMaven31()
158 {
159 return canFindCoreClass( "org.eclipse.aether.artifact.Artifact" );
160 }
161
162 private static boolean canFindCoreClass( String className )
163 {
164 try
165 {
166 Thread.currentThread().getContextClassLoader().loadClass( className );
167
168 return true;
169 }
170 catch ( ClassNotFoundException e )
171 {
172 return false;
173 }
174 }
175
176
177
178
179 public Set<Artifact> filter( Set<Artifact> artifacts )
180 {
181
182 Set<Artifact> result = new HashSet<Artifact>();
183 for ( Artifact artifact : artifacts )
184 {
185 if ( artifactIsATransitiveDependency( artifact ) )
186 {
187 result.add( artifact );
188 }
189 }
190 return result;
191 }
192
193
194
195
196
197
198
199 public boolean artifactIsATransitiveDependency( Artifact artifact )
200 {
201 return transitiveArtifacts.contains( artifact.getDependencyConflictId() );
202 }
203 }