Search code examples
gradlebuild.gradlecheckstylepmdjava-test-fixtures

Adding Test Fixtures into checkstyle checks in gradle multiproject


Recently found out about the java-test-fixtures gradle plugin and added it in order to share some functionality between tests for sub projects in a big multi-project repo, which has strict checkstyle and PMD checks.

The problem is that those classes in the test fixtures folder are not included in the checkstyle or PMD checks, since they are set to only look in to the src/main and src/test folders by default.

My main question is if there is a way to include the testFixtures/ folder into the checkstyle checks.

I tried creating a custom source set for the testFixtures in gradle, but couldn't add it as one of the sourceSets for the checkstyle task


Solution

  • Ended up finding the solution through sheer luck, so came here to answer in case anyone stumbles upon this:

    both Checkstyle and PMD gradle tasks are aware of the sourceSets, so once the gradle is reloaded it'll be aware that there's a new sourceSet for the testFixtures, which then generates the checkstyleTestFixtures and pmdTestFixtures, which can be chained to the main checkstyle and pmd tasks in the following way (for PMD just swap checkstyle for pmd):

    tasks.register('checkstyle') {
        finalizedBy checkstyleMain //typical chain for checks
        finalizedBy checkstyleTest //typical chain for checks
        
        //this if statement checks if the testFixture directory is present
        //in order to only add the task dependency if the plugin is used
        if (!fileTree('src/testFixtures').empty) 
            finalizedBy checkstyleTestFixtures
    }