Example usage for org.apache.maven.artifact.versioning VersionRange getRestrictions

List of usage examples for org.apache.maven.artifact.versioning VersionRange getRestrictions

Introduction

In this page you can find the example usage for org.apache.maven.artifact.versioning VersionRange getRestrictions.

Prototype

public List<Restriction> getRestrictions() 

Source Link

Usage

From source file:com.exentes.maven.versions.UseReleasesMojo.java

License:Apache License

private Collection<String> processProject() throws MojoFailureException {
    List<String> errors = Lists.newArrayList();

    getLog().info("Processing all projects snapshots...");
    SetMultimap<Artifact, MavenProject> allSnapshotProjectArtifacts = VersionMojoUtils
            .allSnapshotProjectArtifacts(session);
    Map<String, Dependency> allDependenciesMap = allDependenciesMap(session);
    Map<Object, MavenProject> origins = origins(session);
    Set<MavenProject> projectsToUpdate = Sets.newHashSet();

    for (Map.Entry<Artifact, Collection<MavenProject>> artifactWithProjects : allSnapshotProjectArtifacts
            .asMap().entrySet()) {/*from w ww  . j  a  v a  2s .co m*/
        Artifact snapshotArtifact = artifactWithProjects.getKey();
        Collection<MavenProject> projects = artifactWithProjects.getValue();

        try {
            ArtifactResult artifactResult = resolveReleasedArtifact(snapshotArtifact);
            for (MavenProject project : projects) {
                Dependency dependencyToUpdate = allDependenciesMap
                        .get(projectArtifactKey(project, snapshotArtifact));

                if (isPropertyPlaceholder(dependencyToUpdate.getVersion())) {
                    MavenProject projectToUpdate = origins.get(dependencyToUpdate);
                    String propertyName = getPropertyName(dependencyToUpdate.getVersion());
                    Properties projectProperties = projectToUpdate.getOriginalModel().getProperties();
                    if (projectProperties != null && projectProperties.containsKey(propertyName)) {
                        String newVersion = null;
                        String versionPropertyValue = projectProperties.getProperty(propertyName);
                        VersionRange versionRange = VersionRange.createFromVersionSpec(versionPropertyValue);
                        if (versionRange.getRecommendedVersion() == null) {
                            int restrictionIndex = 0;
                            for (Restriction restriction : versionRange.getRestrictions()) {
                                if (restriction.isUpperBoundInclusive()) {
                                    if (snapshotArtifact.getVersion()
                                            .equals(restriction.getUpperBound().toString())) {
                                        DefaultArtifactVersion updatedVersion = new DefaultArtifactVersion(
                                                artifactResult.getArtifact().getVersion());
                                        Restriction updatedRestriction = new Restriction(
                                                restriction.getUpperBound().equals(restriction.getLowerBound())
                                                        ? updatedVersion
                                                        : restriction.getLowerBound(),
                                                restriction.isLowerBoundInclusive(), updatedVersion,
                                                restriction.isUpperBoundInclusive());
                                        versionRange.getRestrictions().set(restrictionIndex,
                                                updatedRestriction);
                                        newVersion = versionRange.toString();
                                        break;
                                    }
                                }
                                restrictionIndex++;
                            }
                        } else {
                            newVersion = artifactResult.getArtifact().getVersion();
                        }
                        if (newVersion != null) {
                            projectProperties.setProperty(propertyName, newVersion);
                            projectsToUpdate.add(projectToUpdate);
                            getLog().info("Version property {" + propertyName + "} in the project ["
                                    + projectToUpdate.getId() + "] set to the value: "
                                    + artifactResult.getArtifact().getVersion());
                            getLog().info("Snapshot dependency [" + snapshotArtifact + "] is replaced with ["
                                    + artifactResult.getArtifact() + "] in the project: ["
                                    + projectToUpdate.getId() + "]");
                        } else {
                            errors.add("Dependency version value '" + versionPropertyValue
                                    + "' defined as a value of property '" + propertyName + "' not supported");
                        }
                    } else {
                        errors.add("Property [" + propertyName + "] not found in the project ["
                                + projectsToUpdate + "] properties. The artifact version cannot be resolved");
                    }
                } else if (snapshotArtifact.getVersion().equals(dependencyToUpdate.getVersion())) {
                    MavenProject projectToUpdate = origins.get(dependencyToUpdate);
                    projectsToUpdate.add(projectToUpdate);
                    dependencyToUpdate.setVersion(artifactResult.getArtifact().getVersion());
                    getLog().info("Snapshot dependency [" + snapshotArtifact + "] is replaced with ["
                            + artifactResult.getArtifact() + "] in the project: [" + projectToUpdate.getId()
                            + "]");
                } else if (dependencyToUpdate.getVersion() == null) {
                    errors.add("Unknown version for the dependency [" + dependencyToUpdate + "]");
                } else {
                    // check if this is version range
                    try {
                        VersionRange versionRange = VersionRange
                                .createFromVersionSpec(dependencyToUpdate.getVersion());
                        if (versionRange.getRecommendedVersion() == null) {
                            // this this a version range. now all inclusive upper bounds should be inspected and the one which match resolved snapshot version must be upgraded
                            int restrictionIndex = 0;
                            for (Restriction restriction : versionRange.getRestrictions()) {
                                if (restriction.isUpperBoundInclusive()) {
                                    if (isPropertyPlaceholder(restriction.getUpperBound().toString())) {
                                        // try to update a property which is used as an upper version boundary
                                        String propertyName = getPropertyName(
                                                restriction.getUpperBound().toString());
                                        getLog().info("property name: " + propertyName);
                                        MavenProject projectToUpdate = origins.get(dependencyToUpdate);
                                        Properties projectProperties = projectToUpdate.getOriginalModel()
                                                .getProperties();
                                        if (projectProperties != null
                                                && projectProperties.containsKey(propertyName)
                                                && projectProperties.getProperty(propertyName)
                                                        .equals(snapshotArtifact.getVersion())) {
                                            projectProperties.setProperty(propertyName,
                                                    artifactResult.getArtifact().getVersion());
                                            projectsToUpdate.add(projectToUpdate);
                                            getLog().info(
                                                    "Version property {" + propertyName + "} in the project ["
                                                            + projectToUpdate.getId() + "] set to the value: "
                                                            + artifactResult.getArtifact().getVersion());
                                            getLog().info("Snapshot dependency [" + snapshotArtifact
                                                    + "] is replaced with [" + artifactResult.getArtifact()
                                                    + "] in the project: [" + projectToUpdate.getId() + "]");
                                            break;
                                        }
                                    } else {
                                        if (snapshotArtifact.getVersion()
                                                .equals(restriction.getUpperBound().toString())) {
                                            DefaultArtifactVersion updatedVersion = new DefaultArtifactVersion(
                                                    artifactResult.getArtifact().getVersion());
                                            Restriction updatedRestriction = new Restriction(
                                                    restriction.getUpperBound().equals(
                                                            restriction.getLowerBound()) ? updatedVersion
                                                                    : restriction.getLowerBound(),
                                                    restriction.isLowerBoundInclusive(), updatedVersion,
                                                    restriction.isUpperBoundInclusive());
                                            versionRange.getRestrictions().set(restrictionIndex,
                                                    updatedRestriction);
                                            MavenProject projectToUpdate = origins.get(dependencyToUpdate);
                                            projectsToUpdate.add(projectToUpdate);
                                            dependencyToUpdate.setVersion(versionRange.toString());
                                            getLog().info("Snapshot dependency [" + snapshotArtifact
                                                    + "] is replaced with [" + dependencyToUpdate
                                                    + "] in the project: [" + projectToUpdate.getId() + "]");
                                            break;
                                        }
                                    }
                                }
                                restrictionIndex++;
                            }
                        } else {
                            errors.add("Dependency version value [" + dependencyToUpdate.getVersion()
                                    + "] not supported");
                        }
                    } catch (InvalidVersionSpecificationException e) {
                        errors.add("Invalid version specified: " + dependencyToUpdate.getVersion()
                                + ". Exception when parsing version: " + e.getMessage());
                    }
                }
            }
        } catch (ArtifactResolutionException e) {
            getLog().info(e);
            errors.add("Failed to resolve a RELEASE version for [" + snapshotArtifact + "]. Exception: "
                    + e.getMessage());
        } catch (InvalidVersionSpecificationException e) {
            errors.add("Invalid version specified: " + snapshotArtifact.getVersion()
                    + ". Exception when parsing version: " + e.getMessage());
        }
    }

    if (!dryRun) {
        for (MavenProject project : projectsToUpdate) {
            try {
                modelWriter.write(project.getFile(), null, project.getOriginalModel());
            } catch (IOException e) {
                throw new MojoFailureException("Error writing POM", e);
            }
        }
    }
    return errors;
}

From source file:com.naughtyzombie.calypso.maven.mojo.BuildClasspathMojo.java

License:Apache License

/**
 * Copied from Artifact.VersionRange. This is tweaked to handle singular ranges properly. Currently the default
 * containsVersion method assumes a singular version means allow everything. This method assumes that "2.0.4" ==
 * "[2.0.4,)"//w ww .  jav a2 s  . co  m
 *
 * @param allowedRange range of allowed versions.
 * @param theVersion   the version to be checked.
 * @return true if the version is contained by the range.
 */
public static boolean containsVersion(VersionRange allowedRange, ArtifactVersion theVersion) {
    ArtifactVersion recommendedVersion = allowedRange.getRecommendedVersion();
    if (recommendedVersion == null) {
        @SuppressWarnings("unchecked")
        List<Restriction> restrictions = allowedRange.getRestrictions();
        for (Restriction restriction : restrictions) {
            if (restriction.containsVersion(theVersion)) {
                return true;
            }
        }
    }

    // only singular versions ever have a recommendedVersion
    return recommendedVersion.compareTo(theVersion) <= 0;
}

From source file:de.mytoys.maven.plugins.debug.AbstractDebugMojo.java

License:Apache License

/**
 * Copied from Artifact.VersionRange. This is tweaked to handle singular ranges properly. Currently the default
 * containsVersion method assumes a singular version means allow everything. This method assumes that "2.0.4" ==
 * "[2.0.4,)"//from  www.  ja  va 2 s .  com
 *
 * @param allowedRange range of allowed versions.
 * @param theVersion the version to be checked.
 * @return true if the version is contained by the range.
 */
public static boolean containsVersion(VersionRange allowedRange, ArtifactVersion theVersion) {
    ArtifactVersion recommendedVersion = allowedRange.getRecommendedVersion();
    if (recommendedVersion == null) {
        List<Restriction> restrictions = allowedRange.getRestrictions();
        for (Restriction restriction : restrictions) {
            if (restriction.containsVersion(theVersion)) {
                return true;
            }
        }
    }

    // only singular versions ever have a recommendedVersion
    return recommendedVersion.compareTo(theVersion) <= 0;
}

From source file:fr.synchrotron.soleil.ica.ci.maven.plugins.soleildependency.mojo.TreeMojo.java

License:Apache License

/**
 * Copied from Artifact.VersionRange. This is tweaked to handle singular ranges properly. Currently the default
 * containsVersion method assumes a singular version means allow everything. This method assumes that "2.0.4" ==
 * "[2.0.4,)"//from   w ww.  j a  v  a 2s.  com
 *
 * @param allowedRange range of allowed versions.
 * @param theVersion   the version to be checked.
 * @return true if the version is contained by the range.
 */
public static boolean containsVersion(VersionRange allowedRange, ArtifactVersion theVersion) {
    boolean matched = false;
    ArtifactVersion recommendedVersion = allowedRange.getRecommendedVersion();
    if (recommendedVersion == null) {

        for (Iterator i = allowedRange.getRestrictions().iterator(); i.hasNext() && !matched;) {
            Restriction restriction = (Restriction) i.next();
            if (restriction.containsVersion(theVersion)) {
                matched = true;
            }
        }
    } else {
        // only singular versions ever have a recommendedVersion
        int compareTo = recommendedVersion.compareTo(theVersion);
        matched = (compareTo <= 0);
    }
    return matched;
}

From source file:org.axway.grapes.maven.resolver.ArtifactResolver.java

/**
 * Finds a version out of a range//  www . ja  v  a2 s. co  m
 *
 * @param range VersionRange
 * @return String
 */
public static String getArtifactVersion(final VersionRange range) {
    if (range.getRecommendedVersion() != null) {
        return range.getRecommendedVersion().toString();
    }

    if (range.hasRestrictions()) {
        for (Restriction restriction : range.getRestrictions()) {
            if (restriction.getLowerBound() != null) {
                return restriction.getLowerBound().toString();
            }
            if (restriction.getUpperBound() != null) {
                return restriction.getLowerBound().toString();
            }
        }
    }

    return range.toString();
}

From source file:org.codehaus.mojo.pomtools.wrapper.custom.ModelVersionRange.java

License:Apache License

/** {@link VersionRange#hasRestrictions()} returns false if it has a recommendedVersion regarless
 * of whether it has restrictions or not.  We actually need to know if it has restrictions,
 * so this function re-implements the hasRestrictions to actually determine if the range has
 * restrictions.//from  www  .  ja va 2s .  com
 */
public static boolean hasRestrictions(VersionRange range) {
    if (range == null) {
        return false;
    }

    List restrictions = range.getRestrictions();
    if (restrictions != null && !restrictions.isEmpty()) {
        if (restrictions.size() > 1) {
            return true;
        }

        Restriction restriction = (Restriction) restrictions.get(0);
        return restriction.getLowerBound() != null || restriction.getUpperBound() != null;
    } else {
        return false;
    }
}

From source file:org.codehaus.mojo.pomtools.wrapper.custom.ModelVersionRange.java

License:Apache License

/** {@link VersionRange#toString()} simply returns the recommended version if it has
 * a recommended version; otherwise it builds a proper string based on the restrictions.
 * We need the string build with restrictions if there are any; so this is a copy of the guts
 * of {@link VersionRange#toString()} except we build the string with restrictions regardless 
 * of having a recommendedVersion./*w  w w .  ja  v  a  2s  .co m*/
 */
public static String toString(VersionRange range) {
    if (range == null) {
        return null;
    }

    if (!hasRestrictions(range)) {
        return range.toString();
    } else {
        StringBuffer buf = new StringBuffer();
        for (Iterator i = range.getRestrictions().iterator(); i.hasNext();) {
            Restriction r = (Restriction) i.next();

            buf.append(r.isLowerBoundInclusive() ? "[" : "(");
            if (r.getLowerBound() != null) {
                buf.append(r.getLowerBound().toString());
            }
            buf.append(",");
            if (r.getUpperBound() != null) {
                buf.append(r.getUpperBound().toString());
            }
            buf.append(r.isUpperBoundInclusive() ? "]" : ")");

            if (i.hasNext()) {
                buf.append(",");
            }
        }
        return buf.toString();
    }
}

From source file:org.codehaus.mojo.versions.api.ArtifactVersions.java

License:Apache License

/**
 * Checks if the version is in the range (and ensures that the range respects the <code>-!</code> syntax
 * to rule out any qualifiers from range boundaries).
 *
 * @param version the version to check./*from w  ww. j  av a 2s .  co  m*/
 * @param range   the range to check.
 * @return <code>true</code> if and only if the version is in the range.
 * @since 1.3
 */
public static boolean isVersionInRange(ArtifactVersion version, VersionRange range) {
    if (!range.containsVersion(version)) {
        return false;
    }
    for (Restriction r : ((List<Restriction>) range.getRestrictions())) {
        if (r.containsVersion(version)) {
            // check for the -! syntax
            if (!r.isLowerBoundInclusive() && r.getLowerBound() != null) {
                String s = r.getLowerBound().toString();
                if (s.endsWith("-!") && version.toString().startsWith(s.substring(0, s.length() - 2))) {
                    return false;
                }
            }
            if (!r.isUpperBoundInclusive() && r.getUpperBound() != null) {
                String s = r.getUpperBound().toString();
                if (s.endsWith("-!") && version.toString().startsWith(s.substring(0, s.length() - 2))) {
                    return false;
                }
            }
        }
    }
    return true;
}