5

I am confused by gradle build lifecycle for a few days. Refer tells me

A Gradle build has three distinct phases.

  • Initialization
  • Configuration
  • Execution

Is task creation in the third step? If so, how does gradle find all tasks in a project object?

Here is an example.

 rootporject
       |----app
            |----build.gradle
            |----checkstyle.gradle

The build.gradle is as simple as normal.checkstyle.gradle file has a task. Here is its content.

apply plugin: 'checkstyle'

task checkstyle(type:Checkstyle) {
    description 'Runs Checkstyle inspection against girl sourcesets.'
    group = 'Code Quality'
    configFile rootProject.file('checkstyle.xml')
    ignoreFailures = false
    showViolations true
    classpath = files()
    source 'src/main/java'
}

After ./gradlew -q tasks, there is no task checkstyle.

But if I remove the definition into build.gradle, I get it.

Is there anything wrong?Thanks in advance.

Edit

From doc

There is a one-to-one relationship between a Project and a build.gradle file.

CoXier
  • 2,523
  • 8
  • 33
  • 60
  • 2
    Does your `build.gradle` have an `apply from: 'checkstyle.gradle'` anywhere in it? How would the build script know about it? – mkobit Sep 14 '17 at 19:25
  • 1
    You need to include your `checkstyle.gradle` file the way @mkobit described. Only `build.gradle` files are automatically interpret. Task creation takes place in the second step or, if plugins use the `afterEvaluate` handler, between the second and the third step. Tasks must exist, before the *execution phase* starts, but during *configuration phase* you may need to identify tasks via their name instead of their instance (`dependsOn 'myTask'` instead of `dependsOn myTask`). – Lukas Körfer Sep 14 '17 at 21:04
  • @mkobit Thanks a lot, it works. Can you put your comment into answer? – CoXier Sep 15 '17 at 02:21

1 Answers1

4

You haven't applied your other script.

In your build.gradle you should add an apply from: 'checkstyle.gradle'.

mkobit
  • 43,979
  • 12
  • 156
  • 150
  • 2
    Here is a answer about difference between `apply from` and `apply plugin`https://stackoverflow.com/questions/29378249/difference-apply-from-vs-apply-plugin – CoXier Sep 15 '17 at 02:31