Build.gradle

From NovaOrdis Knowledge Base
Revision as of 23:11, 1 May 2018 by Ovidiu (talk | contribs) (→‎DSL)
Jump to navigation Jump to search

External

Internal

Overview

The project-level configuration script that contains task configuration for the current project, written in the Gradle's DSL. It tells Gradle what to build. The build script may contain any Groovy language element, including:

To make build scripts more concise, Gradle automatically adds the set of default Gradle import statements to the script.

Variables

There are two kinds of variables that can be declared in a build script: local variables and extra properties:

Local Variables

Local variables, which are a feature of the underlying Groovy language, are declared with the "def" keyword. They are only visible in the scope where they have been declared.

def myVariable = "something"
...
println myVariable

Extra Properties

Extra properties can be declared as follows:

...
ext {
    log4jVersion = "2.1.7"
    emailNotification = "build@example.com"
}
...

DSL

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'
  }
}

configurations.all

configurations.all {
    resolutionStrategy.cacheDynamicVersionsFor 10, 'minutes'
}

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()
}

dependencies

A DSL element that declares the dependencies of the module.

dependencies {
    implementation 'org.slf4j:slf4j-api:1.7.12'
}

Note that the declared dependencies must be available in a declared repository, so repositories must be declared.

Dependencies can also be declared without a version, and dependency constraints can be used to manage the version, including the transitive versions.

dependencies {
    implementation 'org.slf4j:slf4j-api'
    constraints {
        implementation 'org.slf4j:slf4j-api:1.7.12'
    }
}