Commit 610b8263 authored by Brian Long's avatar Brian Long
Browse files

attempt to improve ban config version range selection

parent 2bbd70c3
Loading
Loading
Loading
Loading
+19 −20
Original line number Diff line number Diff line
@@ -134,24 +134,22 @@ public abstract class AbstractBanConfiguration implements BanConfiguration {
        return new BanConfigurationDownloader(this.session, this.artifactResolver, this.versionRangeResolver, url);
    }
    
    private BanConfigurationDownloader getArtifactDownloader(Artifact artifact, String logId) throws IOException, MojoFailureException {
    private BanConfigurationDownloader getArtifactDownloader(Artifact artifact, String artifactSpec) throws IOException, MojoFailureException {
        if (!"xml".equals(artifact.getExtension()))
            artifact = new DefaultArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getClassifier(), "xml", artifact.getVersion());

        Version latestVersion = this.findLatestVersion(artifact, logId);
        Artifact latestArtifact = this.findLatestArtifact(artifact, logId);
        if (latestArtifact == null && latestVersion != null) {
            this.logger.debug("A latest version was found, but could not resolve the artifact using the range; trying to resolve the artifact with the specific version: {}: {}", latestVersion, logId);
            artifact = artifact.setVersion(latestVersion.toString());
            latestArtifact = this.findLatestArtifact(artifact, logId);
        }
        Version selectedVersion = this.findLatestEligibleVersion(artifact, artifactSpec);
        if (selectedVersion == null)
            throw new MojoFailureException("An eligible version of the ban configuration could not be found: " + artifactSpec);
        
        if (latestArtifact != null && latestArtifact.getFile() != null) {
            this.logger.debug("The latest artifact was found: {}", latestArtifact);
            File file = latestArtifact.getFile();
        artifact.setVersion(selectedVersion.toString());
        Artifact selectedArtifact = this.resolveArtifact(artifact, artifactSpec);
        if (selectedArtifact != null) {
            File file = selectedArtifact.getFile();
            this.logger.debug("The ban configuration was found: {}: {}", selectedArtifact, file);
            return new BanConfigurationDownloader(this.session, this.artifactResolver, this.versionRangeResolver, file);
        } else {
            this.logger.debug("Using the raw artifact: {}", artifact);
            this.logger.debug("Using the raw artifact for the ban configuration: {}", artifact);
            File file = artifact.getFile();
            return new BanConfigurationDownloader(this.session, this.artifactResolver, this.versionRangeResolver, file);
        }
@@ -176,30 +174,31 @@ public abstract class AbstractBanConfiguration implements BanConfiguration {
        this.excludeArtifacts.addAll(downloader.getExcludeArtifacts());
    }
    
    private Version findLatestVersion(Artifact artifact, String logId) {
        this.logger.trace("Inspecting the local and remote repositories to select the version to import: {}", logId);
    private Version findLatestEligibleVersion(Artifact artifact, String artifactSpec) {
        this.logger.trace("Inspecting the local/remote repositories to select the version to import: {}", artifactSpec);
        VersionRangeRequest vrrequest = new VersionRangeRequest(artifact, this.session.getCurrentProject().getRemoteProjectRepositories(), null);
        try {
            VersionRangeResult vrresult = this.versionRangeResolver.resolveVersionRange(this.session.getRepositorySession(), vrrequest);
            this.logger.trace("Found artifact versions: {}: {}", artifactSpec, vrresult.getVersions());
            if (vrresult.getVersions().isEmpty()) {
                this.logger.debug("The artifact version range could not be resolved: {}", logId);
                this.logger.debug("No versions could be found in the artifact version range: {}", artifactSpec);
                return null;
            } else {
                this.logger.debug("The artifact version discovered: {}: {}", vrresult.getHighestVersion(), logId);
                this.logger.debug("{} versions found in the artifact version range; selecting the latest: {}: {}", vrresult.getVersions().size(), artifactSpec, vrresult.getHighestVersion());
                return vrresult.getHighestVersion();
            }
        } catch (VersionRangeResolutionException vrre) {
            this.logger.error("The artifact version range could not be resolved; skipping: {}", logId);
            this.logger.error("The artifact version range could not be resolved; skipping: {}", artifactSpec);
            return null;
        }
    }
    
    private Artifact findLatestArtifact(Artifact artifact, String logId) {
        this.logger.trace("Inspecting the local/remote repositories to select the artifact to import: {}", logId);
    private Artifact resolveArtifact(Artifact artifact, String artifactSpec) {
        this.logger.trace("Inspecting the local/remote repositories to select the artifact to import: {}", artifactSpec);
        ArtifactRequest arequest = new ArtifactRequest(artifact, this.session.getCurrentProject().getRemoteProjectRepositories(), null);
        try {
            ArtifactResult aresult = this.artifactResolver.resolveArtifact(this.session.getRepositorySession(), arequest);
            this.logger.debug("This artifact version discovered: {}: {}", aresult.getArtifact().getVersion(), logId);
            this.logger.debug("This artifact version discovered: {}: {}", aresult.getArtifact().getVersion(), artifactSpec);
            return aresult.getArtifact();
        } catch (ArtifactResolutionException are) {
            this.logger.warn("The artifact could not be resolved; skipping: {}", artifact);