diff --git a/buildSrc/src/main/resources/checkstyle.xml b/buildSrc/src/main/resources/checkstyle.xml index 867ff3751cb06..c6e8a8c3d3527 100644 --- a/buildSrc/src/main/resources/checkstyle.xml +++ b/buildSrc/src/main/resources/checkstyle.xml @@ -1,7 +1,7 @@ + "-//Puppy Crawl//DTD Check Configuration 1.3//EN" + "http://www.puppycrawl.com/dtds/configuration_1_3.dtd"> @@ -10,12 +10,16 @@ - + + + + + - - - - + + + + - - + + + + + + + + - - - - - + + @@ -45,15 +52,19 @@ + + - + + + - + hard to distinguish from the digit 1 (one). --> + @@ -82,7 +93,7 @@ - + - - - - - + + + + + + - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/buildSrc/version.properties b/buildSrc/version.properties index 13927e31914a0..b28936d20964e 100644 --- a/buildSrc/version.properties +++ b/buildSrc/version.properties @@ -4,7 +4,7 @@ lucene = 8.8.0-snapshot-737cb9c49b0 bundled_jdk_vendor = adoptopenjdk bundled_jdk = 15.0.1+9 -checkstyle = 8.20 +checkstyle = 8.39 # optional dependencies spatial4j = 0.7 diff --git a/gradle/ide.gradle b/gradle/ide.gradle index dbab6c140bcf4..3ef2efab6dd50 100644 --- a/gradle/ide.gradle +++ b/gradle/ide.gradle @@ -1,9 +1,9 @@ import org.elasticsearch.gradle.info.BuildParams -import org.jetbrains.gradle.ext.Remote import org.jetbrains.gradle.ext.JUnit + import java.nio.file.Files -import java.nio.file.StandardCopyOption import java.nio.file.Paths +import java.nio.file.StandardCopyOption buildscript { repositories { @@ -29,9 +29,11 @@ tasks.register('configureIdeCheckstyle') { description = 'Generated a suitable checkstyle config for IDEs' String checkstyleConfig = 'buildSrc/src/main/resources/checkstyle.xml' + String checkstyleSuppressions = 'buildSrc/src/main/resources/checkstyle_suppressions.xml' + String checkstyleIdeFragment = 'buildSrc/src/main/resources/checkstyle_ide_fragment.xml' String checkstyleIdeConfig = "$rootDir/checkstyle_ide.xml" - inputs.files(file(checkstyleConfig)) + inputs.files(file(checkstyleConfig), file(checkstyleIdeFragment)) outputs.files(file(checkstyleIdeConfig)) doLast { @@ -41,28 +43,33 @@ tasks.register('configureIdeCheckstyle') { Paths.get(file(checkstyleIdeConfig).getPath()), StandardCopyOption.REPLACE_EXISTING ) + + // There are some rules that we only want to enable in an IDE. These + // are extracted to a separate file, and merged into the IDE-specific + // Checkstyle config. + Node xmlFragment = parseXml(checkstyleIdeFragment) + // Edit the copy so that IntelliJ can copy with it modifyXml(checkstyleIdeConfig, { xml -> // Remove this module since it is implemented with custom code xml.module.findAll { it.'@name' == 'org.elasticsearch.gradle.checkstyle.SnippetLengthCheck' }.each { it.parent().remove(it) } - // Move the line length check because the IDE thinks it can't belong under a TreeWalker. Moving the - // configuration in the main file causes the command-line Checkstyle task to fail ¯\_(ツ)_/¯ + // Add all the nodes from the fragment file Node treeWalker = xml.module.find { it.'@name' == 'TreeWalker' } - Node lineLength = treeWalker.module.find { it.'@name' == 'LineLength' } - treeWalker.remove(lineLength) - xml.append(lineLength) + xmlFragment.module.each { treeWalker.append(it) } // Change the checkstyle config to inline the path to the // suppressions config. This removes a configuration step when using // the checkstyle config in an IDE. Node suppressions = xml.module.find { it.'@name' == 'SuppressionFilter' } - suppressions.property.findAll { it.'@name' == 'file' }.each { it.'@value' = "buildSrc/src/main/resources/checkstyle_suppressions.xml" } + suppressions.property.findAll { it.'@name' == 'file' }.each { it.'@value' = checkstyleSuppressions } }, "\n" + - "\n" + "\n" + + "\n" + + "\n" ) } } @@ -172,12 +179,10 @@ if (System.getProperty('idea.active') == 'true') { * but before the XML document, e.g. a doctype or comment */ void modifyXml(Object path, Action action, String preface = null) { - File xmlFile = project.file(path) - XmlParser xmlParser = new XmlParser(false, true, true) - xmlParser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false) - Node xml = xmlParser.parse(xmlFile) + Node xml = parseXml(path) action.execute(xml) + File xmlFile = project.file(path) xmlFile.withPrintWriter { writer -> def printer = new XmlNodePrinter(writer) printer.namespaceAware = true @@ -190,3 +195,11 @@ void modifyXml(Object path, Action action, String preface = null) printer.print(xml) } } + +Node parseXml(Object path) { + File xmlFile = project.file(path) + XmlParser xmlParser = new XmlParser(false, true, true) + xmlParser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false) + Node xml = xmlParser.parse(xmlFile) + return xml +}