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/07/31 18:45:09 UTC

[maven-enforcer] branch master updated: [MENFORCER-397] allow no rules

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 ae93fa8  [MENFORCER-397] allow no rules
ae93fa8 is described below

commit ae93fa83d2c0e4d235a007511cdd3f55e4f11cc1
Author: Björn Raupach <ra...@me.com>
AuthorDate: Sun Jul 31 14:48:40 2022 +0200

    [MENFORCER-397] allow no rules
---
 .../apache/maven/plugins/enforcer/EnforceMojo.java | 38 ++++++++++++++++++++--
 .../maven/plugins/enforcer/TestEnforceMojo.java    | 14 ++++++++
 2 files changed, 49 insertions(+), 3 deletions(-)

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 ca5e221..da9a4ac 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
@@ -97,6 +97,14 @@ public class EnforceMojo
     @Parameter( property = "enforcer.failFast", defaultValue = "false" )
     private boolean failFast = false;
 
+    /**
+     * Flag to fail the build if no rules are present
+     *
+     * @since 3.1.1
+     */
+    @Parameter( property = "enforcer.failIfNoRules", defaultValue = "true" )
+    private boolean failIfNoRules = true;
+
     /**
      * Array of objects that implement the EnforcerRule interface to execute.
      */
@@ -153,9 +161,17 @@ public class EnforceMojo
 
         if ( !havingRules() )
         {
-            // CHECKSTYLE_OFF: LineLength
-            throw new MojoExecutionException( "No rules are configured. Use the skip flag if you want to disable execution." );
-            // CHECKSTYLE_ON: LineLength
+            if ( isFailIfNoRules() )
+            {
+                // CHECKSTYLE_OFF: LineLength
+                throw new MojoExecutionException( "No rules are configured. Use the skip flag if you want to disable execution." );
+                // CHECKSTYLE_ON: LineLength
+            }
+            else
+            {
+                log.warn( "No rules are configured." );
+                return;
+            }
         }
 
         // messages with warn/error flag
@@ -399,6 +415,22 @@ public class EnforceMojo
         this.skip = theSkip;
     }
 
+    /**
+     * @return the failIfNoRules
+     */
+    public boolean isFailIfNoRules()
+    {
+        return this.failIfNoRules;
+    }
+
+    /**
+     * @param thefailIfNoRules the failIfNoRules to set
+     */
+    public void setFailIfNoRules( boolean thefailIfNoRules )
+    {
+        this.failIfNoRules = thefailIfNoRules;
+    }
+
     /**
      * @return the project
      */
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 5dcbb91..093c9fa 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
@@ -300,6 +300,20 @@ public class TestEnforceMojo
         Mockito.verify( logSpy ).warn( Mockito.matches( ".* failed with message:" + System.lineSeparator() + "null" ) );
     }
 
+    @Test
+    public void testFailIfNoTests()
+            throws MojoExecutionException {
+        setupBasics( false );
+        mojo.setFailIfNoRules( false );
+
+        Log logSpy = setupLogSpy();
+
+        mojo.execute();
+
+        Mockito.verify( logSpy ).warn( Mockito.eq( "No rules are configured." ) );
+        Mockito.verifyNoMoreInteractions( logSpy );
+    }
+
     private void setupBasics( boolean fail )
     {
         mojo.setFail( fail );