Build.gradle: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(88 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 Project#Overview|Project]]
=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]].


* [[Gradle Configuration#build.gradle|Gradle Configuration]]
=Example=
* [[Gradle_Concepts#build.gradle|Gradle Concepts]]
<syntaxhighlight lang='groovy'>


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


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:
dependencies {
    implementation 'org.slf4j:slf4j-api:1.7.12'
    testImplementation 'junit:junit:4.+'
}


* <tt>println</tt>
configurations {
* [[#Variables|variable declarations]]
    testImplementation.exclude group: 'com.example'
* [[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.
configurations.all {
    resolutionStrategy.cacheChangingModulesFor 3600, 'seconds'
}


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


There are two kinds of variables that can be declared in a build script: local variables and extra properties:
javadoc {
    options.charSet = 'UTF-8'
}


==Local Variables==
task runMain(type: JavaExec) {
    classpath = sourceSets.main.runtimeClasspath
    main = "io.novaordis.playground.gradle.java.Main"
}
</syntaxhighlight>


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.
=Comments=


<syntaxhighlight lang='groovy'>
<syntaxhighlight lang='groovy'>
def myVariable = "something"
// This is a comment
...
println myVariable
</syntaxhighlight>
</syntaxhighlight>


==Extra Properties==
=Code=


[[Gradle_Properties#Extra_Properties|Extra properties]] can be declared as follows:
The build script may include general-purpose Java and Groovy code:
<syntaxhighlight lang='groovy'>


<syntaxhighlight lang='groovy'>
class Experimental {
...
 
ext {
    private String name;
     log4jVersion = "2.1.7"
 
     emailNotification = "build@example.com"
    Experimental(String name) {
 
        this.name = name;
     }
 
     @Override
    public String toString() {
        return name;
    }
}
}
...
</syntaxhighlight>
=Repositories=


Repositories are declared in the
def experimental = new Experimental("blue");


<syntaxhighlight lang='groovy'>
task test {
repositories {
    doLast {
        println experimental
    }
}
}
</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
    }
}