javagradlecheckstylesubproject

run checkstyles in subprojects before all tests in all subprojects


I have gradle project with 4 subprojects. I have current root gradle.build with checkstyle:

allprojects {
  apply plugin: "checkstyle"
  checkstyle {
  ...
  }
}

so when I run ./gradlew build in the main folder, I get next: checkstyle for 1st subproject, then tests. Then it runs checkstyle for 2nd subproject and then tests for 2nd, etc.

The problem is: if I have long tests in 1st subproject, I can wait a lot of time, and then discover that I have 2 spaces in the 4th project, so checkstyle fails, but I was waiting so much time for it.

What I really want: run all checks (checkstyle, and I have pmd too) for all subprojects, and then run all tests in all subprojects. It will save a lot of time for everybody in the team.

Can I do it except, make 2 different pipelines, and run them separately? like: ./gradlew allMyCheckstyles && ./gradlew build. I would love to use just ./gradlew build Thanks!

I tried many dependsOn, runAfter, but it didn't work out.


Solution

  • Apologies, a previous version of this answer misinterpreted the requirements of this question.

    Here's a solution that should do what you want:

    
    // Create a lifecycle task in the root project.
    // We'll make this depend on all checkstyle tasks from subprojects (see below)
    def checkstyleAllTask = task("checkstyleAll")
    
    // Make 'check' task depend on our new lifecycle task
    check.dependsOn(checkstyleAllTask)
    
    allProjects {
    
        // Ensure all checkstyle tasks are a dependency of the "checkstyleAll" task
        checkstyleAllTask.dependsOn(tasks.withType(Checkstyle))
    
        tasks.withType(Test) {
    
            // Indicate that testing tasks should run after the "checkstyleAll" task
            shouldRunAfter(checkstyleAllTask)
    
            // Indicate that testing tasks should run after any checksytle tasks.
            // This is useful for when you only want to run an individual
            // subproject's checks (e.g. ./gradlew ::subprojA::check)
            shouldRunAfter(tasks.withType(Checkstyle))
        }
    }
    

    Documentation here and here