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.Objects;
025import java.util.regex.Pattern;
026
027import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
028import org.apache.maven.project.MavenProject;
029
030/**
031 * This rule checks that the Maven coordinates (i.e. the project's {@code groupId} and {@code artifactId}) each match a given pattern.
032 * @since 3.5.0
033 */
034@Named("requireMatchingCoordinates")
035public final class RequireMatchingCoordinates extends AbstractStandardEnforcerRule {
036
037    private Pattern groupIdPattern;
038
039    private Pattern artifactIdPattern;
040
041    private boolean moduleNameMustMatchArtifactId;
042
043    private final MavenProject project;
044
045    @Inject
046    public RequireMatchingCoordinates(MavenProject project) {
047        this.project = Objects.requireNonNull(project);
048    }
049
050    @Override
051    public void execute() throws EnforcerRuleException {
052        StringBuilder msgBuilder = new StringBuilder();
053        if (groupIdPattern != null
054                && !groupIdPattern.matcher(project.getGroupId()).matches()) {
055            msgBuilder
056                    .append("Group ID must match pattern \"")
057                    .append(groupIdPattern)
058                    .append("\" but is \"")
059                    .append(project.getGroupId())
060                    .append("\"");
061        }
062        if (artifactIdPattern != null
063                && !artifactIdPattern.matcher(project.getArtifactId()).matches()) {
064            if (msgBuilder.length() > 0) {
065                msgBuilder.append(System.lineSeparator());
066            }
067            msgBuilder
068                    .append("Artifact ID must match pattern \"")
069                    .append(artifactIdPattern)
070                    .append("\" but is \"")
071                    .append(project.getArtifactId())
072                    .append("\"");
073        }
074        if (moduleNameMustMatchArtifactId
075                && !project.isExecutionRoot()
076                && !project.getBasedir().getName().equals(project.getArtifactId())) {
077            if (msgBuilder.length() > 0) {
078                msgBuilder.append(System.lineSeparator());
079            }
080            msgBuilder
081                    .append("Module directory name must be equal to its artifact ID \"")
082                    .append(project.getArtifactId())
083                    .append("\" but is \"")
084                    .append(project.getBasedir().getName())
085                    .append("\"");
086        }
087        if (msgBuilder.length() > 0) {
088            throw new EnforcerRuleException(msgBuilder.toString());
089        }
090    }
091
092    public void setGroupIdPattern(String groupIdPattern) {
093        this.groupIdPattern = Pattern.compile(groupIdPattern);
094    }
095
096    public void setArtifactIdPattern(String artifactIdPattern) {
097        this.artifactIdPattern = Pattern.compile(artifactIdPattern);
098    }
099
100    public void setModuleNameMustMatchArtifactId(boolean moduleNameMustMatchArtifactId) {
101        this.moduleNameMustMatchArtifactId = moduleNameMustMatchArtifactId;
102    }
103
104    @Override
105    public String toString() {
106        return String.format(
107                "requireMatchingCoordinates[groupIdPattern=%s, artifactIdPattern=%s, moduleNameMustMatchArtifactId=%b]",
108                groupIdPattern, artifactIdPattern, moduleNameMustMatchArtifactId);
109    }
110}