diff options
author | Julien Eluard <[email protected]> | 2010-11-14 22:11:49 +0100 |
---|---|---|
committer | Julien Eluard <[email protected]> | 2010-11-14 22:11:49 +0100 |
commit | a06cd8f04ce4fed80d9fa1197c222bafb4624e41 (patch) | |
tree | 851bb643bd5d7639d005a0bf96633b7175e8c43f /enforcer-rule/src |
first commit
Diffstat (limited to 'enforcer-rule/src')
-rw-r--r-- | enforcer-rule/src/main/java/org/semver/enforcer/CheckVersionRule.java | 131 | ||||
-rw-r--r-- | enforcer-rule/src/site/apt/examples/checking-version.apt.vm | 251 | ||||
-rw-r--r-- | enforcer-rule/src/site/apt/index.apt | 37 | ||||
-rw-r--r-- | enforcer-rule/src/site/apt/usage.apt.vm | 84 | ||||
-rw-r--r-- | enforcer-rule/src/site/site.xml | 34 |
5 files changed, 537 insertions, 0 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 new file mode 100644 index 0000000..dab4ec3 --- /dev/null +++ b/enforcer-rule/src/main/java/org/semver/enforcer/CheckVersionRule.java @@ -0,0 +1,131 @@ +/** + * This software is licensed under the Apache 2 license, quoted below. + * + * Copyright 2010 Julien Eluard + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * [http://www.apache.org/licenses/LICENSE-2.0] + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package org.semver.enforcer; + +import java.io.File; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.factory.ArtifactFactory; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.resolver.ArtifactResolver; + +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.semver.Checker; +import org.semver.Version; + +/** + * + * Checks {@link Version} for current {@link Artifact} compared to a previous {@link Artifact}. + * <br /> + * Fails if {@link Version} semantic is not respected. + * + */ +public class CheckVersionRule implements EnforcerRule { + + /** + * Version number of artifact to be checked. + * + * @parameter + */ + protected String previousVersion; + + /** + * Class names to be included. + * + * @parameter + */ + protected String[] includes; + + /** + * Class names to be excluded. + * + * @parameter + */ + protected String[] excludes; + + protected final Set<String> extractFilters(final String[] filtersAsStringArray) { + if (filtersAsStringArray == null) { + return Collections.emptySet(); + } + return new HashSet<String>(Arrays.asList(filtersAsStringArray)); + } + + @Override + public void execute(final EnforcerRuleHelper helper) throws EnforcerRuleException { + try { + final MavenProject project = (MavenProject) helper.evaluate("${project}"); + final ArtifactFactory artifactFactory = (ArtifactFactory) helper.getComponent(ArtifactFactory.class); + final Artifact previousArtifact = artifactFactory.createArtifact(project.getGroupId(), project.getArtifactId(), this.previousVersion, null, "jar"); + final ArtifactResolver resolver = (ArtifactResolver) helper.getComponent(ArtifactResolver.class ); + final ArtifactRepository localRepository = (ArtifactRepository) helper.evaluate("${localRepository}"); + resolver.resolve(previousArtifact, project.getRemoteArtifactRepositories(), localRepository); + final Artifact currentArtifact = project.getArtifact(); + + validateArtifact(previousArtifact); + validateArtifact(currentArtifact); + + final Version previous = Version.parse(previousArtifact.getVersion()); + final File previousJar = previousArtifact.getFile(); + final Version current = Version.parse(currentArtifact.getVersion()); + final File currentJar = currentArtifact.getFile(); + final Checker.CompatibilityType compatibilityType = new Checker().check(previousJar, currentJar, extractFilters(this.includes), extractFilters(this.excludes)); + final boolean compatible = Checker.isTypeCompatible(compatibilityType, previous.delta(current)); + if (!compatible) { + throw new EnforcerRuleException("Current codebase incompatible with version <"+current+">. Version should be at least <"+Checker.inferNextVersion(previous, compatibilityType)+">."); + } + } catch (Exception e) { + throw new EnforcerRuleException("Exception while executing rule", e); + } + } + + /** + * Validates that specified {@link Artifact} is a JAR file. + * @param artifact + */ + protected final 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 + public boolean isCacheable() { + return false; + } + + @Override + public boolean isResultValid(final EnforcerRule cachedRule) { + return false; + } + + @Override + public String getCacheId() { + return "0"; + } + +} diff --git a/enforcer-rule/src/site/apt/examples/checking-version.apt.vm b/enforcer-rule/src/site/apt/examples/checking-version.apt.vm new file mode 100644 index 0000000..f86c745 --- /dev/null +++ b/enforcer-rule/src/site/apt/examples/checking-version.apt.vm @@ -0,0 +1,251 @@ +#* + * This software is licensed under the Apache 2 license, quoted below. + * + * Copyright 2010 Julien Eluard + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * [http://www.apache.org/licenses/LICENSE-2.0] + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + *# + +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: + +--- +<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"> + ... + <previousVersion>1.0.0</previousVersion> + ... + </checkVersionRule> + </rules> + </configuration> + </execution> + ... + </executions> + ... + </plugin> + ... + </plugins> + ... + </build> + ... +</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>>>. + +--- +<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"> + ... + <previousVersion>1.0.0</previousVersion> + <includes> + <include>org.project.MyClass</include> + </includes> + ... + </checkVersionRule> + </rules> + </configuration> + </execution> + ... + </executions> + ... + </plugin> + ... + </plugins> + ... + </build> + ... +</project> +--- + + We can specify multiple <includes> classes or packages. For example, to include <<<org.project.MyClass>>> class and <<<org.project.internal>>> package you would use a configuration like: + +--- +<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"> + ... + <previousVersion>1.0.0</previousVersion> + <includes> + <include>org.project.MyClass</include> + <include>org.project.internal</include> + </includes> + ... + </checkVersionRule> + </rules> + </configuration> + </execution> + ... + </executions> + ... + </plugin> + ... + </plugins> + ... + </build> + ... +</project> +--- + +* Excluding classes + + You may also want to exclude some classes/packages. For instance internal code might evolve independently from published API. This is achieved by specifying <<<excludes>>>. + +--- +<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"> + ... + <previousVersion>1.0.0</previousVersion> + <excludes> + <exclude>org.project.MyClass</exclude> + </excludes> + ... + </checkVersionRule> + </rules> + </configuration> + </execution> + ... + </executions> + ... + </plugin> + ... + </plugins> + ... + </build> + ... +</project> +---
\ No newline at end of file diff --git a/enforcer-rule/src/site/apt/index.apt b/enforcer-rule/src/site/apt/index.apt new file mode 100644 index 0000000..ff30f76 --- /dev/null +++ b/enforcer-rule/src/site/apt/index.apt @@ -0,0 +1,37 @@ +~~ +~~ This software is licensed under the Apache 2 license, quoted below. +~~ +~~ Copyright 2010 Julien Eluard +~~ +~~ Licensed under the Apache License, Version 2.0 (the "License"); you may not +~~ use this file except in compliance with the License. You may obtain a copy of +~~ the License at +~~ +~~ [http://www.apache.org/licenses/LICENSE-2.0] +~~ +~~ Unless required by applicable law or agreed to in writing, software +~~ distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +~~ WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +~~ License for the specific language governing permissions and limitations under +~~ the License. +~~ + +Semantic Versioning Enforcer Rule + + The Semantic Versioning Enforcer Rule is used to check your classes against previously released classes. + Depending on changes your version number will be valid or not according to {{{http://semver.org}semantic versioning}} rules. + +* Rules Overview + + The Semantic Versioning Enforcer Rule has the following rule. + + * <<<checkRule>>> checks a project version number against an older release. + +* Examples + + To provide you with better understanding of some usages of Semantic Versioning Enforcer Rule, + you can take a look into the following example: + + * {{{examples/checking-version.html}Checking a project's version against an older release}} + + []
\ No newline at end of file diff --git a/enforcer-rule/src/site/apt/usage.apt.vm b/enforcer-rule/src/site/apt/usage.apt.vm new file mode 100644 index 0000000..b1491a9 --- /dev/null +++ b/enforcer-rule/src/site/apt/usage.apt.vm @@ -0,0 +1,84 @@ +#* + * This software is licensed under the Apache 2 license, quoted below. + * + * Copyright 2010 Julien Eluard + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * [http://www.apache.org/licenses/LICENSE-2.0] + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + *# + +Usage + + The enforcer rule offers a rule for checking project's version against a previously released artifact. + +* Basic Usage + +** Checking a project's version + + In order to check your project's version, you must add the enforcer rule as a dependency to + the maven-enforcer-plugin and then configure the maven-enforcer-plugin to run the rule: + +--- +<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</id> + <phase>verify</phase> + <goals> + <goal>enforce</goal> + </goals> + <configuration> + <rules> + <checkVersionRule implementation="org.semver.enforcer.CheckVersionnRule"> + ... + <previousVersion>1.0.0</previousVersion> + ... + </checkVersionrule> + </rules> + </configuration> + </execution> + ... + </executions> + ... + </plugin> + ... + </plugins> + ... + </build> + ... +</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. + + Some more detailed {{{examples/checking-version.html}examples}} of the <<<checkVersionRule>>> rule.
\ No newline at end of file diff --git a/enforcer-rule/src/site/site.xml b/enforcer-rule/src/site/site.xml new file mode 100644 index 0000000..6320bdf --- /dev/null +++ b/enforcer-rule/src/site/site.xml @@ -0,0 +1,34 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + This software is licensed under the Apache 2 license, quoted below. + + Copyright 2010 Julien Eluard + + Licensed under the Apache License, Version 2.0 (the "License"); you may not + use this file except in compliance with the License. You may obtain a copy of + the License at + + [http://www.apache.org/licenses/LICENSE-2.0] + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + License for the specific language governing permissions and limitations under + the License. + +--> + +<project> + <body> + <menu ref="parent"/> + <menu name="Overview"> + <item name="Introduction" href="index.html" /> + <item name="Usage" href="usage.html" /> + </menu> + <menu name="Examples"> + <item name="Checking a project version number" href="examples/checking-version.html" /> + </menu> + <menu ref="reports" /> + </body> +</project> |