Build.gradle: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(59 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
=External=
* https://docs.gradle.org/current/userguide/writing_build_scripts.html
* https://docs.gradle.org/current/userguide/writing_build_scripts.html


=Internal=
=Internal=
 
* [[Gradle_Concepts#Configuration_Scripts_and_Core_Types|Gradle Concepts]]
* [[Gradle Configuration#build.gradle|Gradle Configuration]]
* [[Gradle Project#Overview|Project]]
* [[Gradle_Concepts#build.gradle|Gradle Concepts]]


=Overview=
=Overview=
build.gradle is referred to as an Gradle build script. It is the script interface to a [[Gradle_Project#Project_API_and_build.gradle_DSL|Project instance]].


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:
=Example=
 
* <tt>println</tt>
* [[#Variables|variable declarations]]
* [[Gradle_Concepts#Plugin|plugin declarations]]
* [[#Repositories|repositories]]
 
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.
 
<syntaxhighlight lang='groovy'>
<syntaxhighlight lang='groovy'>
def myVariable = "something"
...
println myVariable
</syntaxhighlight>


==Extra Properties==
apply plugin: 'java'


[[Gradle_Properties#Extra_Properties|Extra properties]] can be declared as follows:
// name comes from the directory name or it can be changed in settings.gradle
group = "playground.example"
version = "0.1.0"
description = "A Gradle example project"


<syntaxhighlight lang='groovy'>
repositories {
...
     mavenLocal()
ext {
     mavenCentral()
     log4jVersion = "2.1.7"
     emailNotification = "build@example.com"
}
}
...
</syntaxhighlight>


=DSL=
dependencies {
    implementation 'org.slf4j:slf4j-api:1.7.12'
    testImplementation 'junit:junit:4.+'
}


The following elements are part of [https://docs.gradle.org/current/dsl/org.gradle.api.Project.html Project] API.
configurations {
 
    testImplementation.exclude group: 'com.example'
==configurations.all==
}


<syntaxhighlight lang='groovy'>
configurations.all {
configurations.all {
     resolutionStrategy.cacheDynamicVersionsFor 10, 'minutes'
     resolutionStrategy.cacheChangingModulesFor 3600, 'seconds'
}
}
</syntaxhighlight>


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


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


=====cacheDynamicVersionsFor=====
task runMain(type: JavaExec) {
 
    classpath = sourceSets.main.runtimeClasspath
Configures [[Gradle_Concepts#Dynamic_Version|dynamic version]] caching threshold.
    main = "io.novaordis.playground.gradle.java.Main"
 
}
<syntaxhighlight lang='groovy'>
resolutionStrategy.cacheDynamicVersionsFor 10, 'minutes'
</syntaxhighlight>
</syntaxhighlight>


==buildscript==
=Comments=
 
Used when [[Gradle Concepts#External_Plugins|external plugins]] need to be downloaded.


<syntaxhighlight lang='groovy'>
<syntaxhighlight lang='groovy'>
buildscript {
// This is a comment
    repositories {
        maven { url 'https://plugins.gradle.org/m2/' }
    }
  dependencies {
    classpath 'com.netflix.nebula:gradle-ospackage-plugin:4.3.0'
  }
}
</syntaxhighlight>
</syntaxhighlight>


==<span id='Repositories'></span>repositories==
=Code=
 
{{External|https://docs.gradle.org/current/userguide/declaring_repositories.html}}
 
[[Gradle Concepts#Repository|Repositories]] are declared in a 'repositories' block:


The build script may include general-purpose Java and Groovy code:
<syntaxhighlight lang='groovy'>
<syntaxhighlight lang='groovy'>
repositories {
    ...
}
</syntaxhighlight>


Multiple repositories can be declared in the same block. Syntax examples are available for [[Gradle_Concepts#Custom_Maven_Repository|custom Maven repositories]], [[Gradle_Concepts#flatDir|directory-based repositories]], [[Gradle_Concepts#Maven_Central|Maven Central]], etc. The most common setting is declaring that our dependencies are available in Maven Central:
class Experimental {


<syntaxhighlight lang='groovy'>
     private String name;
repositories {
     mavenCentral()
}
</syntaxhighlight>


==dependencies==
    Experimental(String name) {


A DSL element that declares the [[Gradle_Concepts#Dependency|dependencies]] of the [[Gradle_Concepts#Module|module]]. The block lists [[Gradle_Concepts#Dependency_Configuration|dependency configurations]] known to the project, followed by list of dependencies.
        this.name = name;
    }


<syntaxhighlight lang='groovy'>
    @Override
dependencies {
    public String toString() {
    implementation 'org.slf4j:slf4j-api:1.7.12'
        return name;
    api 'com.google.guava:guava:23.0'
     }
     ...
}
}
</syntaxhighlight>
Note that the declared dependencies must be available in a declared repository, so [[#repositories|repositories must be declared]].


Dependencies can also be declared without a version, and [[Gradle_Concepts#Dependency_Constraint|dependency constraints]] can be used to manage the version, including the transitive versions.
def experimental = new Experimental("blue");


<syntaxhighlight lang='groovy'>
task test {
dependencies {
     doLast {
     implementation 'org.slf4j:slf4j-api'
         println experimental
    constraints {
         implementation 'org.slf4j:slf4j-api:1.7.12'
     }
     }
}
}
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 22:10, 11 October 2020

External

Internal

Overview

build.gradle is referred to as an Gradle build script. It is the script interface to a Project instance.

Example

apply plugin: 'java'

// name comes from the directory name or it can be changed in settings.gradle
group = "playground.example"
version = "0.1.0"
description = "A Gradle example project"

repositories {
    mavenLocal()
    mavenCentral()
}

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

configurations {
    testImplementation.exclude group: 'com.example'
}

configurations.all {
    resolutionStrategy.cacheChangingModulesFor 3600, 'seconds'
}

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

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

task runMain(type: JavaExec) {
    classpath = sourceSets.main.runtimeClasspath
    main = "io.novaordis.playground.gradle.java.Main"
}

Comments

// This is a comment

Code

The build script may include general-purpose Java and Groovy code:

class Experimental {

    private String name;

    Experimental(String name) {

        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }
}

def experimental = new Experimental("blue");

task test {
    doLast {
        println experimental
    }
}