Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.google.cloud.tools.opensource.cloudbomdashboard;

import com.google.cloud.tools.opensource.dependencies.RepositoryUtility;
import com.google.common.collect.ImmutableList;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
Expand All @@ -28,11 +30,20 @@
import org.apache.commons.io.FileUtils;
import org.apache.maven.artifact.repository.metadata.Metadata;
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.model.building.DefaultModelBuilderFactory;
import org.apache.maven.model.building.DefaultModelBuildingRequest;
import org.apache.maven.model.building.ModelBuilder;
import org.apache.maven.model.building.ModelBuildingException;
import org.apache.maven.model.building.ModelBuildingRequest;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.project.ProjectModelResolver;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.internal.impl.DefaultRemoteRepositoryManager;
import org.eclipse.aether.repository.RemoteRepository;

/** Container class for all artifact data pulled from Maven central. */
public class ArtifactMavenData {
Expand Down Expand Up @@ -95,7 +106,8 @@ public String getSharedDependenciesVersion() {
return sharedDependenciesVersion;
}

public static ArtifactMavenData generateArtifactMavenData(Artifact artifact) {
public static ArtifactMavenData generateArtifactMavenData(Artifact artifact)
throws ModelBuildingException {
String metadataUrl = generateMetadataUrl(artifact);
String pomFileUrl = generatePomFileUrl(artifact);

Expand Down Expand Up @@ -155,7 +167,7 @@ private static LatestMetadata getLatestVersionAndLastUpdated(String metadataUrl)
}

private static SharedDependenciesData sharedDependencyPositionAndVersion(
String pomUrl, Artifact artifact) {
String pomUrl, Artifact artifact) throws ModelBuildingException {
String groupPath = artifact.getGroupId().replace('.', '/');
String parentPath =
DashboardMain.basePath
Expand Down Expand Up @@ -211,7 +223,8 @@ private static SharedDependenciesData sharedDependencyPositionAndVersion(
return new SharedDependenciesData("", "");
}

private static String getSharedDependenciesVersionFromUrl(String pomUrl) {
private static String getSharedDependenciesVersionFromUrl(String pomUrl)
throws ModelBuildingException {
try {
File pomFile = File.createTempFile("pomFile", ".xml");
pomFile.deleteOnExit();
Expand All @@ -220,20 +233,31 @@ private static String getSharedDependenciesVersionFromUrl(String pomUrl) {
FileUtils.copyInputStreamToFile(input, pomFile);
MavenXpp3Reader read = new MavenXpp3Reader();
Model model = read.read(new FileInputStream(pomFile));
if (model.getDependencyManagement() == null) {
return null;
}
for (Dependency dep : model.getDependencyManagement().getDependencies()) {
if ("com.google.cloud".equals(dep.getGroupId())
&& "google-cloud-shared-dependencies".equals(dep.getArtifactId())) {
if (dep.getVersion().startsWith("${")) {
String sharedVersion = dep.getVersion().substring(1).replaceAll("[{}]", "");
return model.getProperties().getProperty(sharedVersion);
}
return dep.getVersion();
}
}

ModelBuildingRequest request = new DefaultModelBuildingRequest();
request.setRawModel(model);
RepositorySystem repositorySystem = RepositoryUtility.newRepositorySystem();
RepositorySystemSession session = RepositoryUtility.newSession(repositorySystem);

// Set model resolver to locate parent POM on Maven Central.
RemoteRepository mavenCentral =
new RemoteRepository.Builder("central", "default", "https://repo1.maven.org/maven2/")
.build();
request.setModelResolver(
new ProjectModelResolver(
session,
null,
repositorySystem,
new DefaultRemoteRepositoryManager(),
ImmutableList.of(mavenCentral),
null,
null));

// Capture java version from system environment for profile activation.
request.setSystemProperties(System.getProperties());

ModelBuilder builder = new DefaultModelBuilderFactory().newInstance();
Model effectiveModel = builder.build(request).getEffectiveModel();
return effectiveModel.getProperties().getProperty("google-cloud-shared-dependencies.version");
} catch (XmlPullParserException | IOException ignored) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we know why these exceptions are ignored, but the ModelBuildingException is thrown?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a good question. It is a little unclear as to why the exceptions were ignored. https://effectivejava.googleplex.com/third-edition#page=331 advices against this especially without appropriate comments explaining the reasoning and suggests the following to avoid silent failures:

Merely letting an exception propagate outward can at least cause the
program to fail swiftly, preserving information to aid in debugging the failure.

}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import java.util.SortedSet;
import org.apache.commons.cli.ParseException;
import org.apache.maven.artifact.versioning.ComparableVersion;
import org.apache.maven.model.building.ModelBuildingException;
import org.eclipse.aether.RepositoryException;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.artifact.Artifact;
Expand Down Expand Up @@ -89,7 +90,7 @@ public class DashboardMain {
*/
public static void main(String[] arguments)
throws IOException, TemplateException, RepositoryException, URISyntaxException,
ParseException, MavenRepositoryException {
ParseException, MavenRepositoryException, ModelBuildingException {
DashboardArguments dashboardArguments = DashboardArguments.readCommandLine(arguments);

// If looking to edit the dashboard structure, see DashboardMain#generateDashboard.
Expand Down Expand Up @@ -131,7 +132,7 @@ public static void main(String[] arguments)

private static void generateAllVersions(String versionlessCoordinates)
throws IOException, TemplateException, RepositoryException, URISyntaxException,
MavenRepositoryException {
MavenRepositoryException, ModelBuildingException {
List<String> elements = Splitter.on(':').splitToList(versionlessCoordinates);
checkArgument(
elements.size() == 2,
Expand Down Expand Up @@ -164,7 +165,8 @@ private static void generateAllVersions(String versionlessCoordinates)
generateAllVersionsDashboard();
}

private static Path generate(Bom bom) throws IOException, TemplateException, URISyntaxException {
private static Path generate(Bom bom)
throws IOException, TemplateException, URISyntaxException, ModelBuildingException {
ArtifactCache cache = buildCache(bom);
Path output = generateHtml(bom, cache);

Expand All @@ -190,7 +192,8 @@ private static ArtifactCache buildCache(Bom bom) {
return loadArtifactInfo(managedDependencies);
}

private static boolean report(Bom bom, OutputStream outputStream) throws IOException {
private static boolean report(Bom bom, OutputStream outputStream)
throws IOException, ModelBuildingException {
ArtifactCache cache = buildCache(bom);
Map<Artifact, ArtifactInfo> infoMap = cache.getInfoMap();
String cloudBomVersion =
Expand Down Expand Up @@ -253,7 +256,7 @@ private static Path outputDirectory(String groupId, String artifactId, String ve
}

private static Path generateHtml(Bom bom, ArtifactCache cache)
throws IOException, TemplateException, URISyntaxException {
throws IOException, TemplateException, URISyntaxException, ModelBuildingException {

Artifact bomArtifact = new DefaultArtifact(bom.getCoordinates());

Expand Down Expand Up @@ -425,7 +428,7 @@ static void generateAllVersionsDashboard()
@VisibleForTesting
static void generateDashboard(
Path output, List<ArtifactResults> table, ArtifactCache cache, Bom bom)
throws IOException, TemplateException {
throws IOException, TemplateException, ModelBuildingException {

Map<Artifact, ArtifactInfo> infoMap = cache.getInfoMap();
String cloudBomVersion =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.apache.maven.model.building.ModelBuildingException;
import org.eclipse.aether.artifact.Artifact;

/**
Expand All @@ -42,7 +43,8 @@ private VersionData() {}
* @param cloudBomVersion the version of google-cloud-bom associated with the set of artifacts
* @param artifacts the set of artifacts to be pulled from Maven central
*/
public static void addData(String cloudBomVersion, Set<Artifact> artifacts) {
public static void addData(String cloudBomVersion, Set<Artifact> artifacts)
throws ModelBuildingException {
cloudBomVersionToArtifacts.put(cloudBomVersion, artifacts);
for (Artifact artifact : artifacts) {
if (!artifactData.containsKey(artifact)) {
Expand Down