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