Skip to content

Commit

Permalink
Improve baseline-checkstyle implementation and upgrade to 8.12 (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
robert3005 authored Aug 13, 2018
1 parent 41f8151 commit 5375154
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 111 deletions.
22 changes: 11 additions & 11 deletions gradle-baseline-java-config/resources/checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@
<property name="file" value="${config_loc}/checkstyle-suppressions.xml"/>
</module>
<module name="SuppressWarningsFilter"/> <!-- baseline-gradle: README.md -->
<module name="SuppressionCommentFilter"/> <!-- baseline-gradle: README.md -->
<module name="SuppressionCommentFilter">
<property name="offCommentFormat" value="CHECKSTYLE.OFF\: ([\w\|]+)"/>
<property name="onCommentFormat" value="CHECKSTYLE.ON\: ([\w\|]+)"/>
<property name="checkFormat" value="$1"/>
</module>
<module name="TreeWalker">
<module name="SuppressionCommentFilter"/> <!-- baseline-gradle: README.md -->
<module name="SuppressionCommentFilter">
<property name="offCommentFormat" value="CHECKSTYLE.OFF\: ([\w\|]+)"/>
<property name="onCommentFormat" value="CHECKSTYLE.ON\: ([\w\|]+)"/>
<property name="checkFormat" value="$1"/>
</module>
<module name="AbbreviationAsWordInName"> <!-- Java Style Guide: Camel case : defined -->
<property name="ignoreFinal" value="false"/>
<property name="allowedAbbreviationLength" value="1"/>
Expand Down Expand Up @@ -105,7 +105,6 @@
<module name="EmptyStatement"/> <!-- Java Style Guide: One statement per line -->
<module name="EqualsHashCode"/>
<module name="FallThrough"/> <!-- Java Style Guide: Fall-through: commented -->
<module name="FileContentsHolder"/> <!-- Required for SuppressionCommentFilter -->
<module name="FinalClass"/> <!-- Java Coding Guidelines: Private constructors -->
<module name="GenericWhitespace"> <!-- Java Style Guide: Horizontal whitespace -->
<message key="ws.followed" value="GenericWhitespace ''{0}'' is followed by whitespace."/>
Expand Down Expand Up @@ -174,16 +173,15 @@
<module name="ImportOrder"> <!-- Java Style Guide: Ordering and spacing -->
<property name="groups" value="/.*/"/>
<property name="option" value="top"/>
<property name="separated" value="true"/>
<property name="sortStaticImportsAlphabetically" value="true"/>
</module>
<module name="Indentation"> <!-- Java Style Guide: Block indentation: +4 spaces -->
<property name="arrayInitIndent" value="8"/>
<property name="lineWrappingIndentation" value="8"/>
</module>
<module name="InnerAssignment"/> <!-- Java Coding Guidelines: Inner assignments: Not used -->
<module name="LeftCurly"> <!-- Java Style Guide: Nonempty blocks: K & R style -->
<property name="maxLineLength" value="120"/>
</module>
<module name="LeftCurly"/> <!-- Java Style Guide: Nonempty blocks: K & R style -->
<module name="LineLength"> <!-- Java Style Guide: No line-wrapping -->
<property name="max" value="120"/>
<property name="ignorePattern" value="^package.*|^import.*|a href|href|http://|https://|ftp://"/>
Expand Down Expand Up @@ -398,7 +396,9 @@
<property name="target" value="CLASS_DEF, INTERFACE_DEF, ENUM_DEF, METHOD_DEF, CTOR_DEF, VARIABLE_DEF"/>
</module>
<module name="CyclomaticComplexity"/> <!-- Java Coding Guidelines: Reduce Cyclomatic Complexity -->
<module name="DesignForExtension"/> <!-- Java Coding Guidelines: Design for extension -->
<module name="DesignForExtension"> <!-- Java Coding Guidelines: Design for extension -->
<property name="ignoredAnnotations" value="Test, Before, BeforeEach, After, AfterEach, BeforeClass, BeforeAll, AfterClass, AfterAll"/>
</module>
<module name="JavadocMethod"> <!-- Java Style Guide: Where Javadoc is used -->
<property name="scope" value="public"/>
<property name="allowMissingParamTags" value="true"/>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* (c) Copyright 2015 Palantir Technologies Inc. All rights reserved.
*
* Licensed 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.
*/

package com.palantir.baseline.plugins;

import java.nio.file.Paths;
import java.util.stream.Stream;
import org.gradle.api.Project;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.plugins.quality.Checkstyle;
import org.gradle.api.plugins.quality.CheckstyleExtension;
import org.gradle.api.plugins.quality.CheckstylePlugin;
import org.gradle.api.tasks.javadoc.Javadoc;
import org.gradle.external.javadoc.StandardJavadocDocletOptions;
import org.gradle.plugins.ide.eclipse.model.EclipseModel;
import org.gradle.plugins.ide.eclipse.model.EclipseProject;

/**
* Configures the Gradle "checkstyle" task with Baseline settings.
*/
public final class BaselineCheckstyle extends AbstractBaselinePlugin {

private static final String DEFAULT_CHECKSTYLE_VERSION = "8.12";

@Override
public void apply(Project project) {
this.project = project;

project.getPluginManager().apply(CheckstylePlugin.class);

// Set default version (outside afterEvaluate so it can be overridden).
project.getExtensions()
.configure(CheckstyleExtension.class, ext -> ext.setToolVersion(DEFAULT_CHECKSTYLE_VERSION));

// Configure checkstyle
project.getPluginManager().withPlugin("java", plugin -> {
JavaPluginConvention javaConvention = project.getConvention().getPlugin(JavaPluginConvention.class);
// We use the "JavadocMethod" module in our Checkstyle configuration, making
// Java 8+ new doclint compiler feature redundant.
if (javaConvention.getSourceCompatibility().isJava8Compatible()) {
project.getTasks().withType(Javadoc.class, javadoc ->
javadoc.options(javadocOptions -> ((StandardJavadocDocletOptions) javadocOptions)
.addStringOption("Xdoclint:none", "-quiet")));
}
project.getTasks().withType(Checkstyle.class, checkstyle -> {
// Make checkstyle include files in src/main/resources and src/test/resources, e.g.,
// for whitespace checks.
javaConvention.getSourceSets()
.forEach(sourceSet -> sourceSet.getResources().getSrcDirs()
.forEach(resourceDir -> checkstyle.source(resourceDir.toString())));
// These sources are only checked by gradle, NOT by Eclipse.
Stream.of("checks", "manifests", "scripts", "templates").forEach(checkstyle::source);
// Make sure java files are still included. This should match list in etc/eclipse-template/.checkstyle.
// Currently not enforced, but could be eventually.
Stream.of(
"java", "cfg", "coffee", "erb", "groovy", "handlebars", "json", "less", "pl", "pp", "sh", "xml")
.forEach(extension -> checkstyle.include("**/*." + extension));
});
});

project.getExtensions().getByType(CheckstyleExtension.class)
.setConfigDir(this.project.file(Paths.get(getConfigDir(), "checkstyle").toString()));
project.getPluginManager().withPlugin("eclipse", plugin -> {
EclipseProject eclipseProject = project.getExtensions().getByType(EclipseModel.class).getProject();
eclipseProject.buildCommand("net.sf.eclipsecs.core.CheckstyleBuilder");
eclipseProject.natures("net.sf.eclipsecs.core.CheckstyleNature");
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ package com.palantir.baseline

import com.palantir.baseline.plugins.BaselineCheckstyle
import com.palantir.baseline.plugins.BaselineEclipse
import nebula.test.multiproject.MultiProjectIntegrationHelper
import org.gradle.api.Project
import org.gradle.api.plugins.quality.Checkstyle
import org.gradle.api.plugins.quality.CheckstylePlugin
import org.gradle.testfixtures.ProjectBuilder
import org.junit.Rule
import org.junit.rules.TemporaryFolder
import spock.lang.Specification

class BaselineCheckstyleTest extends Specification {
Expand All @@ -42,11 +46,25 @@ class BaselineCheckstyleTest extends Specification {
project.plugins.hasPlugin(CheckstylePlugin.class)
}

def includesResources() {
def file = new File(project.projectDir, 'src/test/resources/checkstyle.xml')
file.getParentFile().mkdirs()
when:
file << '''
<?xml version="1.0"?>
'''.stripIndent()

then:
def tasks = project.tasks.withType(Checkstyle.class)
for (Checkstyle task : tasks) {
assert task.getSource().getFiles().contains(file)
}
}

def appliesEclipseNatures() {
when:
project.plugins.apply 'eclipse'
project.plugins.apply BaselineEclipse
project.plugins.findPlugin(BaselineCheckstyle).configureCheckstyleForEclipse()

then:
project.eclipse.project.natures.contains("net.sf.eclipsecs.core.CheckstyleNature")
Expand Down

0 comments on commit 5375154

Please sign in to comment.