diff --git a/maven-enforcer-plugin/pom.xml b/maven-enforcer-plugin/pom.xml index e8b74a25..dffedf4f 100644 --- a/maven-enforcer-plugin/pom.xml +++ b/maven-enforcer-plugin/pom.xml @@ -77,6 +77,11 @@ junit-jupiter-engine test + + org.assertj + assertj-core + test + diff --git a/maven-enforcer-plugin/src/it/projects/cli-always-fail-deprecated/invoker.properties b/maven-enforcer-plugin/src/it/projects/cli-always-fail-deprecated/invoker.properties new file mode 100644 index 00000000..c683c671 --- /dev/null +++ b/maven-enforcer-plugin/src/it/projects/cli-always-fail-deprecated/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 = validate -Drules=AlwaysFail +invoker.buildResult = failure diff --git a/maven-enforcer-plugin/src/it/projects/cli-always-fail-deprecated/pom.xml b/maven-enforcer-plugin/src/it/projects/cli-always-fail-deprecated/pom.xml new file mode 100644 index 00000000..c3e9c7ab --- /dev/null +++ b/maven-enforcer-plugin/src/it/projects/cli-always-fail-deprecated/pom.xml @@ -0,0 +1,55 @@ + + + + + + 4.0.0 + + org.apache.maven.its.enforcer + test + 1.0 + + + + + + + + org.apache.maven.plugins + maven-enforcer-plugin + @project.version@ + + + test + + enforce + + + + + + true + + + + + + + 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 00000000..d7cdafa9 --- /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 ee2da69d..9d4f4503 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 0915303c..c3e9c7ab 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. org.apache.maven.plugins maven-enforcer-plugin @project.version@ + + + test + + enforce + + + + + + true + + + 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 00000000..d7cdafa9 --- /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-deprecated/invoker.properties b/maven-enforcer-plugin/src/it/projects/cli-always-pass-deprecated/invoker.properties new file mode 100644 index 00000000..d70aaef9 --- /dev/null +++ b/maven-enforcer-plugin/src/it/projects/cli-always-pass-deprecated/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 = validate -Drules=AlwaysPass +invoker.buildResult = success diff --git a/maven-enforcer-plugin/src/it/projects/cli-always-pass-deprecated/pom.xml b/maven-enforcer-plugin/src/it/projects/cli-always-pass-deprecated/pom.xml new file mode 100644 index 00000000..e5e2e807 --- /dev/null +++ b/maven-enforcer-plugin/src/it/projects/cli-always-pass-deprecated/pom.xml @@ -0,0 +1,55 @@ + + + + + + 4.0.0 + + org.apache.maven.its.enforcer + test + 1.0 + + + + + + + + org.apache.maven.plugins + maven-enforcer-plugin + @project.version@ + + + test + + enforce + + + + + + true + + + + + + + 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 00000000..34bbcab7 --- /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 c67cebe4..9ce63b20 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 2375f8c6..e5e2e807 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. - + + true 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 00000000..34bbcab7 --- /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 cf44b6cd..695f1139 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 { * <message>message for rule</message> * </alwaysPass> * <myRule implementation="org.example.MyRule"/> - * </rules> + * </rules> * * * @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 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 rulesToExecute; + + /** + * List of strings that matches the EnforcerRules to execute. Replacement for the rules property. + * + * @since 3.2.0 + */ + @Parameter(required = false, property = "enforcer.rules") + public void setRulesToExecute(List 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 enforcer.rules property instead + */ + @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."); + setRulesToExecute(rulesToExecute); + } + @Override public void execute() throws MojoExecutionException { Log log = this.getLog(); @@ -274,21 +298,19 @@ public void execute() throws MojoExecutionException { */ private Optional 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 60d87bf9..4be12c43 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 @@ 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.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 @@ void testFailIfNoTests() throws MojoExecutionException { Mockito.verifyNoMoreInteractions(logSpy); } + @Test + void testFailIfBothRuleOverridePropertiesAreSet() throws MojoExecutionException { + setupBasics(false); + + Log logSpy = setupLogSpy(); + List 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());