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