From 4718b56f69e975f923c43cf1cc9a91c08b2ecb97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20=C5=A0irok=C3=BD?= Date: Thu, 29 Dec 2022 22:23:00 +0100 Subject: [PATCH] [MENFORCER-454] Log warning only if the '-Drules' is actually used * the method gets called with empty list if there is no configuration, resulting in WARNing being printed every single time --- .../apache/maven/plugins/enforcer/EnforceMojo.java | 5 ++++- .../maven/plugins/enforcer/TestEnforceMojo.java | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) 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 695f1139..24c2545c 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 @@ -173,7 +173,10 @@ public void setRulesToExecute(List rulesToExecute) throws MojoExecutionE @Parameter(required = false, property = "rules") @Deprecated public void setCommandLineRules(List rulesToExecute) throws MojoExecutionException { - getLog().warn("Detected the usage of property '-Drules' which is deprecated. Use '-Denforcer.rules' instead."); + if (rulesToExecute != null && !rulesToExecute.isEmpty()) { + getLog().warn( + "Detected the usage of property '-Drules' which is deprecated. Use '-Denforcer.rules' instead."); + } setRulesToExecute(rulesToExecute); } 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 4be12c43..cd25b7c3 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 @@ -319,6 +319,17 @@ void testShouldPrintWarnWhenUsingDeprecatedRulesProperty() throws MojoExecutionE .warn("Detected the usage of property '-Drules' which is deprecated. Use '-Denforcer.rules' instead."); } + @Test + void testShouldNotPrintWarnWhenDeprecatedRulesPropertyIsEmpty() throws MojoExecutionException { + setupBasics(false); + + Log logSpy = setupLogSpy(); + + mojo.setCommandLineRules(Collections.emptyList()); + + Mockito.verifyNoInteractions(logSpy); + } + private void setupBasics(boolean fail) { mojo.setFail(fail); mojo.setSession(EnforcerTestUtils.getMavenSession());