Skip to content
This repository was archived by the owner on May 29, 2018. It is now read-only.

Commit edfb4c0

Browse files
committed
Handling case when SCM information is stored in the parent POM descriptor
- sometimes SCM information is stored in the parent POM descriptor (for example com.google.truth/truth). When artifact's POM does not contain SCM but there is a parent within the same group, we'll try to fetch SCM URL from the parent POM
1 parent 9483850 commit edfb4c0

1 file changed

Lines changed: 56 additions & 15 deletions

File tree

src/main/java/com/sourcegraph/javagraph/Resolver.java

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import org.apache.commons.io.input.BOMInputStream;
55
import org.apache.commons.lang3.StringUtils;
66
import org.apache.maven.model.Model;
7+
import org.apache.maven.model.Parent;
78
import org.apache.maven.model.Scm;
89
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
10+
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
911
import org.slf4j.Logger;
1012
import org.slf4j.LoggerFactory;
1113

@@ -240,25 +242,11 @@ public DepResolution resolveRawDep(RawDependency d) {
240242
return resolution;
241243
}
242244

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-
248245
DepResolution res = new DepResolution(d, null);
249246

250247
try {
251248

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);
262250

263251
if (cloneURL != null) {
264252
res.Raw = d;
@@ -351,5 +339,58 @@ private static boolean isJDK(Path jarFile) {
351339
return PathUtil.normalize(jarFile.toString()).contains("jre/lib/");
352340
}
353341

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+
}
354395

355396
}

0 commit comments

Comments
 (0)