2
2
3
3
import com .alessiodp .libby .Library ;
4
4
import com .alessiodp .libby .LibraryManager ;
5
+ import com .alessiodp .libby .Util ;
5
6
import com .alessiodp .libby .classloader .IsolatedClassLoader ;
6
7
import org .jetbrains .annotations .NotNull ;
8
+ import org .jetbrains .annotations .Nullable ;
7
9
8
10
import java .io .IOException ;
9
11
import java .lang .reflect .Constructor ;
14
16
import java .util .Collections ;
15
17
import java .util .HashSet ;
16
18
import java .util .List ;
19
+ import java .util .Map .Entry ;
17
20
import java .util .Set ;
18
21
import java .util .stream .Stream ;
19
22
@@ -46,7 +49,7 @@ public class TransitiveDependencyHelper {
46
49
/**
47
50
* Reflected getter methods of Artifact class
48
51
*/
49
- private final Method artifactGetGroupIdMethod , artifactGetArtifactIdMethod , artifactGetBaseVersionMethod , artifactGetClassifierMethod ;
52
+ private final Method artifactGetGroupIdMethod , artifactGetArtifactIdMethod , artifactGetVersionMethod , artifactGetBaseVersionMethod , artifactGetClassifierMethod ;
50
53
51
54
/**
52
55
* LibraryManager instance, used in {@link #findTransitiveLibraries(Library)}
@@ -93,6 +96,8 @@ public TransitiveDependencyHelper(@NotNull LibraryManager libraryManager, @NotNu
93
96
artifactGetGroupIdMethod = artifactClass .getMethod ("getGroupId" );
94
97
// org.eclipse.aether.artifact.Artifact#getArtifactId()
95
98
artifactGetArtifactIdMethod = artifactClass .getMethod ("getArtifactId" );
99
+ // org.eclipse.aether.artifact.Artifact#getVersion()
100
+ artifactGetVersionMethod = artifactClass .getMethod ("getVersion" );
96
101
// org.eclipse.aether.artifact.Artifact#getBaseVersion()
97
102
artifactGetBaseVersionMethod = artifactClass .getMethod ("getBaseVersion" );
98
103
// org.eclipse.aether.artifact.Artifact#getClassifier()
@@ -111,7 +116,7 @@ public TransitiveDependencyHelper(@NotNull LibraryManager libraryManager, @NotNu
111
116
* </p>
112
117
* <p>
113
118
* Note: The method merges the repositories from both the library manager and the given library
114
- * for dependency resolution. And clones all relocations into transitive libraries.
119
+ * for dependency resolution. It also clones all relocations into transitive libraries.
115
120
* </p>
116
121
*
117
122
* @param library The primary library for which transitive dependencies need to be found.
@@ -132,16 +137,20 @@ public Collection<Library> findTransitiveLibraries(@NotNull Library library) {
132
137
133
138
Stream <String > repositories = Stream .of (globalRepositories , libraryRepositories ).flatMap (Collection ::stream );
134
139
try {
135
- Collection <?> artifacts = (Collection <?>) resolveTransitiveDependenciesMethod .invoke (transitiveDependencyCollectorObject ,
140
+ Collection <?> resolvedArtifacts = (Collection <?>) resolveTransitiveDependenciesMethod .invoke (transitiveDependencyCollectorObject ,
136
141
library .getGroupId (),
137
142
library .getArtifactId (),
138
143
library .getVersion (),
139
144
library .getClassifier (),
140
145
repositories );
141
- for (Object artifact : artifacts ) {
146
+ for (Object resolved : resolvedArtifacts ) {
147
+ Entry <?, ?> resolvedEntry = (Entry <?, ?>) resolved ;
148
+ Object artifact = resolvedEntry .getKey ();
149
+ @ Nullable String repository = (String ) resolvedEntry .getValue ();
150
+
142
151
String groupId = (String ) artifactGetGroupIdMethod .invoke (artifact );
143
152
String artifactId = (String ) artifactGetArtifactIdMethod .invoke (artifact );
144
- String version = (String ) artifactGetBaseVersionMethod .invoke (artifact );
153
+ String baseVersion = (String ) artifactGetBaseVersionMethod .invoke (artifact );
145
154
String classifier = (String ) artifactGetClassifierMethod .invoke (artifact );
146
155
147
156
if (library .getGroupId ().equals (groupId ) && library .getArtifactId ().equals (artifactId ))
@@ -153,7 +162,7 @@ public Collection<Library> findTransitiveLibraries(@NotNull Library library) {
153
162
Library .Builder libraryBuilder = Library .builder ()
154
163
.groupId (groupId )
155
164
.artifactId (artifactId )
156
- .version (version )
165
+ .version (baseVersion )
157
166
.isolatedLoad (library .isIsolatedLoad ())
158
167
.loaderId (library .getLoaderId ());
159
168
@@ -162,7 +171,30 @@ public Collection<Library> findTransitiveLibraries(@NotNull Library library) {
162
171
}
163
172
164
173
library .getRelocations ().forEach (libraryBuilder ::relocate );
165
- library .getRepositories ().forEach (libraryBuilder ::repository );
174
+
175
+ if (repository != null ) {
176
+ // Construct direct download URL
177
+
178
+ // Add ending "/" if missing
179
+ if (!repository .endsWith ("/" )) {
180
+ repository = repository + '/' ;
181
+ }
182
+
183
+ // TODO Uncomment the line below once LibraryManager#resolveLibrary stops resolving snapshots
184
+ // for every repository before trying direct URLs
185
+ // Make sure the repository is added as fallback if the dependency isn't found at the constructed URL
186
+ // libraryBuilder.repository(repository);
187
+
188
+ // For snapshots, getVersion() returns version-timestamp-buildNumber instead of version-SNAPSHOT
189
+ String version = (String ) artifactGetVersionMethod .invoke (artifact );
190
+
191
+ String partialPath = Util .craftPartialPath (artifactId , groupId , baseVersion );
192
+ String path = Util .craftPath (partialPath , artifactId , version , classifier );
193
+
194
+ libraryBuilder .url (repository + path );
195
+ } else {
196
+ library .getRepositories ().forEach (libraryBuilder ::repository );
197
+ }
166
198
167
199
transitiveLibraries .add (libraryBuilder .build ());
168
200
}
0 commit comments