use of org.apache.maven.artifact.versioning.ArtifactVersion in project semantic-versioning by jeluard.
the class AbstractEnforcerRule method execute.
@Override
public void execute(final EnforcerRuleHelper helper) throws EnforcerRuleException {
final MavenProject project = getMavenProject(helper);
if (shouldSkipRuleExecution(project)) {
helper.getLog().debug("Skipping non " + AbstractEnforcerRule.JAR_ARTIFACT_TYPE + " or " + BUNDLE_ARTIFACT_TYPE + " artifact.");
return;
}
final Artifact previousArtifact;
final Artifact currentArtifact = validateArtifact(project.getArtifact());
final Version current = Version.parse(currentArtifact.getVersion());
try {
final ArtifactRepository localRepository = (ArtifactRepository) helper.evaluate("${localRepository}");
final String version;
if (this.previousVersion != null) {
version = this.previousVersion;
helper.getLog().info("Version specified as <" + version + ">");
} else {
final ArtifactMetadataSource artifactMetadataSource = (ArtifactMetadataSource) helper.getComponent(ArtifactMetadataSource.class);
final List<ArtifactVersion> availableVersions = getAvailableReleasedVersions(artifactMetadataSource, project, localRepository);
final List<ArtifactVersion> availablePreviousVersions = filterNonPreviousVersions(availableVersions, current);
if (availablePreviousVersions.isEmpty()) {
helper.getLog().warn("No previously released version. Backward compatibility check not performed.");
return;
}
version = availablePreviousVersions.iterator().next().toString();
helper.getLog().info("Version deduced as <" + version + "> (among all availables: " + availablePreviousVersions + ")");
}
final ArtifactFactory artifactFactory = (ArtifactFactory) helper.getComponent(ArtifactFactory.class);
previousArtifact = artifactFactory.createArtifact(project.getGroupId(), project.getArtifactId(), version, null, project.getArtifact().getType());
final ArtifactResolver resolver = (ArtifactResolver) helper.getComponent(ArtifactResolver.class);
resolver.resolve(previousArtifact, project.getRemoteArtifactRepositories(), localRepository);
validateArtifact(previousArtifact);
} catch (Exception e) {
helper.getLog().warn("Exception while accessing artifacts; skipping check.", e);
return;
}
final Version previous = Version.parse(previousArtifact.getVersion());
final File previousJar = previousArtifact.getFile();
final File currentJar = currentArtifact.getFile();
compareJars(helper, previous, previousJar, current, currentJar);
}
use of org.apache.maven.artifact.versioning.ArtifactVersion in project semantic-versioning by jeluard.
the class AbstractEnforcerRule method getAvailableReleasedVersions.
/**
* @param artifactMetadataSource
* @param project
* @param localRepository
* @return all available versions from most recent to oldest
* @throws ArtifactMetadataRetrievalException
*/
protected final List<ArtifactVersion> getAvailableReleasedVersions(final ArtifactMetadataSource artifactMetadataSource, final MavenProject project, final ArtifactRepository localRepository) throws ArtifactMetadataRetrievalException {
final List<ArtifactVersion> availableVersions = artifactMetadataSource.retrieveAvailableVersions(project.getArtifact(), localRepository, project.getRemoteArtifactRepositories());
availableVersions.remove(new DefaultArtifactVersion(project.getArtifact().getVersion()));
for (final Iterator<ArtifactVersion> iterator = availableVersions.iterator(); iterator.hasNext(); ) {
final ArtifactVersion artifactVersion = iterator.next();
if (Version.parse(artifactVersion.toString()).isSnapshot()) {
iterator.remove();
}
}
//TODO proper sorting based on Version
Collections.sort(availableVersions);
Collections.reverse(availableVersions);
return availableVersions;
}
use of org.apache.maven.artifact.versioning.ArtifactVersion in project sling by apache.
the class DisplayBundleUpdatesMojo method logUpdates.
private void logUpdates(Map<Dependency, ArtifactVersions> updates) {
List<String> withUpdates = new ArrayList<String>();
List<String> usingCurrent = new ArrayList<String>();
for (ArtifactVersions versions : updates.values()) {
String left = " " + ArtifactUtils.versionlessKey(versions.getArtifact()) + " ";
final String current = versions.isCurrentVersionDefined() ? versions.getCurrentVersion().toString() : versions.getArtifact().getVersionRange().toString();
ArtifactVersion latest = versions.getNewestUpdate(UpdateScope.ANY, Boolean.TRUE.equals(allowSnapshots));
if (latest != null && !versions.isCurrentVersionDefined()) {
if (versions.getArtifact().getVersionRange().containsVersion(latest)) {
latest = null;
}
}
String right = " " + (latest == null ? current : current + " -> " + latest.toString());
List<String> t = latest == null ? usingCurrent : withUpdates;
if (right.length() + left.length() + 3 > INFO_PAD_SIZE) {
t.add(left + "...");
t.add(StringUtils.leftPad(right, INFO_PAD_SIZE));
} else {
t.add(StringUtils.rightPad(left, INFO_PAD_SIZE - right.length(), ".") + right);
}
}
if (usingCurrent.isEmpty() && !withUpdates.isEmpty()) {
getLog().info("No bundles are using the newest version.");
getLog().info("");
} else if (!usingCurrent.isEmpty()) {
getLog().info("The following bundles are using the newest version:");
for (String str : usingCurrent) {
getLog().info(str);
}
getLog().info("");
}
if (withUpdates.isEmpty() && !usingCurrent.isEmpty()) {
getLog().info("No bundles have newer versions.");
getLog().info("");
} else if (!withUpdates.isEmpty()) {
getLog().info("The following bundles have newer versions:");
for (String str : withUpdates) {
getLog().info(str);
}
getLog().info("");
}
}
use of org.apache.maven.artifact.versioning.ArtifactVersion in project sling by apache.
the class BundleDeployMojo method fixBundleVersion.
@Override
protected File fixBundleVersion(File jarFile) throws MojoExecutionException {
// by the maven deploy plugin
if (this.project.getVersion().indexOf("SNAPSHOT") > 0) {
// create new version string by replacing all '-' with '.'
String newVersion = this.project.getArtifact().getVersion();
int firstPos = newVersion.indexOf('-') + 1;
int pos = 0;
while (pos != -1) {
pos = newVersion.indexOf('-');
if (pos != -1) {
newVersion = newVersion.substring(0, pos) + '.' + newVersion.substring(pos + 1);
}
}
// now remove all dots after the third one
pos = newVersion.indexOf('.', firstPos);
while (pos != -1) {
newVersion = newVersion.substring(0, pos) + newVersion.substring(pos + 1);
pos = newVersion.indexOf('.', pos + 1);
}
return changeVersion(jarFile, project.getVersion(), newVersion);
}
// if this is a final release append "final"
try {
final ArtifactVersion v = this.project.getArtifact().getSelectedVersion();
if (v.getBuildNumber() == 0 && v.getQualifier() == null) {
final String newVersion = this.project.getArtifact().getVersion() + ".FINAL";
return changeVersion(jarFile, project.getVersion(), newVersion);
}
} catch (OverConstrainedVersionException ocve) {
// we ignore this and don't append "final"!
}
// just return the file in case of some issues
return jarFile;
}
use of org.apache.maven.artifact.versioning.ArtifactVersion in project gradle by gradle.
the class DefaultPomDependenciesConverter method compareMavenVersionStrings.
private int compareMavenVersionStrings(String dependencyVersionString, String duplicateVersionString) {
String dependencyVersion = emptyToNull(dependencyVersionString);
String duplicateVersion = emptyToNull(duplicateVersionString);
if (dependencyVersion == null && duplicateVersion == null) {
return 0;
}
if (dependencyVersion == null) {
return -1;
}
if (duplicateVersion == null) {
return 1;
}
ArtifactVersion dependencyArtifactVersion = new DefaultArtifactVersion(dependencyVersion);
ArtifactVersion duplicateArtifactVersion = new DefaultArtifactVersion(duplicateVersion);
return dependencyArtifactVersion.compareTo(duplicateArtifactVersion);
}
Aggregations