Build.gradle: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 4: Line 4:
Applies enclosed to all [[Gradle_Concepts_ToDeplete#Dependency_Configuration|configurations]].
Applies enclosed to all [[Gradle_Concepts_ToDeplete#Dependency_Configuration|configurations]].


<syntaxhighlight lang='groovy'>
configurations.all {
    resolutionStrategy.cacheDynamicVersionsFor 10, 'minutes'
}
</syntaxhighlight>
Alternative:
<syntaxhighlight lang='groovy'>
configurations.all {
    resolutionStrategy {
        cacheDynamicVersionsFor 10, 'minutes'
    }
}
</syntaxhighlight>


===Configuration===
===Configuration===

Revision as of 23:24, 19 May 2018

configurations.all

Applies enclosed to all configurations.


Configuration

resolutionStrategy

https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.ResolutionStrategy.html
cacheDynamicVersionsFor

Configures dynamic version caching threshold.

resolutionStrategy.cacheDynamicVersionsFor 10, 'minutes'
cacheChangingModulesFor

configurations

configurations {
    testCompile.exclude group: 'io.example'
}

buildscript

Used when external plugins need to be downloaded.

buildscript {
    repositories {
        maven { url 'https://plugins.gradle.org/m2/' }
    }
  dependencies {
    classpath 'com.netflix.nebula:gradle-ospackage-plugin:4.3.0'
  }
}

repositories

https://docs.gradle.org/current/userguide/declaring_repositories.html

Repositories are declared in a 'repositories' block:

repositories {
    ...
}

Multiple repositories can be declared in the same block. Syntax examples are available for custom Maven repositories, directory-based repositories, Maven Central, etc. The most common setting is declaring that our dependencies are available in Maven Central:

repositories {
    mavenCentral()
}

Multi-Project Configuration

allprojects

The allprojects block is used to add configuration items that will apply to all sub-projects, as well as the root project.

allprojects {
  repositories {
    ...
  }
}

"allprojects" is a property that contains a list with the current project and all its subprojects underneath it. It can be called with a closure, and the statements of the closure are delegated to the projects.

subprojects

The subprojects block is used to add configuration times for sub-projects only.

subprojects {
    ...
}

Respond to Build Lifecycle Events

Example of how to respond to project build lifecycle events.

allprojects {
      afterEvaluate { project ->
          if (project.hasTests) {
              println "Adding test task to $project"
              project.task('test') {
                   doLast {
                         println "running tests for $project"
                   }
              }
          }
     }
}
gradle.afterProject {project, projectState ->
      if (projectState.failure) {
          println "Evaluation of $project FAILED"
      } else {
          println "Evaluation of $project succeeded"
      }
}

Respond to Task Lifecycle Events

Examples of how to respond to task lifecycle events.

Task Creation

tasks.whenTaskAdded { task ->
      task.ext.srcDir = 'src/main/java'
}

Task Execution

gradle.taskGraph.beforeTask { Task task ->
      println "executing $task ..."
}
gradle.taskGraph.afterTask { Task task, TaskState state ->
      if (state.failure) {
          println "FAILED"
      }
      else {
          println "done"
      }
}