|
4 | 4 | import org.apache.commons.io.input.BOMInputStream; |
5 | 5 | import org.apache.commons.lang3.StringUtils; |
6 | 6 | import org.apache.maven.model.Model; |
| 7 | +import org.apache.maven.model.Parent; |
7 | 8 | import org.apache.maven.model.Scm; |
8 | 9 | import org.apache.maven.model.io.xpp3.MavenXpp3Reader; |
| 10 | +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
9 | 11 | import org.slf4j.Logger; |
10 | 12 | import org.slf4j.LoggerFactory; |
11 | 13 |
|
@@ -240,25 +242,11 @@ public DepResolution resolveRawDep(RawDependency d) { |
240 | 242 | return resolution; |
241 | 243 | } |
242 | 244 |
|
243 | | - // Get the url to the POM file for this artifact |
244 | | - String url = "http://central.maven.org/maven2/" |
245 | | - + groupId.replace('.', '/') + '/' + d.artifactID + '/' |
246 | | - + d.version + '/' + d.artifactID + '-' + d.version + ".pom"; |
247 | | - |
248 | 245 | DepResolution res = new DepResolution(d, null); |
249 | 246 |
|
250 | 247 | try { |
251 | 248 |
|
252 | | - InputStream input = new BOMInputStream(new URL(url).openStream()); |
253 | | - |
254 | | - MavenXpp3Reader xpp3Reader = new MavenXpp3Reader(); |
255 | | - Model model = xpp3Reader.read(input); |
256 | | - input.close(); |
257 | | - |
258 | | - Scm scm = model.getScm(); |
259 | | - if (scm != null) { |
260 | | - cloneURL = scm.getUrl(); |
261 | | - } |
| 249 | + cloneURL = getScmUrl(d); |
262 | 250 |
|
263 | 251 | if (cloneURL != null) { |
264 | 252 | res.Raw = d; |
@@ -351,5 +339,58 @@ private static boolean isJDK(Path jarFile) { |
351 | 339 | return PathUtil.normalize(jarFile.toString()).contains("jre/lib/"); |
352 | 340 | } |
353 | 341 |
|
| 342 | + /** |
| 343 | + * Tries to fetch POM model from maven central for a given dependency |
| 344 | + * @param dependency dependency to fetch model to |
| 345 | + * @return POM model if found and valid |
| 346 | + * @throws IOException |
| 347 | + * @throws XmlPullParserException |
| 348 | + */ |
| 349 | + private static Model fetchModel(RawDependency dependency) |
| 350 | + throws IOException, XmlPullParserException { |
| 351 | + |
| 352 | + // Get the url to the POM file for this artifact |
| 353 | + String url = "http://central.maven.org/maven2/" |
| 354 | + + dependency.groupID.replace('.', '/') + '/' + dependency.artifactID + '/' |
| 355 | + + dependency.version + '/' + dependency.artifactID + '-' + dependency.version + ".pom"; |
| 356 | + InputStream input = new BOMInputStream(new URL(url).openStream()); |
| 357 | + |
| 358 | + MavenXpp3Reader xpp3Reader = new MavenXpp3Reader(); |
| 359 | + Model model = xpp3Reader.read(input); |
| 360 | + input.close(); |
| 361 | + return model; |
| 362 | + } |
| 363 | + |
| 364 | + /** |
| 365 | + * This method tries to retrieve SCM URL, if POM model for given dependency does not specify SCM URL and |
| 366 | + * parent model belongs to the same group, we'll try to fecth URL from the parent model |
| 367 | + * @param dependency dependency to retrieve SCM URL for |
| 368 | + * @return SCM URL or null |
| 369 | + * @throws IOException |
| 370 | + * @throws XmlPullParserException |
| 371 | + */ |
| 372 | + private static String getScmUrl(RawDependency dependency) throws IOException, XmlPullParserException { |
| 373 | + Model model = fetchModel(dependency); |
| 374 | + while (model != null) { |
| 375 | + Scm scm = model.getScm(); |
| 376 | + if (scm != null) { |
| 377 | + return scm.getUrl(); |
| 378 | + } |
| 379 | + |
| 380 | + Parent parent = model.getParent(); |
| 381 | + if (parent == null) { |
| 382 | + return null; |
| 383 | + } |
| 384 | + if (!StringUtils.equals(parent.getGroupId(), dependency.groupID)) { |
| 385 | + return null; |
| 386 | + } |
| 387 | + dependency = new RawDependency(parent.getGroupId(), |
| 388 | + parent.getArtifactId(), |
| 389 | + parent.getVersion(), null, null); |
| 390 | + model = fetchModel(dependency); |
| 391 | + } |
| 392 | + return null; |
| 393 | + |
| 394 | + } |
354 | 395 |
|
355 | 396 | } |
0 commit comments