Skip to content

Commit

Permalink
[MCHECKSTYLE-412] Add option to exclude generated sources/test-source…
Browse files Browse the repository at this point in the history
…s from default source/test-source directories

This closes #76
  • Loading branch information
gmshake authored and michael-o committed May 24, 2023
1 parent 9ebda63 commit 1c271f1
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 4 deletions.
18 changes: 18 additions & 0 deletions src/it/MCHECKSTYLE-412/invoker.properties
Original file line number Diff line number Diff line change
@@ -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=verify
77 changes: 77 additions & 0 deletions src/it/MCHECKSTYLE-412/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.plugins.checkstyle.its</groupId>
<artifactId>MCHECKSTYLE-412</artifactId>
<version>1.0-SNAPSHOT</version>

<url>https://issues.apache.org/jira/browse/MCHECKSTYLE-412</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4</artifactId>
<version>4.3</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>4.3</version>
<executions>
<execution>
<id>antlr</id>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>@project.version@</version>
<configuration>
<excludeGeneratedSources>true</excludeGeneratedSources>
</configuration>
<executions>
<execution>
<id>check</id>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
21 changes: 21 additions & 0 deletions src/it/MCHECKSTYLE-412/src/main/antlr4/MyGrammar.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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.

grammar MyGrammar;
r : 'foo' ID ;
ID : [a-zA-Z0-9_]+ ;
WS : [ \t\r\n]+ -> skip ;
26 changes: 26 additions & 0 deletions src/it/MCHECKSTYLE-412/src/main/java/org/MyClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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 {
}
23 changes: 23 additions & 0 deletions src/it/MCHECKSTYLE-412/src/main/java/org/package-info.java
Original file line number Diff line number Diff line change
@@ -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.
*/
22 changes: 22 additions & 0 deletions src/it/MCHECKSTYLE-412/verify.groovy
Original file line number Diff line number Diff line change
@@ -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( "[INFO] You have 0 Checkstyle violations." )
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -426,6 +427,14 @@ public abstract class AbstractCheckstyleReport extends AbstractMavenReport {
+ " \"https://checkstyle.org/dtds/configuration_1_3.dtd\">\n")
private String checkstyleRulesHeader;

/**
* Specifies whether generated source files should be excluded from Checkstyle.
*
* @since 3.3.1
*/
@Parameter(property = "checkstyle.excludeGeneratedSources", defaultValue = "false")
private boolean excludeGeneratedSources;

/**
*/
@Component
Expand Down Expand Up @@ -697,7 +706,7 @@ private boolean checkMavenJxrPluginIsConfigured() {

protected List<File> getSourceDirectories() {
if (sourceDirectories == null) {
sourceDirectories = project.getCompileSourceRoots();
sourceDirectories = filterBuildTarget(project.getCompileSourceRoots());
}
List<File> sourceDirs = new ArrayList<>(sourceDirectories.size());
for (String sourceDir : sourceDirectories) {
Expand All @@ -708,12 +717,31 @@ protected List<File> getSourceDirectories() {

protected List<File> getTestSourceDirectories() {
if (testSourceDirectories == null) {
testSourceDirectories = project.getTestCompileSourceRoots();
testSourceDirectories = filterBuildTarget(project.getTestCompileSourceRoots());
}
List<File> testSourceDirs = new ArrayList<>(testSourceDirectories.size());
for (String testSourceDir : testSourceDirectories) {
testSourceDirs.add(FileUtils.resolveFile(project.getBasedir(), testSourceDir));
}
return testSourceDirs;
}

private List<String> filterBuildTarget(List<String> sourceDirectories) {
if (!excludeGeneratedSources) {
return sourceDirectories;
}

List<String> filtered = new ArrayList<>(sourceDirectories.size());
Path buildTarget = FileUtils.resolveFile(
project.getBasedir(), project.getBuild().getDirectory())
.toPath();

for (String sourceDir : sourceDirectories) {
Path src = FileUtils.resolveFile(project.getBasedir(), sourceDir).toPath();
if (!src.startsWith(buildTarget)) {
filtered.add(sourceDir);
}
}
return filtered;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.OutputStream;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -476,6 +477,14 @@ public class CheckstyleViolationCheckMojo extends AbstractMojo {
@Parameter(defaultValue = "false")
private boolean omitIgnoredModules;

/**
* Specifies whether generated source files should be excluded from Checkstyle.
*
* @since 3.3.1
*/
@Parameter(property = "checkstyle.excludeGeneratedSources", defaultValue = "false")
private boolean excludeGeneratedSources;

private ByteArrayOutputStream stringOutputStream;

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

private List<File> getSourceDirectories() {
if (sourceDirectories == null) {
sourceDirectories = project.getCompileSourceRoots();
sourceDirectories = filterBuildTarget(project.getCompileSourceRoots());
}
List<File> sourceDirs = new ArrayList<>(sourceDirectories.size());
for (String sourceDir : sourceDirectories) {
Expand All @@ -843,12 +852,31 @@ private List<File> getSourceDirectories() {

private List<File> getTestSourceDirectories() {
if (testSourceDirectories == null) {
testSourceDirectories = project.getTestCompileSourceRoots();
testSourceDirectories = filterBuildTarget(project.getTestCompileSourceRoots());
}
List<File> testSourceDirs = new ArrayList<>(testSourceDirectories.size());
for (String testSourceDir : testSourceDirectories) {
testSourceDirs.add(FileUtils.resolveFile(project.getBasedir(), testSourceDir));
}
return testSourceDirs;
}

private List<String> filterBuildTarget(List<String> sourceDirectories) {
if (!excludeGeneratedSources) {
return sourceDirectories;
}

List<String> filtered = new ArrayList<>(sourceDirectories.size());
Path buildTarget = FileUtils.resolveFile(
project.getBasedir(), project.getBuild().getDirectory())
.toPath();

for (String sourceDir : sourceDirectories) {
Path src = FileUtils.resolveFile(project.getBasedir(), sourceDir).toPath();
if (!src.startsWith(buildTarget)) {
filtered.add(sourceDir);
}
}
return filtered;
}
}

0 comments on commit 1c271f1

Please sign in to comment.