Skip to content

Commit e71d278

Browse files
committed
feat: get dependencies from bom
1 parent cf32e72 commit e71d278

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

java-shared-dependencies/dependency-analyzer/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@
6464
<artifactId>guava</artifactId>
6565
<version>33.3.1-jre</version>
6666
</dependency>
67+
<dependency>
68+
<groupId>com.google.cloud.tools</groupId>
69+
<artifactId>dependencies</artifactId>
70+
<version>1.5.13</version>
71+
</dependency>
6772
<!-- test dependencies -->
6873
<dependency>
6974
<groupId>org.mockito</groupId>

java-shared-dependencies/dependency-analyzer/src/main/java/com/google/cloud/DependencyAnalyzer.java

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,22 @@
1313
import com.google.cloud.model.Result;
1414
import com.google.cloud.model.Version;
1515
import com.google.cloud.model.VersionKey;
16+
import com.google.cloud.tools.opensource.classpath.ClassPathBuilder;
17+
import com.google.cloud.tools.opensource.classpath.DependencyMediation;
18+
import com.google.cloud.tools.opensource.dependencies.Bom;
19+
import com.google.cloud.tools.opensource.dependencies.MavenRepositoryException;
1620
import java.io.IOException;
1721
import java.net.URISyntaxException;
1822
import java.net.http.HttpClient;
23+
import java.nio.file.Paths;
1924
import java.util.ArrayDeque;
2025
import java.util.ArrayList;
2126
import java.util.HashSet;
2227
import java.util.List;
2328
import java.util.Queue;
2429
import java.util.Set;
30+
import org.eclipse.aether.artifact.Artifact;
31+
import org.eclipse.aether.version.InvalidVersionSpecificationException;
2532

2633
public class DependencyAnalyzer {
2734

@@ -34,6 +41,48 @@ public DependencyAnalyzer(DepsDevClient depsDevClient) {
3441
public AnalysisResult analyze(String system, String packageName, String packageVersion)
3542
throws URISyntaxException, IOException, InterruptedException, IllegalArgumentException {
3643
VersionKey root = VersionKey.from(system, packageName, packageVersion);
44+
return AnalysisResult.of(getPackageInfoFrom(root));
45+
}
46+
47+
public AnalysisResult analyze(String bomPath) {
48+
List<PackageInfo> packageInfos = new ArrayList<>();
49+
try {
50+
Set<VersionKey> roots = getManagedDependenciesFromBom(Bom.readBom(Paths.get(bomPath)));
51+
for (VersionKey versionKey : roots) {
52+
packageInfos.addAll(getPackageInfoFrom(versionKey));
53+
}
54+
55+
} catch (MavenRepositoryException | InvalidVersionSpecificationException ex) {
56+
System.out.printf("Caught exception when resolving dependencies from %s.", bomPath);
57+
ex.printStackTrace();
58+
System.exit(1);
59+
} catch (URISyntaxException | IOException | InterruptedException ex) {
60+
System.out.print("Caught exception when retrieving dependency info from https://deps.dev/.");
61+
ex.printStackTrace();
62+
System.exit(2);
63+
}
64+
65+
return AnalysisResult.of(packageInfos);
66+
}
67+
68+
private static Set<VersionKey> getManagedDependenciesFromBom(Bom bom)
69+
throws InvalidVersionSpecificationException {
70+
Set<VersionKey> res = new HashSet<>();
71+
new ClassPathBuilder()
72+
.resolve(bom.getManagedDependencies(), false, DependencyMediation.MAVEN)
73+
.getClassPath()
74+
.forEach(
75+
classPath -> {
76+
Artifact artifact = classPath.getArtifact();
77+
String pkg = String.format("%s:%s", artifact.getGroupId(), artifact.getArtifactId());
78+
res.add(VersionKey.from("MAVEN", pkg, artifact.getVersion()));
79+
});
80+
81+
return res;
82+
}
83+
84+
private List<PackageInfo> getPackageInfoFrom(VersionKey root)
85+
throws URISyntaxException, IOException, InterruptedException {
3786
Set<VersionKey> seenPackage = new HashSet<>();
3887
seenPackage.add(root);
3988
Queue<VersionKey> queue = new ArrayDeque<>();
@@ -68,7 +117,7 @@ public AnalysisResult analyze(String system, String packageName, String packageV
68117
result.add(new PackageInfo(versionKey, licenses, advisories));
69118
}
70119

71-
return AnalysisResult.of(result);
120+
return result;
72121
}
73122

74123
/**

0 commit comments

Comments
 (0)