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 | |
parent | d00ae12688534ae468c12410d97ea2722c046208 (diff) |
Check against most recently released version if no version specified.
Diffstat (limited to 'enforcer-rule')
3 files changed, 100 insertions, 26 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 diff --git a/enforcer-rule/src/site/apt/examples/checking-version.apt.vm b/enforcer-rule/src/site/apt/examples/checking-version.apt.vm index 5e92ceb..328abea 100644 --- a/enforcer-rule/src/site/apt/examples/checking-version.apt.vm +++ b/enforcer-rule/src/site/apt/examples/checking-version.apt.vm @@ -21,7 +21,60 @@ Checking a project's version against older releases * Basic example In order to check your project's version against an older release, you must add the enforcer rule as a dependency to - the maven-enforcer-plugin and then configure your the maven-enforcer-plugin to run the rule: + the maven-enforcer-plugin and then configure your the maven-enforcer-plugin to run the rule. + + By default current artifact will be checked against most recently released version (retrieved from either local or one of configured remote repositories). + +--- +<project> + ... + <build> + ... + <plugins> + ... + <plugin> + <artifactId>maven-enforcer-plugin</artifactId> + <version>1.0-beta-1</version> + ... + <dependencies> + ... + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>${project.artifactId}</artifactId> + <version>${project.version}</version> + </dependency> + ... + </dependencies> + ... + <executions> + .... + <execution> + <id>check-version</id> + <phase>verify</phase> + <goals> + <goal>enforce</goal> + </goals> + <configuration> + <rules> + <checkVersionRule implementation="org.semver.enforcer.CheckVersionRule" /> + </rules> + </configuration> + </execution> + ... + </executions> + ... + </plugin> + ... + </plugins> + ... + </build> + ... +</project> +--- + +* Checking against a specific released version + + You can check your project against a specific released version: --- <project> @@ -74,9 +127,6 @@ Checking a project's version against older releases </project> --- - Once you have configured your project with details of the previous version to check, maven-enforcer will be able to - throw a build error if current version is not backward compatible with previous one. - * Including classes By default all classes will be considered during checking process. You might want to only check a subset of classes/packages. This is achieved by specifying <<<includes>>>. @@ -114,7 +164,6 @@ Checking a project's version against older releases <rules> <checkVersionRule implementation="org.semver.enforcer.CheckVersionRule"> ... - <previousVersion>1.0.0</previousVersion> <includes> <include>org.project.MyClass</include> </includes> @@ -170,7 +219,6 @@ Checking a project's version against older releases <rules> <checkVersionRule implementation="org.semver.enforcer.CheckVersionRule"> ... - <previousVersion>1.0.0</previousVersion> <includes> <include>org.project.MyClass</include> <include>org.project.internal</include> @@ -229,7 +277,6 @@ Checking a project's version against older releases <rules> <checkVersionRule implementation="org.semver.enforcer.CheckVersionRule"> ... - <previousVersion>1.0.0</previousVersion> <excludes> <exclude>org.project.MyClass</exclude> </excludes> diff --git a/enforcer-rule/src/site/apt/usage.apt.vm b/enforcer-rule/src/site/apt/usage.apt.vm index a3caafe..9a21ac0 100644 --- a/enforcer-rule/src/site/apt/usage.apt.vm +++ b/enforcer-rule/src/site/apt/usage.apt.vm @@ -58,11 +58,7 @@ Usage </goals> <configuration> <rules> - <checkVersionRule implementation="org.semver.enforcer.CheckVersionRule"> - ... - <previousVersion>1.0.0</previousVersion> - ... - </checkVersionRule> + <checkVersionRule implementation="org.semver.enforcer.CheckVersionRule" /> </rules> </configuration> </execution> @@ -78,7 +74,6 @@ Usage </project> --- - Once you have configured your project with details of the previous version to check, maven-enforcer will be able to - throw a build error if current version is not backward compatible with previous one. + Once you have configured your project, maven-enforcer will be able to throw a build error if current version is not backward compatible with last released one. Some more detailed {{{./examples/checking-version.html}examples}} of the <<<checkVersionRule>>> rule. |