From 1a01b52bea83730825b62b424dc3b283a008f8ac Mon Sep 17 00:00:00 2001 From: Michael Blow Date: Tue, 24 Mar 2020 08:13:36 -0400 Subject: [PATCH] [NO ISSUE][HYR][LIC] Support 'central' mirroring Fixes license plugin execution when 'central' is covered by a mirror. e.g. Execution default of goal org.apache.hyracks:license-automation-plugin:0.3.5-SNAPSHOT:generate failed: Unable to find 'central' remote repository! Change-Id: I443e240b9817e7541e476a7695c030362878fabe Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/5424 Integration-Tests: Jenkins Tested-by: Jenkins Reviewed-by: Till Westmann --- .../maven/license/SourcePointerResolver.java | 111 ++++++++++-------- 1 file changed, 64 insertions(+), 47 deletions(-) diff --git a/hyracks-fullstack/hyracks/hyracks-maven-plugins/license-automation-plugin/src/main/java/org/apache/hyracks/maven/license/SourcePointerResolver.java b/hyracks-fullstack/hyracks/hyracks-maven-plugins/license-automation-plugin/src/main/java/org/apache/hyracks/maven/license/SourcePointerResolver.java index 5c4a081b9f6..8ac8b12f5c6 100644 --- a/hyracks-fullstack/hyracks/hyracks-maven-plugins/license-automation-plugin/src/main/java/org/apache/hyracks/maven/license/SourcePointerResolver.java +++ b/hyracks-fullstack/hyracks/hyracks-maven-plugins/license-automation-plugin/src/main/java/org/apache/hyracks/maven/license/SourcePointerResolver.java @@ -20,12 +20,13 @@ import java.io.File; import java.io.IOException; -import java.util.Collection; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; import org.apache.commons.io.FileUtils; +import org.apache.commons.lang3.tuple.Pair; import org.apache.hyracks.maven.license.project.LicensedProjects; import org.apache.hyracks.maven.license.project.Project; import org.apache.maven.artifact.Artifact; @@ -45,6 +46,7 @@ public class SourcePointerResolver { + private static final String CENTRAL_REPO_ID = "central"; private final GenerateFileMojo mojo; private SourcePointerResolver(GenerateFileMojo mojo) { @@ -56,70 +58,85 @@ public static void execute(GenerateFileMojo mojo) throws ProjectBuildingExceptio instance.collectSourcePointers(); } - private ArtifactRepository getCentralRepository() { - for (ArtifactRepository repo : mojo.getSession().getRequest().getRemoteRepositories()) { - if ("central".equals(repo.getId())) { - return repo; + /** + * @return an ArtifactRepository pair representing the {@code central} repository, where the left element is how to + * reach the {@code central} repository, with the right element being the {@code central} repository itself. + * Note that these only differ when using a mirror to access {@code central} + */ + private Pair getCentralRepository() { + for (ArtifactRepository candidate : mojo.getSession().getRequest().getRemoteRepositories()) { + if (CENTRAL_REPO_ID.equals(candidate.getId())) { + return Pair.of(candidate, candidate); + } + for (ArtifactRepository mirrored : candidate.getMirroredRepositories()) { + if (CENTRAL_REPO_ID.equals(mirrored.getId())) { + return Pair.of(candidate, mirrored); + } } } - throw new IllegalStateException("Unable to find 'central' remote repository!"); + throw new IllegalStateException("Unable to find '" + CENTRAL_REPO_ID + "' remote repository!"); } private void collectSourcePointers() throws ProjectBuildingException, IOException { + List cddlProjects = new ArrayList<>(); + for (LicensedProjects lp : mojo.getLicenseMap().values()) { + if (lp.getLicense().getDisplayName() != null + && lp.getLicense().getDisplayName().toLowerCase().contains("cddl")) { + cddlProjects.addAll(lp.getProjects()); + } + } + if (cddlProjects.isEmpty()) { + return; + } try (StubArtifactRepository stubRepo = new StubArtifactRepository()) { DefaultRepositoryRequest rr = new DefaultRepositoryRequest(); rr.setLocalRepository(stubRepo); - ArtifactRepository central = getCentralRepository(); - rr.setRemoteRepositories(Collections.singletonList(central)); + Pair central = getCentralRepository(); + rr.setRemoteRepositories(Collections.singletonList(central.getLeft())); ArtifactResolutionRequest request = new ArtifactResolutionRequest(rr); - for (LicensedProjects lp : mojo.getLicenseMap().values()) { - if (lp.getLicense().getDisplayName() != null - && lp.getLicense().getDisplayName().toLowerCase().contains("cddl")) { - ensureCDDLSourcesPointer(lp.getProjects(), central, request); - } + for (Project cddlProject : cddlProjects) { + ensureCDDLSourcesPointer(cddlProject, central.getRight(), request); } } } - private void ensureCDDLSourcesPointer(Collection projects, ArtifactRepository central, + private void ensureCDDLSourcesPointer(Project project, ArtifactRepository central, ArtifactResolutionRequest request) throws ProjectBuildingException, IOException { - for (Project p : projects) { - if (p.getSourcePointer() != null) { - continue; - } - mojo.getLog().debug("finding sources for artifact: " + p); - Artifact sourcesArtifact = new DefaultArtifact(p.getGroupId(), p.getArtifactId(), p.getVersion(), - Artifact.SCOPE_COMPILE, "jar", "sources", null); - MavenProject mavenProject = mojo.resolveDependency(sourcesArtifact); - sourcesArtifact.setArtifactHandler(mavenProject.getArtifact().getArtifactHandler()); - final ArtifactRepository localRepo = mojo.getSession().getLocalRepository(); - final File marker = new File(localRepo.getBasedir(), localRepo.pathOf(sourcesArtifact) + ".oncentral"); - final File antimarker = new File(localRepo.getBasedir(), localRepo.pathOf(sourcesArtifact) + ".nocentral"); - boolean onCentral; - if (marker.exists() || antimarker.exists()) { - onCentral = marker.exists(); - } else { - request.setArtifact(sourcesArtifact); - ArtifactResolutionResult result = mojo.getArtifactResolver().resolve(request); - mojo.getLog().debug("result: " + result); - onCentral = result.isSuccess(); - if (onCentral) { - FileUtils.touch(marker); - } else { - FileUtils.touch(antimarker); - } - } - StringBuilder noticeBuilder = new StringBuilder("You may obtain "); - noticeBuilder.append(p.getName()).append(" in Source Code form code here:\n"); + if (project.getSourcePointer() != null) { + return; + } + mojo.getLog().debug("finding sources for artifact: " + project); + Artifact sourcesArtifact = new DefaultArtifact(project.getGroupId(), project.getArtifactId(), + project.getVersion(), Artifact.SCOPE_COMPILE, "jar", "sources", null); + MavenProject mavenProject = mojo.resolveDependency(sourcesArtifact); + sourcesArtifact.setArtifactHandler(mavenProject.getArtifact().getArtifactHandler()); + final ArtifactRepository localRepo = mojo.getSession().getLocalRepository(); + final File marker = new File(localRepo.getBasedir(), localRepo.pathOf(sourcesArtifact) + ".oncentral"); + final File antimarker = new File(localRepo.getBasedir(), localRepo.pathOf(sourcesArtifact) + ".nocentral"); + boolean onCentral; + if (marker.exists() || antimarker.exists()) { + onCentral = marker.exists(); + } else { + request.setArtifact(sourcesArtifact); + ArtifactResolutionResult result = mojo.getArtifactResolver().resolve(request); + mojo.getLog().debug("result: " + result); + onCentral = result.isSuccess(); if (onCentral) { - noticeBuilder.append(central.getUrl()).append("/").append(central.pathOf(sourcesArtifact)); + FileUtils.touch(marker); } else { - mojo.getLog().warn("Unable to find sources in 'central' for " + p + ", falling back to project url: " - + p.getUrl()); - noticeBuilder.append(p.getUrl() != null ? p.getUrl() : "MISSING SOURCE POINTER"); + FileUtils.touch(antimarker); } - p.setSourcePointer(noticeBuilder.toString()); } + StringBuilder noticeBuilder = new StringBuilder("You may obtain "); + noticeBuilder.append(project.getName()).append(" in Source Code form code here:\n"); + if (onCentral) { + noticeBuilder.append(central.getUrl()).append("/").append(central.pathOf(sourcesArtifact)); + } else { + mojo.getLog().warn("Unable to find sources on '" + CENTRAL_REPO_ID + "' for " + project + + ", falling back to project url: " + project.getUrl()); + noticeBuilder.append(project.getUrl() != null ? project.getUrl() : "MISSING SOURCE POINTER"); + } + project.setSourcePointer(noticeBuilder.toString()); } private static class StubArtifactRepository implements ArtifactRepository, AutoCloseable {