Author: rfscholte
Date: Thu Jun 13 21:45:07 2013
New Revision: 1492881

URL: http://svn.apache.org/r1492881
Log:
[MENFORCER-147] Add RequireSameVersions

Added:
    
maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireSameVersions.java
    maven/enforcer/trunk/enforcer-rules/src/site/apt/requireSameVersions.apt.vm
    
maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-same-versions_failure/
    
maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-same-versions_failure/invoker.properties
    
maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-same-versions_failure/pom.xml
    
maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-same-versions_success/
    
maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-same-versions_success/pom.xml
Modified:
    maven/enforcer/trunk/enforcer-rules/src/site/apt/index.apt

Added: 
maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireSameVersions.java
URL: 
http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireSameVersions.java?rev=1492881&view=auto
==============================================================================
--- 
maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireSameVersions.java
 (added)
+++ 
maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireSameVersions.java
 Thu Jun 13 21:45:07 2013
@@ -0,0 +1,128 @@
+package org.apache.maven.plugins.enforcer;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Pattern;
+
+import org.apache.maven.artifact.Artifact;
+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;
+
+/**
+ * 
+ * @author Robert Scholte
+ * @since 1.3
+ */
+public class RequireSameVersions
+    extends AbstractNonCacheableEnforcerRule
+{
+    private boolean uniqueVersions;
+
+    private Set<String> dependencies = new HashSet<String>();
+
+    private Set<String> plugins = new HashSet<String>();
+
+    private Set<String> buildPlugins = new HashSet<String>();
+
+    private Set<String> reportPlugins = new HashSet<String>();
+
+    public void execute( EnforcerRuleHelper helper )
+        throws EnforcerRuleException
+    {
+        // get the project
+        MavenProject project = null;
+        try
+        {
+            project = (MavenProject) helper.evaluate( "${project}" );
+        }
+        catch ( ExpressionEvaluationException eee )
+        {
+            throw new EnforcerRuleException( "Unable to retrieve the 
MavenProject: ", eee );
+        }
+
+        // consider including profile based artifacts
+        Map<String, List<String>> versionMembers = new LinkedHashMap<String, 
List<String>>();
+
+        Set<String> buildPluginSet = new HashSet<String>( buildPlugins );
+        buildPluginSet.addAll( plugins );
+        Set<String> reportPluginSet = new HashSet<String>( reportPlugins );
+        reportPluginSet.addAll( plugins );
+
+        versionMembers.putAll( collectVersionMembers( project.getArtifacts(), 
dependencies, " (dependency)" ) );
+        versionMembers.putAll( collectVersionMembers( 
project.getPluginArtifacts(), buildPlugins, " (buildPlugin)" ) );
+        versionMembers.putAll( collectVersionMembers( 
project.getReportArtifacts(), reportPlugins, " (reportPlugin)" ) );
+
+        if ( versionMembers.size() > 1 )
+        {
+            StringBuilder builder = new StringBuilder( "Found entries with 
different versions\n" );
+            for ( Map.Entry<String, List<String>> entry : 
versionMembers.entrySet() )
+            {
+                builder.append( "Entries with version " ).append( 
entry.getKey() ).append( '\n' );
+                for ( String conflictId : entry.getValue() )
+                {
+                    builder.append( "- " ).append( conflictId ).append( '\n' );
+                }
+            }
+            throw new EnforcerRuleException( builder.toString() );
+        }
+    }
+
+    private Map<String, List<String>> collectVersionMembers( Set<Artifact> 
artifacts, Collection<String> patterns,
+                                                             String source )
+    {
+        Map<String, List<String>> versionMembers = new LinkedHashMap<String, 
List<String>>();
+        
+        List<Pattern> regExs = new ArrayList<Pattern>();
+        for( String pattern : patterns )
+        {
+            String regex = pattern.replace( ".", "\\." ).replace( "*", ".*" 
).replace( ":", "\\:" ).replace( '?', '.' );
+
+            // pattern is groupId[:artifactId[:type[:classifier]]]
+            regExs.add( Pattern.compile( regex  + "(\\:.+)?" ) );
+        }
+        
+        for ( Artifact artifact : artifacts )
+        {
+            for ( Pattern regEx: regExs )
+            {
+                if ( regEx.matcher( artifact.getDependencyConflictId() 
).matches() )
+                {
+                    String version = uniqueVersions ? artifact.getVersion() : 
artifact.getBaseVersion();
+                    if ( !versionMembers.containsKey( version ) )
+                    {
+                        versionMembers.put( version, new ArrayList<String>() );
+                    }
+                    versionMembers.get( version ).add( 
artifact.getDependencyConflictId() + source );
+                }
+            }
+        }
+        return versionMembers;
+    }
+
+}
\ No newline at end of file

Modified: maven/enforcer/trunk/enforcer-rules/src/site/apt/index.apt
URL: 
http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/site/apt/index.apt?rev=1492881&r1=1492880&r2=1492881&view=diff
==============================================================================
--- maven/enforcer/trunk/enforcer-rules/src/site/apt/index.apt (original)
+++ maven/enforcer/trunk/enforcer-rules/src/site/apt/index.apt Thu Jun 13 
21:45:07 2013
@@ -67,6 +67,8 @@ Standard Rules
 
   * {{{./requireReleaseVersion.html}requireReleaseVersion}} - enforces that 
the artifact is not a snapshot.
 
+  * {{{./requireSameVersions.html}requireSameVersions}} - enforces that 
specific dependencies and/or plugins have the same version.
+
   * {{{./requireUpperBoundDeps.html}requireUpperBoundDeps}} - ensures that 
every (transitive) dependency is resolved to it's specified version or higher.
 
   []

Added: 
maven/enforcer/trunk/enforcer-rules/src/site/apt/requireSameVersions.apt.vm
URL: 
http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/site/apt/requireSameVersions.apt.vm?rev=1492881&view=auto
==============================================================================
--- maven/enforcer/trunk/enforcer-rules/src/site/apt/requireSameVersions.apt.vm 
(added)
+++ maven/enforcer/trunk/enforcer-rules/src/site/apt/requireSameVersions.apt.vm 
Thu Jun 13 21:45:07 2013
@@ -0,0 +1,86 @@
+ ~~ Licensed to the Apache Software Foundation (ASF) under one
+ ~~ or more contributor license agreements.  See the NOTICE file
+ ~~ distributed with this work for additional information
+ ~~ regarding copyright ownership.  The ASF licenses this file
+ ~~ to you 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.
+
+ -----
+ Require Same Version
+ -----
+ Robert Scholte
+ -----
+ 2013-06-13
+ -----
+ 
+ Require Same Versions
+
+  This rule enforces that specific dependencies and/or plugins have the same 
version.
+
+
+   The following parameters are supported by this rule:
+   
+   * dependencies - an optional list of dependency patterns
+   
+   * buildPlugins - an optional list of build plugin patterns
+   
+   * reportPlugins - an optional list of report plugin patterns
+   
+   * plugins - an optional list of both build and report plugin patterns
+    
+   * uniqueVersions - if SNAPSHOTs should be compared by their timestamped 
version or not. Default: false 
+
+   []
+
+   
+  Sample Plugin Configuration:
+  
++---+
+<project>
+  [...]
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-enforcer-plugin</artifactId>
+        <version>${project.version}</version>
+        <executions>
+          <execution>
+            <id>enforce-no-snapshots</id>
+            <goals>
+              <goal>enforce</goal>
+            </goals>
+            <configuration>
+              <rules>
+                <requireSameVersions>
+                  <plugins>
+                    
<plugin>org.apache.maven.plugins:maven-surefire-plugin</plugin>
+                    
<plugin>org.apache.maven.plugins:maven-failsafe-plugin</plugin>
+                    
<plugin>org.apache.maven.plugins:maven-surefire-report-plugin</plugin>
+                  </plugins>
+                </requireSameVersions>
+                <requireSameVersions>
+                  <dependencies>
+                    <dependency>org.apache.maven</dependency> <!-- or 
org.apache.maven:* -->
+                  </dependencies>
+                </requireSameVersions>
+              </rules>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+  [...]
+</project>
++---+
\ No newline at end of file

Added: 
maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-same-versions_failure/invoker.properties
URL: 
http://svn.apache.org/viewvc/maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-same-versions_failure/invoker.properties?rev=1492881&view=auto
==============================================================================
--- 
maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-same-versions_failure/invoker.properties
 (added)
+++ 
maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-same-versions_failure/invoker.properties
 Thu Jun 13 21:45:07 2013
@@ -0,0 +1 @@
+invoker.buildResult=failure
\ No newline at end of file

Added: 
maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-same-versions_failure/pom.xml
URL: 
http://svn.apache.org/viewvc/maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-same-versions_failure/pom.xml?rev=1492881&view=auto
==============================================================================
--- 
maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-same-versions_failure/pom.xml
 (added)
+++ 
maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-same-versions_failure/pom.xml
 Thu Jun 13 21:45:07 2013
@@ -0,0 +1,68 @@
+<?xml version="1.0" encoding="UTF-8"?><project>
+  
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.apache.maven.enforcer.its</groupId>
+  <artifactId>requireSameVersion</artifactId>
+  <version>1.0-SNAPSHOT</version>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-enforcer-plugin</artifactId>
+        <version>@project.version@</version>
+        <executions>
+          <execution>
+            <id>enforce</id>
+            <goals>
+              <goal>enforce</goal>
+            </goals>
+            <configuration>
+              <rules>
+                <requireSameVersions>
+                  <buildPlugins>
+                    
<buildPlugin>org.apache.maven.plugins:maven-surefire-plugin</buildPlugin>
+                    
<buildPlugin>org.apache.maven.plugins:maven-failsafe-plugin</buildPlugin>
+                  </buildPlugins>
+                  <reportPlugins>
+                    
<reportPlugin>org.apache.maven.plugins:maven-surefire-report-plugin</reportPlugin>
+                  </reportPlugins>
+                </requireSameVersions>
+                <!-- almost the rule configuration -->
+                <requireSameVersions>
+                  <plugins>
+                    
<plugin>org.apache.maven.plugins:maven-surefire-plugin</plugin>
+                    
<plugin>org.apache.maven.plugins:maven-failsafe-plugin</plugin>
+                    
<plugin>org.apache.maven.plugins:maven-surefire-report-plugin</plugin>
+                  </plugins>
+                </requireSameVersions>
+              </rules>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-surefire-plugin</artifactId>
+        <version>2.14</version>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-failsafe-plugin</artifactId>
+        <version>2.15</version>
+      </plugin>
+    </plugins>
+  </build>
+  
+  <reporting>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-surefire-report-plugin</artifactId>
+        <version>2.13</version>
+      </plugin>
+    </plugins>
+  </reporting>
+  
+
+ </project>
\ No newline at end of file

Added: 
maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-same-versions_success/pom.xml
URL: 
http://svn.apache.org/viewvc/maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-same-versions_success/pom.xml?rev=1492881&view=auto
==============================================================================
--- 
maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-same-versions_success/pom.xml
 (added)
+++ 
maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-same-versions_success/pom.xml
 Thu Jun 13 21:45:07 2013
@@ -0,0 +1,69 @@
+<?xml version="1.0" encoding="UTF-8"?><project>
+  
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.apache.maven.enforcer.its</groupId>
+  <artifactId>requireSameVersion</artifactId>
+  <packaging>jar</packaging>
+  <version>1.0-SNAPSHOT</version>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-enforcer-plugin</artifactId>
+        <version>@project.version@</version>
+        <executions>
+          <execution>
+            <id>enforce</id>
+            <goals>
+              <goal>enforce</goal>
+            </goals>
+            <configuration>
+              <rules>
+                <requireSameVersions>
+                  <buildPlugins>
+                    
<buildPlugin>org.apache.maven.plugins:maven-surefire-plugin</buildPlugin>
+                    
<buildPlugin>org.apache.maven.plugins:maven-failsafe-plugin</buildPlugin>
+                  </buildPlugins>
+                  <reportPlugins>
+                    
<reportPlugin>org.apache.maven.plugins:maven-surefire-report-plugin</reportPlugin>
+                  </reportPlugins>
+                </requireSameVersions>
+                <!-- almost the rule configuration -->
+                <requireSameVersions>
+                  <plugins>
+                    
<plugin>org.apache.maven.plugins:maven-surefire-plugin</plugin>
+                    
<plugin>org.apache.maven.plugins:maven-failsafe-plugin</plugin>
+                    
<plugin>org.apache.maven.plugins:maven-surefire-report-plugin</plugin>
+                  </plugins>
+                </requireSameVersions>
+              </rules>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-surefire-plugin</artifactId>
+        <version>2.14</version>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-failsafe-plugin</artifactId>
+        <version>2.14</version>
+      </plugin>
+    </plugins>
+  </build>
+  
+  <reporting>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-surefire-report-plugin</artifactId>
+        <version>2.14</version>
+      </plugin>
+    </plugins>
+  </reporting>
+  
+
+ </project>
\ No newline at end of file


Reply via email to