diff options
author | Julien Eluard <[email protected]> | 2010-11-27 21:33:16 +0100 |
---|---|---|
committer | Julien Eluard <[email protected]> | 2010-11-27 21:33:16 +0100 |
commit | 1ec8c629d0fe27d4926f94b5608112b78cc9a492 (patch) | |
tree | 6a9cc3500a05df52df7669a03dc7a593959b1acb /enforcer-rule/src/main/java | |
parent | d00ae12688534ae468c12410d97ea2722c046208 (diff) |
Check against most recently released version if no version specified.
Diffstat (limited to 'enforcer-rule/src/main/java')
-rw-r--r-- | enforcer-rule/src/main/java/org/semver/enforcer/CheckVersionRule.java | 56 |
1 files changed, 44 insertions, 12 deletions
diff --git a/enforcer-rule/src/main/java/org/semver/enforcer/CheckVersionRule.java b/enforcer-rule/src/main/java/org/semver/enforcer/CheckVersionRule.java index df23250..5de3a32 100644 --- a/enforcer-rule/src/main/java/org/semver/enforcer/CheckVersionRule.java +++ b/enforcer-rule/src/main/java/org/semver/enforcer/CheckVersionRule.java @@ -23,18 +23,22 @@ import java.io.IOException; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; +import java.util.List; import java.util.Set; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.factory.ArtifactFactory; +import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException; +import org.apache.maven.artifact.metadata.ArtifactMetadataSource; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.resolver.ArtifactResolver; +import org.apache.maven.artifact.versioning.ArtifactVersion; +import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.apache.maven.enforcer.rule.api.EnforcerRule; import org.apache.maven.enforcer.rule.api.EnforcerRuleException; import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; -import org.codehaus.plexus.util.StringUtils; import org.semver.Comparer; import org.semver.Delta; import org.semver.Dumper; @@ -86,10 +90,6 @@ public final class CheckVersionRule implements EnforcerRule { @Override public void execute(final EnforcerRuleHelper helper) throws EnforcerRuleException { - if (StringUtils.isEmpty(this.previousVersion)) { - throw new EnforcerRuleException("previousVersion can't be empty"); - } - final MavenProject project; try { project = (MavenProject) helper.evaluate("${project}"); @@ -104,10 +104,29 @@ public final class CheckVersionRule implements EnforcerRule { final Artifact previousArtifact; final Artifact currentArtifact; 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 = getAvailableVersions(artifactMetadataSource, project, localRepository); + + if (availableVersions.isEmpty()) { + helper.getLog().info("No previously released version. Backward compatibility check not performed."); + + return; + } + + version = availableVersions.iterator().next().toString(); + + helper.getLog().info("Version deduced as <"+version+"> (among all availables: "+availableVersions+")"); + } final ArtifactFactory artifactFactory = (ArtifactFactory) helper.getComponent(ArtifactFactory.class); - previousArtifact = artifactFactory.createArtifact(project.getGroupId(), project.getArtifactId(), this.previousVersion, null, type); + previousArtifact = artifactFactory.createArtifact(project.getGroupId(), project.getArtifactId(), version, null, type); final ArtifactResolver resolver = (ArtifactResolver) helper.getComponent(ArtifactResolver.class ); - final ArtifactRepository localRepository = (ArtifactRepository) helper.evaluate("${localRepository}"); resolver.resolve(previousArtifact, project.getRemoteArtifactRepositories(), localRepository); currentArtifact = project.getArtifact(); @@ -121,9 +140,10 @@ public final class CheckVersionRule implements EnforcerRule { final File previousJar = previousArtifact.getFile(); final Version current = Version.parse(currentArtifact.getVersion()); final File currentJar = currentArtifact.getFile(); + helper.getLog().info("Using <"+previousJar+"> as previous JAR"); helper.getLog().info("Using <"+currentJar+"> as current JAR"); - + try { final Comparer comparer = new Comparer(previousJar, currentJar, extractFilters(this.includes), extractFilters(this.excludes)); final Delta delta = comparer.diff(); @@ -140,16 +160,28 @@ public final class CheckVersionRule implements EnforcerRule { } /** - * Validates that specified {@link Artifact} is a JAR file. + * @param artifactMetadataSource + * @param project + * @param localRepository + * @return all available versions from most recent to oldest + * @throws ArtifactMetadataRetrievalException + */ + protected final List<ArtifactVersion> getAvailableVersions(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())); + Collections.sort(availableVersions); + Collections.reverse(availableVersions); + return availableVersions; + } + + /** + * Validates that specified {@link Artifact} is a file. * @param artifact */ private void validateArtifact(final Artifact artifact) { if (!artifact.getFile().isFile()) { throw new IllegalArgumentException("<"+artifact.getFile()+"> is not a file"); } - if (!artifact.getType().equalsIgnoreCase("jar")) { - throw new IllegalArgumentException("<"+artifact.getFile()+"> is not a JAR"); - } } @Override |