You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sj...@apache.org on 2025/01/15 23:04:43 UTC

(maven-enforcer) branch master updated: [MENFORCER-516] Use rule configuration in POM when executed from CLI (#344)

This is an automated email from the ASF dual-hosted git repository.

sjaranowski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-enforcer.git


The following commit(s) were added to refs/heads/master by this push:
     new 375fb15  [MENFORCER-516] Use rule configuration in POM when executed from CLI (#344)
375fb15 is described below

commit 375fb152925970975d0b721c2da73f1dd595076d
Author: Ben Foster <bp...@gmail.com>
AuthorDate: Wed Jan 15 18:04:38 2025 -0500

    [MENFORCER-516] Use rule configuration in POM when executed from CLI (#344)
---
 .../projects/cli-banned-plugins/invoker.properties | 18 +++++
 .../src/it/projects/cli-banned-plugins/pom.xml     | 56 ++++++++++++++++
 .../it/projects/cli-banned-plugins/verify.groovy   | 20 ++++++
 .../apache/maven/plugins/enforcer/EnforceMojo.java | 18 ++++-
 .../examples/check-specific-rule-via-cli.apt.vm    | 76 ++++++++++++++++++++++
 5 files changed, 187 insertions(+), 1 deletion(-)

diff --git a/maven-enforcer-plugin/src/it/projects/cli-banned-plugins/invoker.properties b/maven-enforcer-plugin/src/it/projects/cli-banned-plugins/invoker.properties
new file mode 100644
index 0000000..02f4cea
--- /dev/null
+++ b/maven-enforcer-plugin/src/it/projects/cli-banned-plugins/invoker.properties
@@ -0,0 +1,18 @@
+# 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.
+invoker.goals = enforcer:enforce -Denforcer.rules=bannedPlugins
+invoker.buildResult = failure
diff --git a/maven-enforcer-plugin/src/it/projects/cli-banned-plugins/pom.xml b/maven-enforcer-plugin/src/it/projects/cli-banned-plugins/pom.xml
new file mode 100644
index 0000000..c426ab2
--- /dev/null
+++ b/maven-enforcer-plugin/src/it/projects/cli-banned-plugins/pom.xml
@@ -0,0 +1,56 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+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.
+-->
+
+<project>
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.maven.its.enforcer</groupId>
+  <artifactId>test</artifactId>
+  <version>1.0</version>
+
+  <description>
+  </description>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins.enforcer.its</groupId>
+        <artifactId>menforcer126_maven-plugin</artifactId>
+        <version>1.0</version>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-enforcer-plugin</artifactId>
+        <version>@project.version@</version>
+        <configuration>
+          <rules>
+            <bannedPlugins>
+              <excludes>
+                <exclude>org.apache.maven.plugins.enforcer.its:menforcer126_maven-plugin</exclude>
+              </excludes>
+            </bannedPlugins>
+          </rules>
+          <fail>true</fail>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/maven-enforcer-plugin/src/it/projects/cli-banned-plugins/verify.groovy b/maven-enforcer-plugin/src/it/projects/cli-banned-plugins/verify.groovy
new file mode 100644
index 0000000..6bea152
--- /dev/null
+++ b/maven-enforcer-plugin/src/it/projects/cli-banned-plugins/verify.groovy
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+def buildLog = new File(basedir, 'build.log').text
+assert buildLog.contains('org.apache.maven.plugins.enforcer.its:menforcer126_maven-plugin:maven-plugin:1.0 <--- banned plugin')
diff --git a/maven-enforcer-plugin/src/main/java/org/apache/maven/plugins/enforcer/EnforceMojo.java b/maven-enforcer-plugin/src/main/java/org/apache/maven/plugins/enforcer/EnforceMojo.java
index 2e4c478..bbd3b81 100644
--- a/maven-enforcer-plugin/src/main/java/org/apache/maven/plugins/enforcer/EnforceMojo.java
+++ b/maven-enforcer-plugin/src/main/java/org/apache/maven/plugins/enforcer/EnforceMojo.java
@@ -366,7 +366,23 @@ public class EnforceMojo extends AbstractMojo {
 
         PlexusConfiguration configuration = new DefaultPlexusConfiguration("rules");
         for (String rule : rulesToExecute) {
-            configuration.addChild(new DefaultPlexusConfiguration(rule));
+            PlexusConfiguration configuredRule = null;
+            // Check if there's configuration in the project for this rule and use it if so
+            if (rules != null) {
+                // rule names haven't been normalized yet, so check both with first character upper and lower
+                String ruleLower = Character.toLowerCase(rule.charAt(0)) + rule.substring(1);
+                String ruleUpper = Character.toUpperCase(rule.charAt(0)) + rule.substring(1);
+                configuredRule = rules.getChild(ruleLower, false);
+                if (configuredRule == null) {
+                    configuredRule = rules.getChild(ruleUpper, false);
+                }
+            }
+
+            if (configuredRule != null) {
+                configuration.addChild(configuredRule);
+            } else {
+                configuration.addChild(new DefaultPlexusConfiguration(rule));
+            }
         }
         return Optional.of(configuration);
     }
diff --git a/maven-enforcer-plugin/src/site/apt/examples/check-specific-rule-via-cli.apt.vm b/maven-enforcer-plugin/src/site/apt/examples/check-specific-rule-via-cli.apt.vm
index 656b697..c6aa8b0 100644
--- a/maven-enforcer-plugin/src/site/apt/examples/check-specific-rule-via-cli.apt.vm
+++ b/maven-enforcer-plugin/src/site/apt/examples/check-specific-rule-via-cli.apt.vm
@@ -31,4 +31,80 @@ Check specific rule via CLI
 mvn enforcer:enforce -Denforcer.rules=alwaysPass,alwaysFail
 +---+
 
+  Many rules require configuration to be meaningful.  If configuration for a rule is defined in the POM, it will be used.
+
+  Note that if configuration for a rule is defined within an execution, the execution ID must be specified on the command line.
+
+  For example, given the following plugin configuration:
+
++---+
+<project>
+  [...]
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-enforcer-plugin</artifactId>
+        <version>${project.version}</version>
+        <configuration>
+          <rules>
+            <requireMavenVersion>
+              <version>3.0</version>
+            </requireMavenVersion>
+          </rules>    
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+  [...]
+</project>
++---+
+
+  You can run this rule with the defined configuration:
+
++---+
+mvn enforcer:enforce -Denforcer.rules=requireMavenVersion
++---+
+
+
+  However, if your plugin configuration is within an execution context:
+
++---+
+<project>
+  [...]
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-enforcer-plugin</artifactId>
+        <version>${project.version}</version>
+        <executions>
+          <execution>
+            <id>enforce-maven</id>
+            <goals>
+              <goal>enforce</goal>
+            </goals>
+            <configuration>
+              <rules>
+                <requireMavenVersion>
+                  <version>3.0</version>
+                </requireMavenVersion>
+              </rules>    
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+  [...]
+</project>
++---+
+
+  You must name the execution ID on the command line for the configuration to apply:
+
++---+
+mvn enforcer:enforce@enforce-maven -Denforcer.rules=requireMavenVersion
++---+
+
+
   A full list of built-in rules can be found {{{../../../enforcer/enforcer-rules/index.html}here}}.
\ No newline at end of file