Skip to content

Commit 1c271f1

Browse files
gmshakemichael-o
authored andcommitted
[MCHECKSTYLE-412] Add option to exclude generated sources/test-sources from default source/test-source directories
This closes #76
1 parent 9ebda63 commit 1c271f1

File tree

8 files changed

+247
-4
lines changed

8 files changed

+247
-4
lines changed
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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

src/it/MCHECKSTYLE-412/pom.xml

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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-412</artifactId>
28+
<version>1.0-SNAPSHOT</version>
29+
30+
<url>https://issues.apache.org/jira/browse/MCHECKSTYLE-412</url>
31+
32+
<properties>
33+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
34+
</properties>
35+
36+
<dependencies>
37+
<dependency>
38+
<groupId>org.antlr</groupId>
39+
<artifactId>antlr4</artifactId>
40+
<version>4.3</version>
41+
</dependency>
42+
</dependencies>
43+
44+
<build>
45+
<plugins>
46+
<plugin>
47+
<groupId>org.antlr</groupId>
48+
<artifactId>antlr4-maven-plugin</artifactId>
49+
<version>4.3</version>
50+
<executions>
51+
<execution>
52+
<id>antlr</id>
53+
<goals>
54+
<goal>antlr4</goal>
55+
</goals>
56+
</execution>
57+
</executions>
58+
</plugin>
59+
<plugin>
60+
<groupId>org.apache.maven.plugins</groupId>
61+
<artifactId>maven-checkstyle-plugin</artifactId>
62+
<version>@project.version@</version>
63+
<configuration>
64+
<excludeGeneratedSources>true</excludeGeneratedSources>
65+
</configuration>
66+
<executions>
67+
<execution>
68+
<id>check</id>
69+
<goals>
70+
<goal>check</goal>
71+
</goals>
72+
</execution>
73+
</executions>
74+
</plugin>
75+
</plugins>
76+
</build>
77+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
grammar MyGrammar;
19+
r : 'foo' ID ;
20+
ID : [a-zA-Z0-9_]+ ;
21+
WS : [ \t\r\n]+ -> skip ;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Nothing very important here, only a file for checkstyle rule PackageInfo.
3+
*/
4+
package org;
5+
6+
/*
7+
* Licensed to the Apache Software Foundation (ASF) under one
8+
* or more contributor license agreements. See the NOTICE file
9+
* distributed with this work for additional information
10+
* regarding copyright ownership. The ASF licenses this file
11+
* to you under the Apache License, Version 2.0 (the
12+
* "License"); you may not use this file except in compliance
13+
* with the License. You may obtain a copy of the License at
14+
*
15+
* http://www.apache.org/licenses/LICENSE-2.0
16+
*
17+
* Unless required by applicable law or agreed to in writing,
18+
* software distributed under the License is distributed on an
19+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20+
* KIND, either express or implied. See the License for the
21+
* specific language governing permissions and limitations
22+
* under the License.
23+
*/

src/it/MCHECKSTYLE-412/verify.groovy

+22
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 0 Checkstyle violations." )

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

+30-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.FileOutputStream;
2525
import java.io.IOException;
2626
import java.io.OutputStream;
27+
import java.nio.file.Path;
2728
import java.util.ArrayList;
2829
import java.util.List;
2930
import java.util.Locale;
@@ -426,6 +427,14 @@ public abstract class AbstractCheckstyleReport extends AbstractMavenReport {
426427
+ " \"https://checkstyle.org/dtds/configuration_1_3.dtd\">\n")
427428
private String checkstyleRulesHeader;
428429

430+
/**
431+
* Specifies whether generated source files should be excluded from Checkstyle.
432+
*
433+
* @since 3.3.1
434+
*/
435+
@Parameter(property = "checkstyle.excludeGeneratedSources", defaultValue = "false")
436+
private boolean excludeGeneratedSources;
437+
429438
/**
430439
*/
431440
@Component
@@ -697,7 +706,7 @@ private boolean checkMavenJxrPluginIsConfigured() {
697706

698707
protected List<File> getSourceDirectories() {
699708
if (sourceDirectories == null) {
700-
sourceDirectories = project.getCompileSourceRoots();
709+
sourceDirectories = filterBuildTarget(project.getCompileSourceRoots());
701710
}
702711
List<File> sourceDirs = new ArrayList<>(sourceDirectories.size());
703712
for (String sourceDir : sourceDirectories) {
@@ -708,12 +717,31 @@ protected List<File> getSourceDirectories() {
708717

709718
protected List<File> getTestSourceDirectories() {
710719
if (testSourceDirectories == null) {
711-
testSourceDirectories = project.getTestCompileSourceRoots();
720+
testSourceDirectories = filterBuildTarget(project.getTestCompileSourceRoots());
712721
}
713722
List<File> testSourceDirs = new ArrayList<>(testSourceDirectories.size());
714723
for (String testSourceDir : testSourceDirectories) {
715724
testSourceDirs.add(FileUtils.resolveFile(project.getBasedir(), testSourceDir));
716725
}
717726
return testSourceDirs;
718727
}
728+
729+
private List<String> filterBuildTarget(List<String> sourceDirectories) {
730+
if (!excludeGeneratedSources) {
731+
return sourceDirectories;
732+
}
733+
734+
List<String> filtered = new ArrayList<>(sourceDirectories.size());
735+
Path buildTarget = FileUtils.resolveFile(
736+
project.getBasedir(), project.getBuild().getDirectory())
737+
.toPath();
738+
739+
for (String sourceDir : sourceDirectories) {
740+
Path src = FileUtils.resolveFile(project.getBasedir(), sourceDir).toPath();
741+
if (!src.startsWith(buildTarget)) {
742+
filtered.add(sourceDir);
743+
}
744+
}
745+
return filtered;
746+
}
719747
}

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

+30-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.io.OutputStream;
2828
import java.io.Reader;
2929
import java.nio.file.Files;
30+
import java.nio.file.Path;
3031
import java.util.ArrayList;
3132
import java.util.Collections;
3233
import java.util.List;
@@ -476,6 +477,14 @@ public class CheckstyleViolationCheckMojo extends AbstractMojo {
476477
@Parameter(defaultValue = "false")
477478
private boolean omitIgnoredModules;
478479

480+
/**
481+
* Specifies whether generated source files should be excluded from Checkstyle.
482+
*
483+
* @since 3.3.1
484+
*/
485+
@Parameter(property = "checkstyle.excludeGeneratedSources", defaultValue = "false")
486+
private boolean excludeGeneratedSources;
487+
479488
private ByteArrayOutputStream stringOutputStream;
480489

481490
private File outputXmlFile;
@@ -832,7 +841,7 @@ private List<Artifact> getCheckstylePluginDependenciesAsArtifacts(Map<String, Pl
832841

833842
private List<File> getSourceDirectories() {
834843
if (sourceDirectories == null) {
835-
sourceDirectories = project.getCompileSourceRoots();
844+
sourceDirectories = filterBuildTarget(project.getCompileSourceRoots());
836845
}
837846
List<File> sourceDirs = new ArrayList<>(sourceDirectories.size());
838847
for (String sourceDir : sourceDirectories) {
@@ -843,12 +852,31 @@ private List<File> getSourceDirectories() {
843852

844853
private List<File> getTestSourceDirectories() {
845854
if (testSourceDirectories == null) {
846-
testSourceDirectories = project.getTestCompileSourceRoots();
855+
testSourceDirectories = filterBuildTarget(project.getTestCompileSourceRoots());
847856
}
848857
List<File> testSourceDirs = new ArrayList<>(testSourceDirectories.size());
849858
for (String testSourceDir : testSourceDirectories) {
850859
testSourceDirs.add(FileUtils.resolveFile(project.getBasedir(), testSourceDir));
851860
}
852861
return testSourceDirs;
853862
}
863+
864+
private List<String> filterBuildTarget(List<String> sourceDirectories) {
865+
if (!excludeGeneratedSources) {
866+
return sourceDirectories;
867+
}
868+
869+
List<String> filtered = new ArrayList<>(sourceDirectories.size());
870+
Path buildTarget = FileUtils.resolveFile(
871+
project.getBasedir(), project.getBuild().getDirectory())
872+
.toPath();
873+
874+
for (String sourceDir : sourceDirectories) {
875+
Path src = FileUtils.resolveFile(project.getBasedir(), sourceDir).toPath();
876+
if (!src.startsWith(buildTarget)) {
877+
filtered.add(sourceDir);
878+
}
879+
}
880+
return filtered;
881+
}
854882
}

0 commit comments

Comments
 (0)