2

I am trying to apply the checkstyle plugin to my gradle project. The configuration of which is in a seperate, shared jar dependency:

project.apply plugin: StaticAnalysisPlugin

class StaticAnalysisPlugin implements Plugin<Project> {
    @Override
    void apply(Project project) {
        project.apply plugin: 'checkstyle'     

        project.configurations {
            codingStandardsConfig
        }

        project.dependencies {
            codingStandardsConfig 'com.sample.tools:coding-standards:1.+:@jar'
        }

        def checkstyleConfigFileLocation = "classpath:sample-checkstyle-config.xml"

        project.checkstyle {
            toolVersion = '6.3'
            project.logger.debug "$project Using checkstyle version $toolVersion."
            project.logger.debug "$project Using checkstyle config from: ${checkstyleConfigFileLocation}"
            config = project.resources.text.fromFile(checkstyleConfigFileLocation)
        }

        project.checkstyleMain.source = "src/main/java"
        project.checkstyleTest.exclude "**/*"
    }
}

the config file is located in the coding-standards jar, but I am unsure how to wire this in to the checkstyle config.

barfuin
  • 16,865
  • 10
  • 85
  • 132
75inchpianist
  • 4,112
  • 1
  • 21
  • 39

1 Answers1

0

I think you should use the configFile option (which expects a File object) rather than the config option (which expects a text resource with your actual configuration).

Then the problem comes down to determining the correct File object. From this answer, it seems you can do

ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("file/test.xml").getFile());

Alternative solutions can be found on StackOverflow. Make sure that the class loader's class path includes your coding-standards.jar. You may have to put that dependency in a buildscript block to make it available, well, to the build script.

Community
  • 1
  • 1
barfuin
  • 16,865
  • 10
  • 85
  • 132