Search in sources :

Example 1 with ArtifactVersion

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);
}
Also used : DefaultArtifactVersion(org.apache.maven.artifact.versioning.DefaultArtifactVersion) ArtifactVersion(org.apache.maven.artifact.versioning.ArtifactVersion) ArtifactFactory(org.apache.maven.artifact.factory.ArtifactFactory) MavenProject(org.apache.maven.project.MavenProject) DefaultArtifactVersion(org.apache.maven.artifact.versioning.DefaultArtifactVersion) Version(org.semver.Version) ArtifactVersion(org.apache.maven.artifact.versioning.ArtifactVersion) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) ArtifactMetadataSource(org.apache.maven.artifact.metadata.ArtifactMetadataSource) File(java.io.File) Artifact(org.apache.maven.artifact.Artifact) ArtifactResolver(org.apache.maven.artifact.resolver.ArtifactResolver) ExpressionEvaluationException(org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException) IOException(java.io.IOException) EnforcerRuleException(org.apache.maven.enforcer.rule.api.EnforcerRuleException) ArtifactMetadataRetrievalException(org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException)

Example 2 with ArtifactVersion

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;
}
Also used : DefaultArtifactVersion(org.apache.maven.artifact.versioning.DefaultArtifactVersion) ArtifactVersion(org.apache.maven.artifact.versioning.ArtifactVersion) DefaultArtifactVersion(org.apache.maven.artifact.versioning.DefaultArtifactVersion)

Example 3 with ArtifactVersion

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("");
    }
}
Also used : ArtifactVersions(org.codehaus.mojo.versions.api.ArtifactVersions) ArtifactVersion(org.apache.maven.artifact.versioning.ArtifactVersion) ArrayList(java.util.ArrayList)

Example 4 with ArtifactVersion

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;
}
Also used : ArtifactVersion(org.apache.maven.artifact.versioning.ArtifactVersion) OverConstrainedVersionException(org.apache.maven.artifact.versioning.OverConstrainedVersionException)

Example 5 with ArtifactVersion

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);
}
Also used : DefaultArtifactVersion(org.apache.maven.artifact.versioning.DefaultArtifactVersion) ArtifactVersion(org.apache.maven.artifact.versioning.ArtifactVersion) DefaultArtifactVersion(org.apache.maven.artifact.versioning.DefaultArtifactVersion)

Aggregations

ArtifactVersion (org.apache.maven.artifact.versioning.ArtifactVersion)9 Artifact (org.apache.maven.artifact.Artifact)5 DefaultArtifactVersion (org.apache.maven.artifact.versioning.DefaultArtifactVersion)4 ArtifactMetadataRetrievalException (org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException)3 File (java.io.File)2 ArtifactMetadataSource (org.apache.maven.artifact.metadata.ArtifactMetadataSource)2 InvalidVersionSpecificationException (org.apache.maven.artifact.versioning.InvalidVersionSpecificationException)2 VersionRange (org.apache.maven.artifact.versioning.VersionRange)2 MavenProject (org.apache.maven.project.MavenProject)2 IOException (java.io.IOException)1 RemoteException (java.rmi.RemoteException)1 ArrayList (java.util.ArrayList)1 DefaultArtifact (org.apache.maven.artifact.DefaultArtifact)1 ArtifactFactory (org.apache.maven.artifact.factory.ArtifactFactory)1 DefaultArtifactHandler (org.apache.maven.artifact.handler.DefaultArtifactHandler)1 ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)1 ArtifactNotFoundException (org.apache.maven.artifact.resolver.ArtifactNotFoundException)1 ArtifactResolutionException (org.apache.maven.artifact.resolver.ArtifactResolutionException)1 ArtifactResolver (org.apache.maven.artifact.resolver.ArtifactResolver)1 OverConstrainedVersionException (org.apache.maven.artifact.versioning.OverConstrainedVersionException)1