001package org.apache.maven.plugins.enforcer; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.util.ArrayList; 023import java.util.Collection; 024import java.util.HashSet; 025import java.util.LinkedHashMap; 026import java.util.List; 027import java.util.Map; 028import java.util.Set; 029import java.util.regex.Pattern; 030 031import org.apache.maven.artifact.Artifact; 032import org.apache.maven.enforcer.rule.api.EnforcerRuleException; 033import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; 034import org.apache.maven.project.MavenProject; 035import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; 036 037/** 038 * @author Robert Scholte 039 * @since 1.3 040 */ 041public class RequireSameVersions 042 extends AbstractNonCacheableEnforcerRule 043{ 044 private boolean uniqueVersions; 045 046 private Set<String> dependencies = new HashSet<String>(); 047 048 private Set<String> plugins = new HashSet<String>(); 049 050 private Set<String> buildPlugins = new HashSet<String>(); 051 052 private Set<String> reportPlugins = new HashSet<String>(); 053 054 public void execute( EnforcerRuleHelper helper ) 055 throws EnforcerRuleException 056 { 057 // get the project 058 MavenProject project = null; 059 try 060 { 061 project = (MavenProject) helper.evaluate( "${project}" ); 062 } 063 catch ( ExpressionEvaluationException eee ) 064 { 065 throw new EnforcerRuleException( "Unable to retrieve the MavenProject: ", eee ); 066 } 067 068 // consider including profile based artifacts 069 Map<String, List<String>> versionMembers = new LinkedHashMap<String, List<String>>(); 070 071 Set<String> buildPluginSet = new HashSet<String>( buildPlugins ); 072 buildPluginSet.addAll( plugins ); 073 Set<String> reportPluginSet = new HashSet<String>( reportPlugins ); 074 reportPluginSet.addAll( plugins ); 075 076 // CHECKSTYLE_OFF: LineLength 077 versionMembers.putAll( collectVersionMembers( project.getArtifacts(), dependencies, " (dependency)" ) ); 078 versionMembers.putAll( collectVersionMembers( project.getPluginArtifacts(), buildPlugins, " (buildPlugin)" ) ); 079 versionMembers.putAll( collectVersionMembers( project.getReportArtifacts(), reportPlugins, " (reportPlugin)" ) ); 080 // CHECKSTYLE_ON: LineLength 081 082 if ( versionMembers.size() > 1 ) 083 { 084 StringBuilder builder = new StringBuilder( "Found entries with different versions\n" ); 085 for ( Map.Entry<String, List<String>> entry : versionMembers.entrySet() ) 086 { 087 builder.append( "Entries with version " ).append( entry.getKey() ).append( '\n' ); 088 for ( String conflictId : entry.getValue() ) 089 { 090 builder.append( "- " ).append( conflictId ).append( '\n' ); 091 } 092 } 093 throw new EnforcerRuleException( builder.toString() ); 094 } 095 } 096 097 private Map<String, List<String>> collectVersionMembers( Set<Artifact> artifacts, Collection<String> patterns, 098 String source ) 099 { 100 Map<String, List<String>> versionMembers = new LinkedHashMap<String, List<String>>(); 101 102 List<Pattern> regExs = new ArrayList<Pattern>(); 103 for ( String pattern : patterns ) 104 { 105 String regex = pattern.replace( ".", "\\." ).replace( "*", ".*" ).replace( ":", "\\:" ).replace( '?', '.' ); 106 107 // pattern is groupId[:artifactId[:type[:classifier]]] 108 regExs.add( Pattern.compile( regex + "(\\:.+)?" ) ); 109 } 110 111 for ( Artifact artifact : artifacts ) 112 { 113 for ( Pattern regEx : regExs ) 114 { 115 if ( regEx.matcher( artifact.getDependencyConflictId() ).matches() ) 116 { 117 String version = uniqueVersions ? artifact.getVersion() : artifact.getBaseVersion(); 118 if ( !versionMembers.containsKey( version ) ) 119 { 120 versionMembers.put( version, new ArrayList<String>() ); 121 } 122 versionMembers.get( version ).add( artifact.getDependencyConflictId() + source ); 123 } 124 } 125 } 126 return versionMembers; 127 } 128 129}