Skip to content

Commit

Permalink
[NO ISSUE][HYR][LIC] Support 'central' mirroring
Browse files Browse the repository at this point in the history
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 <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Till Westmann <tillw@apache.org>
  • Loading branch information
mblow committed Mar 25, 2020
1 parent 7d660a0 commit 1a01b52
Showing 1 changed file with 64 additions and 47 deletions.
Expand Up @@ -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;
Expand All @@ -45,6 +46,7 @@

public class SourcePointerResolver {

private static final String CENTRAL_REPO_ID = "central";
private final GenerateFileMojo mojo;

private SourcePointerResolver(GenerateFileMojo mojo) {
Expand All @@ -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<ArtifactRepository, ArtifactRepository> 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<Project> 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<ArtifactRepository, ArtifactRepository> 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<Project> 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 {
Expand Down

0 comments on commit 1a01b52

Please sign in to comment.