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.execution.MavenSession;
037import org.apache.maven.project.MavenProject;
038
039/**
040 * @author Robert Scholte
041 * @since 1.3
042 */
043@Named("requireSameVersions")
044public final class RequireSameVersions extends AbstractStandardEnforcerRule {
045    private boolean uniqueVersions;
046
047    private Set<String> dependencies = new HashSet<>();
048
049    private Set<String> plugins = new HashSet<>();
050
051    private Set<String> buildPlugins = new HashSet<>();
052
053    private Set<String> reportPlugins = new HashSet<>();
054
055    private boolean sameModuleVersions;
056
057    private final MavenProject project;
058
059    private final MavenSession session;
060
061    @Inject
062    public RequireSameVersions(MavenProject project, MavenSession session) {
063        this.project = Objects.requireNonNull(project);
064        this.session = Objects.requireNonNull(session);
065    }
066
067    @Override
068    public void execute() throws EnforcerRuleException {
069        // consider including profile based artifacts
070        Map<String, List<String>> versionMembers = new LinkedHashMap<>();
071
072        Set<String> allBuildPlugins = new HashSet<>(buildPlugins);
073        allBuildPlugins.addAll(plugins);
074        Set<String> allReportPlugins = new HashSet<>(reportPlugins);
075        allReportPlugins.addAll(plugins);
076        // CHECKSTYLE_OFF: LineLength
077        versionMembers.putAll(collectVersionMembers(project.getArtifacts(), dependencies, " (dependency)"));
078        versionMembers.putAll(collectVersionMembers(project.getPluginArtifacts(), allBuildPlugins, " (buildPlugin)"));
079        versionMembers.putAll(collectVersionMembers(project.getReportArtifacts(), allReportPlugins, " (reportPlugin)"));
080        // CHECKSTYLE_ON: LineLength
081
082        if (versionMembers.size() > 1) {
083            StringBuilder builder = new StringBuilder("Found entries with different versions" + System.lineSeparator());
084            for (Map.Entry<String, List<String>> entry : versionMembers.entrySet()) {
085                builder.append("Entries with version ").append(entry.getKey()).append(System.lineSeparator());
086                for (String conflictId : entry.getValue()) {
087                    builder.append("- ").append(conflictId).append(System.lineSeparator());
088                }
089            }
090            throw new EnforcerRuleException(builder.toString());
091        }
092
093        if (sameModuleVersions) {
094            MavenProject topLevelProject = session.getTopLevelProject();
095            if (!Objects.equals(topLevelProject.getVersion(), project.getVersion())) {
096                throw new EnforcerRuleException("Top level project has version " + topLevelProject.getVersion()
097                        + " but current module has different version " + project.getVersion());
098            }
099        }
100    }
101
102    private Map<String, List<String>> collectVersionMembers(
103            Set<Artifact> artifacts, Collection<String> patterns, String source) {
104        Map<String, List<String>> versionMembers = new LinkedHashMap<>();
105
106        List<Pattern> regExs = new ArrayList<>();
107        for (String pattern : patterns) {
108            String regex = pattern.replace(".", "\\.")
109                    .replace("*", ".*")
110                    .replace(":", "\\:")
111                    .replace('?', '.');
112
113            // pattern is groupId[:artifactId[:type[:classifier]]]
114            regExs.add(Pattern.compile(regex + "(\\:.+)?"));
115        }
116
117        for (Artifact artifact : artifacts) {
118            for (Pattern regEx : regExs) {
119                if (regEx.matcher(artifact.getDependencyConflictId()).matches()) {
120                    String version = uniqueVersions ? artifact.getVersion() : artifact.getBaseVersion();
121                    versionMembers
122                            .computeIfAbsent(version, unused -> new ArrayList<>())
123                            .add(artifact.getDependencyConflictId() + source);
124                }
125            }
126        }
127        return versionMembers;
128    }
129
130    void addDependency(String dependency) {
131        dependencies.add(dependency);
132    }
133
134    void addPlugin(String plugin) {
135        plugins.add(plugin);
136    }
137
138    void addBuildPlugin(String buildPlugin) {
139        buildPlugins.add(buildPlugin);
140    }
141
142    void addReportPlugin(String reportPlugin) {
143        reportPlugins.add(reportPlugin);
144    }
145
146    @Override
147    public String toString() {
148        return String.format(
149                "RequireSameVersions[dependencies=%s, buildPlugins=%s, reportPlugins=%s, plugins=%s, uniqueVersions=%b, sameModuleVersions=%b]",
150                dependencies, buildPlugins, reportPlugins, plugins, uniqueVersions, sameModuleVersions);
151    }
152}