You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by "ASF GitHub Bot (Jira)" <ji...@apache.org> on 2024/04/04 18:12:00 UTC

[jira] [Commented] (MENFORCER-500) New rule: Maven coordinates must match pattern

    [ https://issues.apache.org/jira/browse/MENFORCER-500?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17834043#comment-17834043 ] 

ASF GitHub Bot commented on MENFORCER-500:
------------------------------------------

slawekjaranowski commented on code in PR #309:
URL: https://github.com/apache/maven-enforcer/pull/309#discussion_r1552202688


##########
enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/RequireMatchingCoordinates.java:
##########
@@ -0,0 +1,110 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.enforcer.rules;
+
+import javax.inject.Inject;
+import javax.inject.Named;
+
+import java.util.Objects;
+import java.util.regex.Pattern;
+
+import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
+import org.apache.maven.project.MavenProject;
+
+/**
+ * This rule checks that the Maven coordinates (i.e. the project's {@code groupId} and {@code artifactId}) each match a given pattern.
+ * @since 3.5.0
+ */
+@Named("requireMatchingCoordinates")
+public final class RequireMatchingCoordinates extends AbstractStandardEnforcerRule {
+
+    private Pattern groupIdPattern;
+
+    private Pattern artifactIdPattern;
+
+    private boolean moduleNameMustMatchArtifactId;
+
+    private final MavenProject project;
+
+    @Inject
+    public RequireMatchingCoordinates(MavenProject project) {
+        this.project = Objects.requireNonNull(project);
+    }
+
+    @Override
+    public void execute() throws EnforcerRuleException {
+        StringBuilder msgBuilder = new StringBuilder();
+        if (groupIdPattern != null
+                && !groupIdPattern.matcher(project.getGroupId()).matches()) {
+            msgBuilder
+                    .append("Group ID must match pattern \"")
+                    .append(groupIdPattern)
+                    .append("\" but is \"")
+                    .append(project.getGroupId())
+                    .append("\"");
+        }
+        if (artifactIdPattern != null
+                && !artifactIdPattern.matcher(project.getArtifactId()).matches()) {
+            if (msgBuilder.length() > 0) {
+                msgBuilder.append(System.lineSeparator());
+            }
+            msgBuilder
+                    .append("Artifact ID must match pattern \"")
+                    .append(artifactIdPattern)
+                    .append("\" but is \"")
+                    .append(project.getArtifactId())
+                    .append("\"");
+        }
+        if (moduleNameMustMatchArtifactId
+                && !project.isExecutionRoot()
+                && !project.getBasedir().getName().equals(project.getArtifactId())) {
+            if (msgBuilder.length() > 0) {
+                msgBuilder.append(System.lineSeparator());
+            }
+            msgBuilder
+                    .append("Module directory name must be equal to its artifact ID \"")
+                    .append(project.getArtifactId())
+                    .append("\" but is \"")
+                    .append(project.getBasedir().getName())
+                    .append("\"");
+        }
+        if (msgBuilder.length() > 0) {
+            throw new EnforcerRuleException(msgBuilder.toString());
+        }
+    }
+
+    public void setGroupIdPattern(String groupIdPattern) {

Review Comment:
   setters methods are not required





> New rule: Maven coordinates must match pattern
> ----------------------------------------------
>
>                 Key: MENFORCER-500
>                 URL: https://issues.apache.org/jira/browse/MENFORCER-500
>             Project: Maven Enforcer Plugin
>          Issue Type: Improvement
>            Reporter: Konrad Windszus
>            Assignee: Konrad Windszus
>            Priority: Major
>
> Sometimes it is crucial that the {{groupId}} and/or the {{artifactId}} match a certain pattern (e.g. because it is reserved namespace prefix for deploying to Maven Central). It would be beneficial to enforce that with a standard rule.
> The pattern should be a configurable regex pattern (independently for {{artifactId}} and {{groupId}})



--
This message was sent by Atlassian Jira
(v8.20.10#820010)