乐闻世界logo
搜索文章和话题

What phases does Gradle's lifecycle include? What is the purpose of each phase?

2月21日 18:10

Gradle's lifecycle consists of three main phases:

1. Initialization Phase

  • Purpose: Determine which projects will participate in the build and create Project instances for each
  • Execution:
    • Read settings.gradle or settings.gradle.kts file
    • Determine project structure based on include statements
    • Create Project instances for each included project
    • Execute init.gradle (if present)
  • Example:
    groovy
    // settings.gradle rootProject.name = 'my-project' include 'app', 'library', 'common'

2. Configuration Phase

  • Purpose: Execute build scripts for all projects and build the task dependency graph
  • Execution:
    • Execute build.gradle or build.gradle.kts for each project
    • Configure project properties, plugins, dependencies
    • Create and configure all tasks
    • Establish dependencies between tasks
  • Characteristics:
    • All projects are configured even if only one task is executed
    • Use gradle -m or --dry-run to view tasks that will be executed
  • Optimization Tips:
    groovy
    // Use onlyIf or enabled to skip unnecessary configuration tasks.register('myTask') { onlyIf { project.hasProperty('enableMyTask') } }

3. Execution Phase

  • Purpose: Execute actual tasks according to the task dependency graph
  • Execution:
    • Only execute tasks specified on the command line and their dependencies
    • Execute task action logic
    • Support incremental builds, only process changed files
  • Characteristics:
    • Task execution order is determined by dependencies
    • Support parallel execution (via --parallel parameter)
    • Support continuous builds (via --continuous parameter)

Lifecycle Hooks

Gradle provides multiple lifecycle hooks to execute custom logic at specific stages:

groovy
// Initialization phase gradle.projectsLoaded { println "All projects loaded" } // Configuration phase gradle.beforeProject { project -> println "Configuring project: ${project.name}" } gradle.afterProject { project -> println "Project ${project.name} configuration complete" } // Execution phase gradle.taskGraph.whenReady { graph -> println "Task graph ready" } gradle.taskGraph.beforeTask { task -> println "About to execute task: ${task.name}" } gradle.taskGraph.afterTask { task, state -> println "Task ${task.name} completed, status: ${state.failure ? 'failed' : 'success'} }

Performance Optimization Recommendations

  1. Reduce work in configuration phase: Move unnecessary logic to execution phase
  2. Use lazy initialization: Use tasks.register() instead of tasks.create()
  3. Avoid I/O operations in configuration phase: Such as network requests, file reads/writes, etc.
  4. Use configuration cache: Enable via --configuration-cache parameter
标签:Gradle