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 2022/12/29 20:19:39 UTC

[maven-enforcer] branch master updated: [MENFORCER-454] Deprecate 'rules' property and introduce 'enforcer.rules' as a replacement

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 495818f  [MENFORCER-454] Deprecate 'rules' property and introduce 'enforcer.rules' as a replacement
495818f is described below

commit 495818f95eb773a677672b21a2bf5969ebdc2411
Author: Petr Široký <pe...@pm.me>
AuthorDate: Thu Dec 29 16:10:31 2022 +0100

    [MENFORCER-454] Deprecate 'rules' property and introduce 'enforcer.rules' as a replacement
    
     * updated the ITs to verify the rules are indeed getting fully replaced
       with the list from the property
---
 maven-enforcer-plugin/pom.xml                      |  5 +++
 .../invoker.properties                             |  2 +-
 .../pom.xml                                        |  3 +-
 .../cli-always-fail-deprecated/verify.groovy       | 20 ++++++++++
 .../it/projects/cli-always-fail/invoker.properties |  2 +-
 .../src/it/projects/cli-always-fail/pom.xml        | 14 +++++++
 .../src/it/projects/cli-always-fail/verify.groovy  | 20 ++++++++++
 .../invoker.properties                             |  2 +-
 .../pom.xml                                        |  3 +-
 .../cli-always-pass-deprecated/verify.groovy       | 20 ++++++++++
 .../it/projects/cli-always-pass/invoker.properties |  2 +-
 .../src/it/projects/cli-always-pass/pom.xml        |  3 +-
 .../src/it/projects/cli-always-pass/verify.groovy  | 20 ++++++++++
 .../apache/maven/plugins/enforcer/EnforceMojo.java | 46 ++++++++++++++++------
 .../maven/plugins/enforcer/TestEnforceMojo.java    | 28 +++++++++++++
 15 files changed, 171 insertions(+), 19 deletions(-)

diff --git a/maven-enforcer-plugin/pom.xml b/maven-enforcer-plugin/pom.xml
index e8b74a2..dffedf4 100644
--- a/maven-enforcer-plugin/pom.xml
+++ b/maven-enforcer-plugin/pom.xml
@@ -77,6 +77,11 @@
       <artifactId>junit-jupiter-engine</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.assertj</groupId>
+      <artifactId>assertj-core</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <build>
diff --git a/maven-enforcer-plugin/src/it/projects/cli-always-fail/invoker.properties b/maven-enforcer-plugin/src/it/projects/cli-always-fail-deprecated/invoker.properties
similarity index 88%
copy from maven-enforcer-plugin/src/it/projects/cli-always-fail/invoker.properties
copy to maven-enforcer-plugin/src/it/projects/cli-always-fail-deprecated/invoker.properties
index ee2da69..c683c67 100644
--- a/maven-enforcer-plugin/src/it/projects/cli-always-fail/invoker.properties
+++ b/maven-enforcer-plugin/src/it/projects/cli-always-fail-deprecated/invoker.properties
@@ -14,5 +14,5 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
-invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:enforce -Drules=alwaysFail
+invoker.goals = validate -Drules=AlwaysFail
 invoker.buildResult = failure
diff --git a/maven-enforcer-plugin/src/it/projects/cli-always-pass/pom.xml b/maven-enforcer-plugin/src/it/projects/cli-always-fail-deprecated/pom.xml
similarity index 92%
copy from maven-enforcer-plugin/src/it/projects/cli-always-pass/pom.xml
copy to maven-enforcer-plugin/src/it/projects/cli-always-fail-deprecated/pom.xml
index 2375f8c..c3e9c7a 100644
--- a/maven-enforcer-plugin/src/it/projects/cli-always-pass/pom.xml
+++ b/maven-enforcer-plugin/src/it/projects/cli-always-fail-deprecated/pom.xml
@@ -43,8 +43,9 @@ under the License.
             </goals>
             <configuration>
               <rules>
-                <AlwaysPass/>
+                <AlwaysPass/> <!-- Should be replaced by 'AlwaysFail' via command line property -->
               </rules>
+              <fail>true</fail>
             </configuration>
           </execution>
         </executions>
diff --git a/maven-enforcer-plugin/src/it/projects/cli-always-fail-deprecated/verify.groovy b/maven-enforcer-plugin/src/it/projects/cli-always-fail-deprecated/verify.groovy
new file mode 100644
index 0000000..d7cdafa
--- /dev/null
+++ b/maven-enforcer-plugin/src/it/projects/cli-always-fail-deprecated/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('[INFO] Always pass!')
diff --git a/maven-enforcer-plugin/src/it/projects/cli-always-fail/invoker.properties b/maven-enforcer-plugin/src/it/projects/cli-always-fail/invoker.properties
index ee2da69..9d4f450 100644
--- a/maven-enforcer-plugin/src/it/projects/cli-always-fail/invoker.properties
+++ b/maven-enforcer-plugin/src/it/projects/cli-always-fail/invoker.properties
@@ -14,5 +14,5 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
-invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:enforce -Drules=alwaysFail
+invoker.goals = validate -Denforcer.rules=AlwaysFail
 invoker.buildResult = failure
diff --git a/maven-enforcer-plugin/src/it/projects/cli-always-fail/pom.xml b/maven-enforcer-plugin/src/it/projects/cli-always-fail/pom.xml
index 0915303..c3e9c7a 100644
--- a/maven-enforcer-plugin/src/it/projects/cli-always-fail/pom.xml
+++ b/maven-enforcer-plugin/src/it/projects/cli-always-fail/pom.xml
@@ -35,6 +35,20 @@ under the License.
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-enforcer-plugin</artifactId>
         <version>@project.version@</version>
+        <executions>
+          <execution>
+            <id>test</id>
+            <goals>
+              <goal>enforce</goal>
+            </goals>
+            <configuration>
+              <rules>
+                <AlwaysPass/> <!-- Should be replaced by 'AlwaysFail' via command line property -->
+              </rules>
+              <fail>true</fail>
+            </configuration>
+          </execution>
+        </executions>
       </plugin>
     </plugins>
   </build>
diff --git a/maven-enforcer-plugin/src/it/projects/cli-always-fail/verify.groovy b/maven-enforcer-plugin/src/it/projects/cli-always-fail/verify.groovy
new file mode 100644
index 0000000..d7cdafa
--- /dev/null
+++ b/maven-enforcer-plugin/src/it/projects/cli-always-fail/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('[INFO] Always pass!')
diff --git a/maven-enforcer-plugin/src/it/projects/cli-always-pass/invoker.properties b/maven-enforcer-plugin/src/it/projects/cli-always-pass-deprecated/invoker.properties
similarity index 88%
copy from maven-enforcer-plugin/src/it/projects/cli-always-pass/invoker.properties
copy to maven-enforcer-plugin/src/it/projects/cli-always-pass-deprecated/invoker.properties
index c67cebe..d70aaef 100644
--- a/maven-enforcer-plugin/src/it/projects/cli-always-pass/invoker.properties
+++ b/maven-enforcer-plugin/src/it/projects/cli-always-pass-deprecated/invoker.properties
@@ -14,5 +14,5 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
-invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:enforce -Drules=alwaysPass
+invoker.goals = validate -Drules=AlwaysPass
 invoker.buildResult = success
diff --git a/maven-enforcer-plugin/src/it/projects/cli-always-pass/pom.xml b/maven-enforcer-plugin/src/it/projects/cli-always-pass-deprecated/pom.xml
similarity index 92%
copy from maven-enforcer-plugin/src/it/projects/cli-always-pass/pom.xml
copy to maven-enforcer-plugin/src/it/projects/cli-always-pass-deprecated/pom.xml
index 2375f8c..e5e2e80 100644
--- a/maven-enforcer-plugin/src/it/projects/cli-always-pass/pom.xml
+++ b/maven-enforcer-plugin/src/it/projects/cli-always-pass-deprecated/pom.xml
@@ -43,8 +43,9 @@ under the License.
             </goals>
             <configuration>
               <rules>
-                <AlwaysPass/>
+                <AlwaysFail/> <!-- Should be replaced by 'AlwaysPass' via command line property -->
               </rules>
+              <fail>true</fail>
             </configuration>
           </execution>
         </executions>
diff --git a/maven-enforcer-plugin/src/it/projects/cli-always-pass-deprecated/verify.groovy b/maven-enforcer-plugin/src/it/projects/cli-always-pass-deprecated/verify.groovy
new file mode 100644
index 0000000..34bbcab
--- /dev/null
+++ b/maven-enforcer-plugin/src/it/projects/cli-always-pass-deprecated/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('[INFO] Always pass!')
diff --git a/maven-enforcer-plugin/src/it/projects/cli-always-pass/invoker.properties b/maven-enforcer-plugin/src/it/projects/cli-always-pass/invoker.properties
index c67cebe..9ce63b2 100644
--- a/maven-enforcer-plugin/src/it/projects/cli-always-pass/invoker.properties
+++ b/maven-enforcer-plugin/src/it/projects/cli-always-pass/invoker.properties
@@ -14,5 +14,5 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
-invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:enforce -Drules=alwaysPass
+invoker.goals = validate -Denforcer.rules=AlwaysPass
 invoker.buildResult = success
diff --git a/maven-enforcer-plugin/src/it/projects/cli-always-pass/pom.xml b/maven-enforcer-plugin/src/it/projects/cli-always-pass/pom.xml
index 2375f8c..e5e2e80 100644
--- a/maven-enforcer-plugin/src/it/projects/cli-always-pass/pom.xml
+++ b/maven-enforcer-plugin/src/it/projects/cli-always-pass/pom.xml
@@ -43,8 +43,9 @@ under the License.
             </goals>
             <configuration>
               <rules>
-                <AlwaysPass/>
+                <AlwaysFail/> <!-- Should be replaced by 'AlwaysPass' via command line property -->
               </rules>
+              <fail>true</fail>
             </configuration>
           </execution>
         </executions>
diff --git a/maven-enforcer-plugin/src/it/projects/cli-always-pass/verify.groovy b/maven-enforcer-plugin/src/it/projects/cli-always-pass/verify.groovy
new file mode 100644
index 0000000..34bbcab
--- /dev/null
+++ b/maven-enforcer-plugin/src/it/projects/cli-always-pass/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('[INFO] Always pass!')
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 cf44b6c..695f113 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
@@ -118,7 +118,7 @@ public class EnforceMojo extends AbstractMojo {
      *             &lt;message&gt;message for rule&lt;/message&gt;
      *         &lt;/alwaysPass&gt;
      *         &lt;myRule implementation="org.example.MyRule"/&gt;
-     *     &lt;/rules>
+     *     &lt;/rules&gt;
      * </pre>
      *
      * @since 1.0.0
@@ -126,12 +126,6 @@ public class EnforceMojo extends AbstractMojo {
     @Parameter
     private PlexusConfiguration rules;
 
-    /**
-     * List of strings that matches the EnforcerRules to execute.
-     */
-    @Parameter(required = false, property = "rules")
-    private List<String> commandLineRules;
-
     /**
      * List of strings that matches the EnforcerRules to skip.
      *
@@ -153,6 +147,36 @@ public class EnforceMojo extends AbstractMojo {
     @Component
     private EnforcerRuleManager enforcerRuleManager;
 
+    private List<String> rulesToExecute;
+
+    /**
+     * List of strings that matches the EnforcerRules to execute. Replacement for the <code>rules</code> property.
+     *
+     * @since 3.2.0
+     */
+    @Parameter(required = false, property = "enforcer.rules")
+    public void setRulesToExecute(List<String> rulesToExecute) throws MojoExecutionException {
+        if (rulesToExecute != null && !rulesToExecute.isEmpty()) {
+            if (this.rulesToExecute != null && !this.rulesToExecute.isEmpty()) {
+                throw new MojoExecutionException("Detected the usage of both '-Drules' (which is deprecated) "
+                        + "and '-Denforcer.rules'. Please use only one of them, preferably '-Denforcer.rules'.");
+            }
+            this.rulesToExecute = rulesToExecute;
+        }
+    }
+
+    /**
+     * List of strings that matches the EnforcerRules to execute.
+     *
+     * @deprecated Use <code>enforcer.rules</code> property instead
+     */
+    @Parameter(required = false, property = "rules")
+    @Deprecated
+    public void setCommandLineRules(List<String> rulesToExecute) throws MojoExecutionException {
+        getLog().warn("Detected the usage of property '-Drules' which is deprecated. Use '-Denforcer.rules' instead.");
+        setRulesToExecute(rulesToExecute);
+    }
+
     @Override
     public void execute() throws MojoExecutionException {
         Log log = this.getLog();
@@ -274,21 +298,19 @@ public class EnforceMojo extends AbstractMojo {
      */
     private Optional<PlexusConfiguration> createRulesFromCommandLineOptions() {
 
-        if (commandLineRules == null || commandLineRules.isEmpty()) {
+        if (rulesToExecute == null || rulesToExecute.isEmpty()) {
             return Optional.empty();
         }
 
         PlexusConfiguration configuration = new DefaultPlexusConfiguration("rules");
-
-        for (String rule : commandLineRules) {
+        for (String rule : rulesToExecute) {
             configuration.addChild(new DefaultPlexusConfiguration(rule));
         }
-
         return Optional.of(configuration);
     }
 
     /**
-     * Filter our (remove) rules that have been specifically skipped via additional configuration.
+     * Filter out (remove) rules that have been specifically skipped via additional configuration.
      *
      * @param allRules list of enforcer rules to go through and filter
      *
diff --git a/maven-enforcer-plugin/src/test/java/org/apache/maven/plugins/enforcer/TestEnforceMojo.java b/maven-enforcer-plugin/src/test/java/org/apache/maven/plugins/enforcer/TestEnforceMojo.java
index 60d87bf..4be12c4 100644
--- a/maven-enforcer-plugin/src/test/java/org/apache/maven/plugins/enforcer/TestEnforceMojo.java
+++ b/maven-enforcer-plugin/src/test/java/org/apache/maven/plugins/enforcer/TestEnforceMojo.java
@@ -20,6 +20,7 @@ package org.apache.maven.plugins.enforcer;
 
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.List;
 
 import org.apache.maven.enforcer.rule.api.EnforcerRule;
 import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
@@ -35,6 +36,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
 import org.mockito.junit.jupiter.MockitoSettings;
 import org.mockito.quality.Strictness;
 
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.junit.jupiter.api.Assertions.*;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.when;
@@ -291,6 +293,32 @@ class TestEnforceMojo {
         Mockito.verifyNoMoreInteractions(logSpy);
     }
 
+    @Test
+    void testFailIfBothRuleOverridePropertiesAreSet() throws MojoExecutionException {
+        setupBasics(false);
+
+        Log logSpy = setupLogSpy();
+        List<String> rules = Arrays.asList("rule1", "rule2");
+        mojo.setRulesToExecute(rules);
+
+        assertThatThrownBy(() -> mojo.setCommandLineRules(rules))
+                .isInstanceOf(MojoExecutionException.class)
+                .hasMessageContaining(
+                        "Detected the usage of both '-Drules' (which is deprecated) and '-Denforcer.rules'");
+    }
+
+    @Test
+    void testShouldPrintWarnWhenUsingDeprecatedRulesProperty() throws MojoExecutionException {
+        setupBasics(false);
+
+        Log logSpy = setupLogSpy();
+
+        mojo.setCommandLineRules(Arrays.asList("rule1", "rule2"));
+
+        Mockito.verify(logSpy)
+                .warn("Detected the usage of property '-Drules' which is deprecated. Use '-Denforcer.rules' instead.");
+    }
+
     private void setupBasics(boolean fail) {
         mojo.setFail(fail);
         mojo.setSession(EnforcerTestUtils.getMavenSession());