|
| 1 | +package com.google.cloud; |
| 2 | + |
| 3 | +import static com.google.common.base.Preconditions.checkArgument; |
| 4 | + |
| 5 | +import com.google.cloud.tools.opensource.classpath.ClassPathBuilder; |
| 6 | +import com.google.cloud.tools.opensource.classpath.DependencyMediation; |
| 7 | +import com.google.cloud.tools.opensource.dependencies.Bom; |
| 8 | +import com.google.cloud.tools.opensource.dependencies.MavenRepositoryException; |
| 9 | +import java.nio.file.Paths; |
| 10 | +import java.util.HashSet; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Set; |
| 13 | +import java.util.stream.Collectors; |
| 14 | +import org.eclipse.aether.artifact.Artifact; |
| 15 | +import org.eclipse.aether.version.InvalidVersionSpecificationException; |
| 16 | + |
| 17 | +/** |
| 18 | + * A utility class to check unmanaged dependencies in BOM. |
| 19 | + */ |
| 20 | +public class UnmanagedDependencyCheck { |
| 21 | + // regex of handwritten artifacts |
| 22 | + private final static String downstreamArtifact = "(com.google.cloud:google-cloud-.*)|(com.google.api.grpc:(grpc|proto)-google-cloud-.*)"; |
| 23 | + |
| 24 | + |
| 25 | + /** |
| 26 | + * @param args An array with two elements.<p> The first string is the path of Java shared |
| 27 | + * dependencies BOM. <p> The second string is the path of a pom.xml contains BOM. |
| 28 | + */ |
| 29 | + public static void main(String[] args) |
| 30 | + throws MavenRepositoryException, InvalidVersionSpecificationException { |
| 31 | + checkArgument(args.length == 2, "The length of the inputs should be 2"); |
| 32 | + System.out.println(getUnmanagedDependencies(args[0], args[1])); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Returns dependency coordinates that are not managed by shared dependency BOM. |
| 37 | + * |
| 38 | + * @param sharedDependenciesBomPath the path of shared dependency BOM |
| 39 | + * @param projectBomPath the path of current project BOM |
| 40 | + * @return a list of unmanaged dependencies by the given version of shared dependency BOM |
| 41 | + * @throws MavenRepositoryException thrown if the artifacts in Bom can't be reached in remote or |
| 42 | + * local Maven repository |
| 43 | + * @throws InvalidVersionSpecificationException thrown if the shared dependency version can't be |
| 44 | + * parsed |
| 45 | + */ |
| 46 | + public static List<String> getUnmanagedDependencies( |
| 47 | + String sharedDependenciesBomPath, String projectBomPath) |
| 48 | + throws MavenRepositoryException, InvalidVersionSpecificationException { |
| 49 | + Set<String> sharedDependencies = getManagedDependencies(sharedDependenciesBomPath); |
| 50 | + Set<String> managedDependencies = getManagedDependencies(projectBomPath); |
| 51 | + |
| 52 | + return managedDependencies.stream() |
| 53 | + .filter(dependency -> !sharedDependencies.contains(dependency)) |
| 54 | + // handwritten artifacts, e.g., com.google.cloud:google-cloud-bigtable, should be excluded. |
| 55 | + .filter(dependency -> !dependency.matches(downstreamArtifact)) |
| 56 | + .collect(Collectors.toList()); |
| 57 | + } |
| 58 | + |
| 59 | + private static Set<String> getManagedDependencies(String projectBomPath) |
| 60 | + throws MavenRepositoryException, InvalidVersionSpecificationException { |
| 61 | + return getManagedDependenciesFromBom(Bom.readBom(Paths.get(projectBomPath))); |
| 62 | + } |
| 63 | + |
| 64 | + private static Set<String> getManagedDependenciesFromBom(Bom bom) |
| 65 | + throws InvalidVersionSpecificationException { |
| 66 | + Set<String> res = new HashSet<>(); |
| 67 | + new ClassPathBuilder() |
| 68 | + .resolve(bom.getManagedDependencies(), true, DependencyMediation.MAVEN) |
| 69 | + .getClassPath() |
| 70 | + .forEach( |
| 71 | + classPath -> { |
| 72 | + Artifact artifact = classPath.getArtifact(); |
| 73 | + res.add(String.format("%s:%s", artifact.getGroupId(), artifact.getArtifactId())); |
| 74 | + }); |
| 75 | + |
| 76 | + return res; |
| 77 | + } |
| 78 | + |
| 79 | + private UnmanagedDependencyCheck() { |
| 80 | + throw new IllegalStateException("Utility class"); |
| 81 | + } |
| 82 | +} |
0 commit comments