View Javadoc
1   package org.apache.maven.plugins.enforcer;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.HashSet;
25  import java.util.LinkedHashMap;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Set;
29  import java.util.regex.Pattern;
30  
31  import org.apache.maven.artifact.Artifact;
32  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
33  import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
34  import org.apache.maven.project.MavenProject;
35  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
36  
37  /**
38   * @author Robert Scholte
39   * @since 1.3
40   */
41  public class RequireSameVersions
42      extends AbstractNonCacheableEnforcerRule
43  {
44      private boolean uniqueVersions;
45  
46      private Set<String> dependencies = new HashSet<String>();
47  
48      private Set<String> plugins = new HashSet<String>();
49  
50      private Set<String> buildPlugins = new HashSet<String>();
51  
52      private Set<String> reportPlugins = new HashSet<String>();
53  
54      @Override
55      public void execute( EnforcerRuleHelper helper )
56          throws EnforcerRuleException
57      {
58          // get the project
59          MavenProject project = null;
60          try
61          {
62              project = (MavenProject) helper.evaluate( "${project}" );
63          }
64          catch ( ExpressionEvaluationException eee )
65          {
66              throw new EnforcerRuleException( "Unable to retrieve the MavenProject: ", eee );
67          }
68  
69          // consider including profile based artifacts
70          Map<String, List<String>> versionMembers = new LinkedHashMap<String, List<String>>();
71  
72          Set<String> buildPluginSet = new HashSet<String>( buildPlugins );
73          buildPluginSet.addAll( plugins );
74          Set<String> reportPluginSet = new HashSet<String>( reportPlugins );
75          reportPluginSet.addAll( plugins );
76  
77          // CHECKSTYLE_OFF: LineLength
78          versionMembers.putAll( collectVersionMembers( project.getArtifacts(), dependencies, " (dependency)" ) );
79          versionMembers.putAll( collectVersionMembers( project.getPluginArtifacts(), buildPlugins, " (buildPlugin)" ) );
80          versionMembers.putAll( collectVersionMembers( project.getReportArtifacts(), reportPlugins, " (reportPlugin)" ) );
81          // CHECKSTYLE_ON: LineLength
82  
83          if ( versionMembers.size() > 1 )
84          {
85              StringBuilder builder = new StringBuilder( "Found entries with different versions\n" );
86              for ( Map.Entry<String, List<String>> entry : versionMembers.entrySet() )
87              {
88                  builder.append( "Entries with version " ).append( entry.getKey() ).append( '\n' );
89                  for ( String conflictId : entry.getValue() )
90                  {
91                      builder.append( "- " ).append( conflictId ).append( '\n' );
92                  }
93              }
94              throw new EnforcerRuleException( builder.toString() );
95          }
96      }
97  
98      private Map<String, List<String>> collectVersionMembers( Set<Artifact> artifacts, Collection<String> patterns,
99                                                               String source )
100     {
101         Map<String, List<String>> versionMembers = new LinkedHashMap<String, List<String>>();
102 
103         List<Pattern> regExs = new ArrayList<Pattern>();
104         for ( String pattern : patterns )
105         {
106             String regex = pattern.replace( ".", "\\." ).replace( "*", ".*" ).replace( ":", "\\:" ).replace( '?', '.' );
107 
108             // pattern is groupId[:artifactId[:type[:classifier]]]
109             regExs.add( Pattern.compile( regex + "(\\:.+)?" ) );
110         }
111 
112         for ( Artifact artifact : artifacts )
113         {
114             for ( Pattern regEx : regExs )
115             {
116                 if ( regEx.matcher( artifact.getDependencyConflictId() ).matches() )
117                 {
118                     String version = uniqueVersions ? artifact.getVersion() : artifact.getBaseVersion();
119                     if ( !versionMembers.containsKey( version ) )
120                     {
121                         versionMembers.put( version, new ArrayList<String>() );
122                     }
123                     versionMembers.get( version ).add( artifact.getDependencyConflictId() + source );
124                 }
125             }
126         }
127         return versionMembers;
128     }
129 
130 }