Skip to content

[MCHECKSTYLE-412] - Exclude generated sources / test-sources from defau… #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}