8
8
import org .eclipse .aether .artifact .DefaultArtifact ;
9
9
import org .eclipse .aether .collection .CollectRequest ;
10
10
import org .eclipse .aether .graph .Dependency ;
11
+ import org .eclipse .aether .repository .ArtifactRepository ;
11
12
import org .eclipse .aether .repository .LocalRepository ;
12
13
import org .eclipse .aether .repository .RemoteRepository ;
13
14
import org .eclipse .aether .resolution .ArtifactResult ;
18
19
import org .eclipse .aether .util .artifact .JavaScopes ;
19
20
import org .eclipse .aether .util .filter .ScopeDependencyFilter ;
20
21
import org .jetbrains .annotations .NotNull ;
22
+ import org .jetbrains .annotations .Nullable ;
21
23
22
24
import java .nio .file .Path ;
25
+ import java .util .AbstractMap .SimpleEntry ;
23
26
import java .util .Arrays ;
24
27
import java .util .Collection ;
25
28
import java .util .Collections ;
26
29
import java .util .List ;
30
+ import java .util .Map .Entry ;
27
31
import java .util .Properties ;
32
+ import java .util .concurrent .atomic .AtomicInteger ;
28
33
import java .util .stream .Collectors ;
29
34
import java .util .stream .Stream ;
30
35
35
40
*/
36
41
class TransitiveDependencyCollector {
37
42
43
+ /**
44
+ * Counter used to generate ids for repositories
45
+ *
46
+ * @see #newDefaultRepository(String)
47
+ */
48
+ private static final AtomicInteger ID_GENERATOR = new AtomicInteger ();
49
+
38
50
/**
39
51
* Maven repository system
40
52
*
@@ -72,7 +84,7 @@ public TransitiveDependencyCollector(@NotNull Path saveDirectory) {
72
84
*/
73
85
@ NotNull
74
86
public static RemoteRepository newDefaultRepository (@ NotNull String url ) {
75
- return new RemoteRepository .Builder (url , "default" , url ).build ();
87
+ return new RemoteRepository .Builder ("repo" + ID_GENERATOR . getAndIncrement () , "default" , url ).build ();
76
88
}
77
89
78
90
/**
@@ -83,19 +95,30 @@ public static RemoteRepository newDefaultRepository(@NotNull String url) {
83
95
* @param version Maven dependency version
84
96
* @param classifier Maven artifact classifier. May be null
85
97
* @param repositories Maven repositories that would be used for dependency resolution
86
- * @return Transitive dependencies, exception otherwise
98
+ * @return Transitive dependencies paired with their repository url , exception otherwise
87
99
* @throws DependencyResolutionException thrown if dependency doesn't exist on provided repositories
88
100
*/
89
101
@ NotNull
90
- public Collection <Artifact > findTransitiveDependencies (@ NotNull String groupId , @ NotNull String artifactId , @ NotNull String version , @ NotNull String classifier , @ NotNull List <RemoteRepository > repositories ) throws DependencyResolutionException {
102
+ public Collection <Entry < Artifact , @ Nullable String > > findTransitiveDependencies (@ NotNull String groupId , @ NotNull String artifactId , @ NotNull String version , @ NotNull String classifier , @ NotNull List <RemoteRepository > repositories ) throws DependencyResolutionException {
91
103
Artifact artifact = new DefaultArtifact (groupId , artifactId , classifier , "jar" , version );
92
104
93
105
CollectRequest collectRequest = new CollectRequest (new Dependency (artifact , JavaScopes .COMPILE ), repositories );
94
106
DependencyRequest dependencyRequest = new DependencyRequest (collectRequest , new ScopeDependencyFilter (Arrays .asList (JavaScopes .COMPILE , JavaScopes .RUNTIME ), Collections .emptyList ()));
95
107
96
108
DependencyResult dependencyResult = repositorySystem .resolveDependencies (repositorySystemSession , dependencyRequest );
97
109
98
- return dependencyResult .getArtifactResults ().stream ().filter (ArtifactResult ::isResolved ).map (ArtifactResult ::getArtifact ).collect (Collectors .toList ());
110
+ return dependencyResult .getArtifactResults ()
111
+ .stream ()
112
+ .filter (ArtifactResult ::isResolved )
113
+ .map (artifactResult -> {
114
+ ArtifactRepository repo = artifactResult .getRepository ();
115
+ String url = null ;
116
+ if (repo instanceof RemoteRepository ) {
117
+ url = ((RemoteRepository ) repo ).getUrl ();
118
+ }
119
+ return new SimpleEntry <>(artifactResult .getArtifact (), url );
120
+ })
121
+ .collect (Collectors .toList ());
99
122
}
100
123
101
124
/**
@@ -106,12 +129,12 @@ public Collection<Artifact> findTransitiveDependencies(@NotNull String groupId,
106
129
* @param version Maven artifact version
107
130
* @param classifier Maven artifact classifier. May be null
108
131
* @param repositories Maven repositories for transitive dependencies search
109
- * @return Transitive dependencies, exception otherwise
132
+ * @return Transitive dependencies paired with their repository url , exception otherwise
110
133
* @throws DependencyResolutionException thrown if dependency doesn't exist on provided repositories
111
134
* @see #findTransitiveDependencies(String, String, String, String, List)
112
135
*/
113
136
@ NotNull
114
- public Collection <Artifact > findTransitiveDependencies (@ NotNull String groupId , @ NotNull String artifactId , @ NotNull String version , @ NotNull String classifier , @ NotNull Stream <String > repositories ) throws DependencyResolutionException {
137
+ public Collection <Entry < Artifact , @ Nullable String > > findTransitiveDependencies (@ NotNull String groupId , @ NotNull String artifactId , @ NotNull String version , @ NotNull String classifier , @ NotNull Stream <String > repositories ) throws DependencyResolutionException {
115
138
return findTransitiveDependencies (groupId , artifactId , version , classifier , repositories .map (TransitiveDependencyCollector ::newDefaultRepository ).collect (Collectors .toList ()));
116
139
}
117
140
0 commit comments