List of usage examples for org.apache.maven.artifact.versioning Restriction Restriction
public Restriction(ArtifactVersion lowerBound, boolean lowerBoundInclusive, ArtifactVersion upperBound, boolean upperBoundInclusive)
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 ww w. ja v a2 s . c om 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:org.codehaus.mojo.pomtools.wrapper.custom.DependencyWrapper.java
License:Apache License
protected boolean setBoundedVersion(String boundVersionSpec, boolean isLower) throws PomToolsVersionException { boolean isUpper = !isLower; try {/*from ww w . j a va2 s. c om*/ ModelVersionRange currentRange = getVersionRange(); ModelVersionRange newRange = ModelVersionRange.createFromVersionSpec(boundVersionSpec); if (currentRange != null && currentRange.hasRestrictions()) { Restriction curres = (Restriction) currentRange.getRestrictions().get(0); Restriction newres = (Restriction) newRange.getRestrictions().get(0); Restriction r = new Restriction( (isLower || curres.getLowerBound() == null) ? newres.getLowerBound() : curres.getLowerBound(), (isLower || curres.getLowerBound() == null) ? newres.isLowerBoundInclusive() : curres.isLowerBoundInclusive(), (isUpper || curres.getUpperBound() == null) ? newres.getUpperBound() : curres.getUpperBound(), (isUpper || curres.getUpperBound() == null) ? newres.isUpperBoundInclusive() : curres.isUpperBoundInclusive()); currentRange.getRestrictions().clear(); currentRange.getRestrictions().add(r); } else { currentRange = newRange; } String strVersion = currentRange.toString(); if (ModelHelper.isParsableVersion(strVersion)) { setVersion(currentRange.toString()); return true; } else { return false; } } catch (InvalidVersionSpecificationException e) { throw new PomToolsVersionException("Unable to parse version: " + boundVersionSpec, e); } }
From source file:org.commonjava.emb.component.vscheme.SchemeAwareVersionRange.java
License:Apache License
@SuppressWarnings({ "unchecked" })
private static Restriction parseRestriction(final String spec, final VersionScheme scheme)
throws InvalidVersionSpecificationException {
final boolean lowerBoundInclusive = spec.startsWith("[");
final boolean upperBoundInclusive = spec.endsWith("]");
final String process = spec.substring(1, spec.length() - 1).trim();
Restriction restriction;/*www .j av a2s . c o m*/
final int index = process.indexOf(",");
if (index < 0) {
if (!lowerBoundInclusive || !upperBoundInclusive) {
throw new InvalidVersionSpecificationException("Single version must be surrounded by []: " + spec);
}
final ArtifactVersion version = new SchemeAwareArtifactVersion(process, scheme);
restriction = new Restriction(version, lowerBoundInclusive, version, upperBoundInclusive);
} else {
final String lowerBound = process.substring(0, index).trim();
final String upperBound = process.substring(index + 1).trim();
if (lowerBound.equals(upperBound)) {
throw new InvalidVersionSpecificationException("Range cannot have identical boundaries: " + spec);
}
ArtifactVersion lowerVersion = null;
if (lowerBound.length() > 0) {
lowerVersion = new SchemeAwareArtifactVersion(lowerBound, scheme);
}
ArtifactVersion upperVersion = null;
if (upperBound.length() > 0) {
upperVersion = new SchemeAwareArtifactVersion(upperBound, scheme);
}
if (upperVersion != null && lowerVersion != null && upperVersion.compareTo(lowerVersion) < 0) {
throw new InvalidVersionSpecificationException("Range defies version ordering: " + spec);
}
restriction = new Restriction(lowerVersion, lowerBoundInclusive, upperVersion, upperBoundInclusive);
}
return restriction;
}
From source file:org.commonjava.emb.component.vscheme.SchemeAwareVersionRange.java
License:Apache License
@SuppressWarnings("unchecked") private List<Restriction> intersection(final List<Restriction> r1, final List<Restriction> r2) { final List<Restriction> restrictions = new ArrayList<Restriction>(r1.size() + r2.size()); final Iterator<Restriction> i1 = r1.iterator(); final Iterator<Restriction> i2 = r2.iterator(); Restriction res1 = i1.next();/*from w w w .ja v a 2s . c o m*/ Restriction res2 = i2.next(); boolean done = false; while (!done) { if (res1.getLowerBound() == null || res2.getUpperBound() == null || res1.getLowerBound().compareTo(res2.getUpperBound()) <= 0) { if (res1.getUpperBound() == null || res2.getLowerBound() == null || res1.getUpperBound().compareTo(res2.getLowerBound()) >= 0) { ArtifactVersion lower; ArtifactVersion upper; boolean lowerInclusive; boolean upperInclusive; // overlaps if (res1.getLowerBound() == null) { lower = res2.getLowerBound(); lowerInclusive = res2.isLowerBoundInclusive(); } else if (res2.getLowerBound() == null) { lower = res1.getLowerBound(); lowerInclusive = res1.isLowerBoundInclusive(); } else { final int comparison = res1.getLowerBound().compareTo(res2.getLowerBound()); if (comparison < 0) { lower = res2.getLowerBound(); lowerInclusive = res2.isLowerBoundInclusive(); } else if (comparison == 0) { lower = res1.getLowerBound(); lowerInclusive = res1.isLowerBoundInclusive() && res2.isLowerBoundInclusive(); } else { lower = res1.getLowerBound(); lowerInclusive = res1.isLowerBoundInclusive(); } } if (res1.getUpperBound() == null) { upper = res2.getUpperBound(); upperInclusive = res2.isUpperBoundInclusive(); } else if (res2.getUpperBound() == null) { upper = res1.getUpperBound(); upperInclusive = res1.isUpperBoundInclusive(); } else { final int comparison = res1.getUpperBound().compareTo(res2.getUpperBound()); if (comparison < 0) { upper = res1.getUpperBound(); upperInclusive = res1.isUpperBoundInclusive(); } else if (comparison == 0) { upper = res1.getUpperBound(); upperInclusive = res1.isUpperBoundInclusive() && res2.isUpperBoundInclusive(); } else { upper = res2.getUpperBound(); upperInclusive = res2.isUpperBoundInclusive(); } } // don't add if they are equal and one is not inclusive if (lower == null || upper == null || lower.compareTo(upper) != 0) { restrictions.add(new Restriction(lower, lowerInclusive, upper, upperInclusive)); } else if (lowerInclusive && upperInclusive) { restrictions.add(new Restriction(lower, lowerInclusive, upper, upperInclusive)); } // noinspection ObjectEquality if (upper == res2.getUpperBound()) { // advance res2 if (i2.hasNext()) { res2 = i2.next(); } else { done = true; } } else { // advance res1 if (i1.hasNext()) { res1 = i1.next(); } else { done = true; } } } else { // move on to next in r1 if (i1.hasNext()) { res1 = i1.next(); } else { done = true; } } } else { // move on to next in r2 if (i2.hasNext()) { res2 = i2.next(); } else { done = true; } } } return restrictions; }