Build.gradle: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 48: Line 48:


     testImplementation.exclude group: 'com.example'
     testImplementation.exclude group: 'com.example'
}
compileJava {
    options.compilerArgs += ['-Xlint']
}
javadoc {
    options.charSet = 'UTF-8'
}
}



Revision as of 21:04, 3 May 2018

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.

Example

apply plugin: 'java'

group = "io.novaordis.playground.gradle"
version = "1.0"
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {

    mavenCentral()
}

configurations.all {

    resolutionStrategy.cacheChangingModulesFor 3600, 'seconds'
}

dependencies {

    implementation 'org.slf4j:slf4j-api:1.7.12'
    testImplementation 'junit:junit:4.+'
}

configurations {

    testImplementation.exclude group: 'com.example'
}

compileJava {
    options.compilerArgs += ['-Xlint']
}

javadoc {
    options.charSet = 'UTF-8'
}

task runMain(type: JavaExec) {

    main = "io.novaordis.playground.gradle.java.Main"
    systemProperties System.properties
}

Comments

// This is a comment

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

The following elements are part of Project API.

configurations.all

Applies enclosed to all configurations.

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

Alternative:

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

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

dependencies

A DSL element that declares the dependencies of the module. The block lists dependency configurations known to the project, followed by list of dependencies.

dependencies {
    implementation 'org.slf4j:slf4j-api:1.7.12'
    api 'com.google.guava:guava:23.0'
    ...
}

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

Custom Tasks

Custom Tasks