private Feature getPreviousFeature()

in src/main/java/org/apache/sling/feature/maven/mojos/FeaturesDiffMojo.java [129:189]


    private Feature getPreviousFeature(Feature current) throws MojoExecutionException, MojoFailureException {
        VersionRange range;
        try {
            range = VersionRange.createFromVersionSpec(comparisonVersion);
        } catch (InvalidVersionSpecificationException e) {
            throw new MojoFailureException("Invalid comparison version: " + e.getMessage());
        }

        org.eclipse.aether.artifact.Artifact previousArtifact = new DefaultArtifact(
                current.getId().getGroupId(),
                current.getId().getArtifactId(),
                current.getId().getType(),
                current.getId().getClassifier(),
                comparisonVersion);

        try {

            if (!range.isSelectedVersionKnown(RepositoryUtils.toArtifact(previousArtifact))) {
                getLog().debug("Searching for versions in range: " + comparisonVersion);
                VersionRangeRequest vrr =
                        new VersionRangeRequest(previousArtifact, project.getRemoteProjectRepositories(), null);
                VersionRangeResult vrResult = repoSystem.resolveVersionRange(mavenSession.getRepositorySession(), vrr);
                // filter out snapshots
                Optional<Version> version = vrResult.getVersions().stream()
                        .filter(v -> !ArtifactUtils.isSnapshot(v.toString()))
                        .reduce((first, second) -> second);
                previousArtifact.setVersion(version.map(Version::toString).orElse(null));
            }
        } catch (OverConstrainedVersionException ocve) {
            throw new MojoFailureException("Invalid comparison version: " + ocve.getMessage());
        } catch (VersionRangeResolutionException e) {
            throw new MojoExecutionException("Error determining previous version: " + e.getMessage(), e);
        }

        if (previousArtifact.getVersion() == null) {
            getLog().info("Unable to find a previous version of the " + current + " Feature in the repository");
            return null;
        }

        File featureFile = null;
        try {
            ArtifactRequest artifactRequest =
                    new ArtifactRequest(previousArtifact, project.getRemoteProjectRepositories(), null);
            ArtifactResult artifactResult =
                    repoSystem.resolveArtifact(mavenSession.getRepositorySession(), artifactRequest);
            featureFile = artifactResult.getArtifact().getFile();
        } catch (org.eclipse.aether.resolution.ArtifactResolutionException e) {
            getLog().warn("Artifact " + previousArtifact + " cannot be resolved : " + e.getMessage(), e);
        }

        if (featureFile == null || !featureFile.exists()) {
            return null;
        }

        try (FileReader reader = new FileReader(featureFile)) {
            return FeatureJSONReader.read(reader, featureFile.getAbsolutePath());
        } catch (IOException e) {
            throw new MojoExecutionException(
                    "An error occurred while reading the " + featureFile + " Feature file:", e);
        }
    }