diff --git a/src/it/check-fail-with-mismatch-violation-count/invoker.properties b/src/it/check-fail-with-mismatch-violation-count/invoker.properties new file mode 100644 index 00000000..0221874f --- /dev/null +++ b/src/it/check-fail-with-mismatch-violation-count/invoker.properties @@ -0,0 +1,19 @@ +# 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=verify +invoker.buildResult=failure diff --git a/src/it/check-fail-with-mismatch-violation-count/pom.xml b/src/it/check-fail-with-mismatch-violation-count/pom.xml new file mode 100644 index 00000000..645d0994 --- /dev/null +++ b/src/it/check-fail-with-mismatch-violation-count/pom.xml @@ -0,0 +1,57 @@ + + + + + 4.0.0 + + org.apache.maven.plugins.checkstyle + check-fail + 1.0-SNAPSHOT + jar + + http://maven.apache.org/ + + + UTF-8 + NewlineAtEndOfFile + 314 + true + + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + @project.version@ + + + check + + check + + + + + + + diff --git a/src/it/check-fail-with-mismatch-violation-count/src/main/java/org/MyClass.java b/src/it/check-fail-with-mismatch-violation-count/src/main/java/org/MyClass.java new file mode 100644 index 00000000..666efd9e --- /dev/null +++ b/src/it/check-fail-with-mismatch-violation-count/src/main/java/org/MyClass.java @@ -0,0 +1,27 @@ +package org; + +/* + * 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. + */ + +/** + * My class. + */ +public class MyClass +{ +} diff --git a/src/it/check-fail-with-mismatch-violation-count/src/main/java/org/package-info.java b/src/it/check-fail-with-mismatch-violation-count/src/main/java/org/package-info.java new file mode 100644 index 00000000..6d0409e8 --- /dev/null +++ b/src/it/check-fail-with-mismatch-violation-count/src/main/java/org/package-info.java @@ -0,0 +1,23 @@ +/** + * Nothing very important here, only a file for checkstyle rule PackageInfo. + */ +package org; + +/* + * 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. + */ diff --git a/src/it/check-fail-with-mismatch-violation-count/verify.groovy b/src/it/check-fail-with-mismatch-violation-count/verify.groovy new file mode 100644 index 00000000..c50301e4 --- /dev/null +++ b/src/it/check-fail-with-mismatch-violation-count/verify.groovy @@ -0,0 +1,22 @@ + +/* + * 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' ) + +assert buildLog.text.contains( "The number of violations (1) is not equal to the maximum number of allowed violations (314)" ) diff --git a/src/main/java/org/apache/maven/plugins/checkstyle/CheckstyleViolationCheckMojo.java b/src/main/java/org/apache/maven/plugins/checkstyle/CheckstyleViolationCheckMojo.java index 5ba62853..0c033719 100644 --- a/src/main/java/org/apache/maven/plugins/checkstyle/CheckstyleViolationCheckMojo.java +++ b/src/main/java/org/apache/maven/plugins/checkstyle/CheckstyleViolationCheckMojo.java @@ -108,6 +108,19 @@ public class CheckstyleViolationCheckMojo extends AbstractMojo { @Parameter(property = "checkstyle.failOnViolation", defaultValue = "true") private boolean failOnViolation; + /** + * Fail the build when the number of violations is not equal to {@link #maxAllowedViolations}. + *

This prevents the following scenario: + *

+ * + * * @since 3.6.1 + */ + @Parameter(property = "checkstyle.failOnMismatchedAllowedViolationsCount", defaultValue = "false") + private boolean failOnMismatchedAllowedViolationsCount; + /** * The maximum number of allowed violations. The execution fails only if the * number of violations is above this limit. @@ -599,6 +612,10 @@ public void execute() throws MojoExecutionException, MojoFailureException { } getLog().warn("checkstyle:check violations detected but failOnViolation set to false"); + } else if (failOnMismatchedAllowedViolationsCount && violationCount != maxAllowedViolations) { + throw new MojoFailureException("The number of violations (" + violationCount + + ") is not equal to the maximum number of allowed violations (" + maxAllowedViolations + + ")."); } if (logViolationCountToConsole) { if (maxAllowedViolations > 0) {