Skip to content

Commit 26848dc

Browse files
authored
Merge pull request #12 from srdo/MCHECKSTYLE-371
[MCHECKSTYLE-371]: Add logViolationCountToConsole option to check goal
2 parents 2485ea7 + 3a8d555 commit 26848dc

File tree

5 files changed

+160
-3
lines changed

5 files changed

+160
-3
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
invoker.goals=verify
19+
# checkstyleRules requires Maven 3+
20+
invoker.maven.version = 3.0+

src/it/MCHECKSTYLE-371/pom.xml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one
4+
~ or more contributor license agreements. See the NOTICE file
5+
~ distributed with this work for additional information
6+
~ regarding copyright ownership. The ASF licenses this file
7+
~ to you under the Apache License, Version 2.0 (the
8+
~ "License"); you may not use this file except in compliance
9+
~ with the License. You may obtain a copy of the License at
10+
~
11+
~ http://www.apache.org/licenses/LICENSE-2.0
12+
~
13+
~ Unless required by applicable law or agreed to in writing,
14+
~ software distributed under the License is distributed on an
15+
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
~ KIND, either express or implied. See the License for the
17+
~ specific language governing permissions and limitations
18+
~ under the License.
19+
-->
20+
21+
<project xmlns="http://maven.apache.org/POM/4.0.0"
22+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
24+
<modelVersion>4.0.0</modelVersion>
25+
26+
<groupId>org.apache.maven.plugins.checkstyle.its</groupId>
27+
<artifactId>MCHECKSTYLE-371</artifactId>
28+
<version>1.0-SNAPSHOT</version>
29+
30+
<url>https://issues.apache.org/jira/browse/MCHECKSTYLE-371</url>
31+
32+
<properties>
33+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
34+
</properties>
35+
36+
<build>
37+
<plugins>
38+
<plugin>
39+
<groupId>org.apache.maven.plugins</groupId>
40+
<artifactId>maven-checkstyle-plugin</artifactId>
41+
<version>@project.version@</version>
42+
<configuration>
43+
<!-- some test resources are Java files and have to be properly formatted -->
44+
<resourceIncludes>**\/*.java</resourceIncludes>
45+
46+
<checkstyleRules>
47+
<module name="Checker">
48+
<property name="severity" value="warning"/>
49+
<!-- Checks for Size Violations. -->
50+
<!-- See http://checkstyle.sf.net/config_sizes.html -->
51+
<module name="FileLength">
52+
<property name="max" value="10" />
53+
<property name="fileExtensions" value="java" />
54+
</module>
55+
</module>
56+
</checkstyleRules>
57+
<logViolationCountToConsole>true</logViolationCountToConsole>
58+
<maxAllowedViolations>10</maxAllowedViolations>
59+
<violationSeverity>warning</violationSeverity>
60+
</configuration>
61+
<executions>
62+
<execution>
63+
<id>check</id>
64+
<goals>
65+
<goal>check</goal>
66+
</goals>
67+
</execution>
68+
</executions>
69+
</plugin>
70+
</plugins>
71+
</build>
72+
</project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
/**
23+
* My class.
24+
*/
25+
public class MyClass
26+
{
27+
}

src/it/MCHECKSTYLE-371/verify.groovy

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
/*
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
*/
20+
def buildLog = new File( basedir, 'build.log' )
21+
22+
assert buildLog.text.contains( "[INFO] You have 1 Checkstyle violation. The maximum number of allowed violations is 10." )

src/main/java/org/apache/maven/plugins/checkstyle/CheckstyleViolationCheckMojo.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ public class CheckstyleViolationCheckMojo
156156
*/
157157
@Parameter( property = "checkstyle.console", defaultValue = "true" )
158158
private boolean logViolationsToConsole;
159+
160+
/**
161+
* Output the detected violation count to the console.
162+
*
163+
* @since 3.0.1
164+
*/
165+
@Parameter( property = "checkstyle.logViolationCount", defaultValue = "true" )
166+
private boolean logViolationCountToConsole;
159167

160168
/**
161169
* Specifies the location of the resources to be used for Checkstyle.
@@ -571,21 +579,29 @@ public void execute()
571579

572580
int violations = countViolations( xpp );
573581

582+
String msg = "You have " + violations + " Checkstyle violation"
583+
+ ( ( violations > 1 || violations == 0 ) ? "s" : "" ) + ".";
574584
if ( violations > maxAllowedViolations )
575585
{
576586
if ( failOnViolation )
577587
{
578-
String msg =
579-
"You have " + violations + " Checkstyle violation" + ( ( violations > 1 ) ? "s" : "" ) + ".";
580588
if ( maxAllowedViolations > 0 )
581589
{
582590
msg += " The maximum number of allowed violations is " + maxAllowedViolations + ".";
583591
}
584592
throw new MojoFailureException( msg );
585593
}
586-
594+
587595
getLog().warn( "checkstyle:check violations detected but failOnViolation set to false" );
588596
}
597+
if ( logViolationCountToConsole )
598+
{
599+
if ( maxAllowedViolations > 0 )
600+
{
601+
msg += " The maximum number of allowed violations is " + maxAllowedViolations + ".";
602+
}
603+
getLog().info( msg );
604+
}
589605
}
590606
catch ( IOException | XmlPullParserException e )
591607
{

0 commit comments

Comments
 (0)